1 #include "PhysicsLoopBack.h"
2 #include "PhysicsServerSharedMemory.h"
3 #include "PhysicsClientSharedMemory.h"
4 #include "../CommonInterfaces/CommonGUIHelperInterface.h"
5 #include "PhysicsServerCommandProcessor.h"
6 #include "../CommonInterfaces/CommonExampleInterface.h"
7 struct PhysicsLoopBackInternalData
8 {
9 	CommandProcessorInterface* m_commandProcessor;
10 	PhysicsClientSharedMemory* m_physicsClient;
11 	PhysicsServerSharedMemory* m_physicsServer;
12 	DummyGUIHelper m_noGfx;
13 
PhysicsLoopBackInternalDataPhysicsLoopBackInternalData14 	PhysicsLoopBackInternalData()
15 		: m_commandProcessor(0),
16 		  m_physicsClient(0),
17 		  m_physicsServer(0)
18 	{
19 	}
20 };
21 
22 struct Bullet2CommandProcessorCreation2 : public CommandProcessorCreationInterface
23 {
createCommandProcessorBullet2CommandProcessorCreation224 	virtual class CommandProcessorInterface* createCommandProcessor()
25 	{
26 		PhysicsServerCommandProcessor* proc = new PhysicsServerCommandProcessor;
27 		return proc;
28 	}
29 
deleteCommandProcessorBullet2CommandProcessorCreation230 	virtual void deleteCommandProcessor(CommandProcessorInterface* proc)
31 	{
32 		delete proc;
33 	}
34 };
35 
36 static Bullet2CommandProcessorCreation2 sB2Proc;
37 
PhysicsLoopBack()38 PhysicsLoopBack::PhysicsLoopBack()
39 {
40 	m_data = new PhysicsLoopBackInternalData;
41 	m_data->m_physicsServer = new PhysicsServerSharedMemory(&sB2Proc, 0, 0);
42 	m_data->m_physicsClient = new PhysicsClientSharedMemory();
43 }
44 
~PhysicsLoopBack()45 PhysicsLoopBack::~PhysicsLoopBack()
46 {
47 	delete m_data->m_physicsClient;
48 	delete m_data->m_physicsServer;
49 	delete m_data->m_commandProcessor;
50 	delete m_data;
51 }
52 
53 // return true if connection succesfull, can also check 'isConnected'
connect()54 bool PhysicsLoopBack::connect()
55 {
56 	m_data->m_physicsServer->connectSharedMemory(&m_data->m_noGfx);
57 	m_data->m_physicsClient->connect();
58 	return m_data->m_physicsClient->isConnected();
59 }
60 
61 ////todo: rename to 'disconnect'
disconnectSharedMemory()62 void PhysicsLoopBack::disconnectSharedMemory()
63 {
64 	m_data->m_physicsClient->disconnectSharedMemory();
65 	m_data->m_physicsServer->disconnectSharedMemory(true);
66 }
67 
isConnected() const68 bool PhysicsLoopBack::isConnected() const
69 {
70 	return m_data->m_physicsClient->isConnected();
71 }
72 
73 // return non-null if there is a status, nullptr otherwise
processServerStatus()74 const SharedMemoryStatus* PhysicsLoopBack::processServerStatus()
75 {
76 	m_data->m_physicsServer->processClientCommands();
77 	return m_data->m_physicsClient->processServerStatus();
78 }
79 
getAvailableSharedMemoryCommand()80 SharedMemoryCommand* PhysicsLoopBack::getAvailableSharedMemoryCommand()
81 {
82 	return m_data->m_physicsClient->getAvailableSharedMemoryCommand();
83 }
84 
canSubmitCommand() const85 bool PhysicsLoopBack::canSubmitCommand() const
86 {
87 	return m_data->m_physicsClient->canSubmitCommand();
88 }
89 
submitClientCommand(const struct SharedMemoryCommand & command)90 bool PhysicsLoopBack::submitClientCommand(const struct SharedMemoryCommand& command)
91 {
92 	return m_data->m_physicsClient->submitClientCommand(command);
93 }
94 
getNumBodies() const95 int PhysicsLoopBack::getNumBodies() const
96 {
97 	return m_data->m_physicsClient->getNumBodies();
98 }
99 
getBodyUniqueId(int serialIndex) const100 int PhysicsLoopBack::getBodyUniqueId(int serialIndex) const
101 {
102 	return m_data->m_physicsClient->getBodyUniqueId(serialIndex);
103 }
104 
getBodyInfo(int bodyUniqueId,struct b3BodyInfo & info) const105 bool PhysicsLoopBack::getBodyInfo(int bodyUniqueId, struct b3BodyInfo& info) const
106 {
107 	return m_data->m_physicsClient->getBodyInfo(bodyUniqueId, info);
108 }
109 
getNumJoints(int bodyUniqueId) const110 int PhysicsLoopBack::getNumJoints(int bodyUniqueId) const
111 {
112 	return m_data->m_physicsClient->getNumJoints(bodyUniqueId);
113 }
114 
getNumDofs(int bodyUniqueId) const115 int PhysicsLoopBack::getNumDofs(int bodyUniqueId) const
116 {
117         return m_data->m_physicsClient->getNumDofs(bodyUniqueId);
118 }
119 
getJointInfo(int bodyIndex,int jointIndex,struct b3JointInfo & info) const120 bool PhysicsLoopBack::getJointInfo(int bodyIndex, int jointIndex, struct b3JointInfo& info) const
121 {
122 	return m_data->m_physicsClient->getJointInfo(bodyIndex, jointIndex, info);
123 }
124 
getNumUserConstraints() const125 int PhysicsLoopBack::getNumUserConstraints() const
126 {
127 	return m_data->m_physicsClient->getNumUserConstraints();
128 }
getUserConstraintInfo(int constraintUniqueId,struct b3UserConstraint & info) const129 int PhysicsLoopBack::getUserConstraintInfo(int constraintUniqueId, struct b3UserConstraint& info) const
130 {
131 	return m_data->m_physicsClient->getUserConstraintInfo(constraintUniqueId, info);
132 }
133 
getUserConstraintId(int serialIndex) const134 int PhysicsLoopBack::getUserConstraintId(int serialIndex) const
135 {
136 	return m_data->m_physicsClient->getUserConstraintId(serialIndex);
137 }
138 
139 ///todo: move this out of the interface
setSharedMemoryKey(int key)140 void PhysicsLoopBack::setSharedMemoryKey(int key)
141 {
142 	m_data->m_physicsServer->setSharedMemoryKey(key);
143 	m_data->m_physicsClient->setSharedMemoryKey(key);
144 }
145 
uploadBulletFileToSharedMemory(const char * data,int len)146 void PhysicsLoopBack::uploadBulletFileToSharedMemory(const char* data, int len)
147 {
148 	m_data->m_physicsClient->uploadBulletFileToSharedMemory(data, len);
149 }
150 
uploadRaysToSharedMemory(struct SharedMemoryCommand & command,const double * rayFromWorldArray,const double * rayToWorldArray,int numRays)151 void PhysicsLoopBack::uploadRaysToSharedMemory(struct SharedMemoryCommand& command, const double* rayFromWorldArray, const double* rayToWorldArray, int numRays)
152 {
153 	m_data->m_physicsClient->uploadRaysToSharedMemory(command, rayFromWorldArray, rayToWorldArray, numRays);
154 }
155 
getNumDebugLines() const156 int PhysicsLoopBack::getNumDebugLines() const
157 {
158 	return m_data->m_physicsClient->getNumDebugLines();
159 }
160 
getDebugLinesFrom() const161 const float* PhysicsLoopBack::getDebugLinesFrom() const
162 {
163 	return m_data->m_physicsClient->getDebugLinesFrom();
164 }
165 
getDebugLinesTo() const166 const float* PhysicsLoopBack::getDebugLinesTo() const
167 {
168 	return m_data->m_physicsClient->getDebugLinesTo();
169 }
170 
getDebugLinesColor() const171 const float* PhysicsLoopBack::getDebugLinesColor() const
172 {
173 	return m_data->m_physicsClient->getDebugLinesColor();
174 }
175 
getCachedCameraImage(struct b3CameraImageData * cameraData)176 void PhysicsLoopBack::getCachedCameraImage(struct b3CameraImageData* cameraData)
177 {
178 	return m_data->m_physicsClient->getCachedCameraImage(cameraData);
179 }
180 
getCachedMeshData(struct b3MeshData * meshData)181 void PhysicsLoopBack::getCachedMeshData(struct b3MeshData* meshData)
182 {
183 	return m_data->m_physicsClient->getCachedMeshData(meshData);
184 }
185 
getCachedContactPointInformation(struct b3ContactInformation * contactPointData)186 void PhysicsLoopBack::getCachedContactPointInformation(struct b3ContactInformation* contactPointData)
187 {
188 	return m_data->m_physicsClient->getCachedContactPointInformation(contactPointData);
189 }
190 
getCachedVisualShapeInformation(struct b3VisualShapeInformation * visualShapesInfo)191 void PhysicsLoopBack::getCachedVisualShapeInformation(struct b3VisualShapeInformation* visualShapesInfo)
192 {
193 	return m_data->m_physicsClient->getCachedVisualShapeInformation(visualShapesInfo);
194 }
195 
getCachedCollisionShapeInformation(struct b3CollisionShapeInformation * collisionShapesInfo)196 void PhysicsLoopBack::getCachedCollisionShapeInformation(struct b3CollisionShapeInformation* collisionShapesInfo)
197 {
198 	return m_data->m_physicsClient->getCachedCollisionShapeInformation(collisionShapesInfo);
199 }
200 
getCachedVREvents(struct b3VREventsData * vrEventsData)201 void PhysicsLoopBack::getCachedVREvents(struct b3VREventsData* vrEventsData)
202 {
203 	return m_data->m_physicsClient->getCachedVREvents(vrEventsData);
204 }
205 
getCachedKeyboardEvents(struct b3KeyboardEventsData * keyboardEventsData)206 void PhysicsLoopBack::getCachedKeyboardEvents(struct b3KeyboardEventsData* keyboardEventsData)
207 {
208 	return m_data->m_physicsClient->getCachedKeyboardEvents(keyboardEventsData);
209 }
210 
getCachedMouseEvents(struct b3MouseEventsData * mouseEventsData)211 void PhysicsLoopBack::getCachedMouseEvents(struct b3MouseEventsData* mouseEventsData)
212 {
213 	return m_data->m_physicsClient->getCachedMouseEvents(mouseEventsData);
214 }
215 
getCachedOverlappingObjects(struct b3AABBOverlapData * overlappingObjects)216 void PhysicsLoopBack::getCachedOverlappingObjects(struct b3AABBOverlapData* overlappingObjects)
217 {
218 	return m_data->m_physicsClient->getCachedOverlappingObjects(overlappingObjects);
219 }
220 
getCachedRaycastHits(struct b3RaycastInformation * raycastHits)221 void PhysicsLoopBack::getCachedRaycastHits(struct b3RaycastInformation* raycastHits)
222 {
223 	return m_data->m_physicsClient->getCachedRaycastHits(raycastHits);
224 }
225 
getCachedMassMatrix(int dofCountCheck,double * massMatrix)226 void PhysicsLoopBack::getCachedMassMatrix(int dofCountCheck, double* massMatrix)
227 {
228 	m_data->m_physicsClient->getCachedMassMatrix(dofCountCheck, massMatrix);
229 }
getCachedReturnData(struct b3UserDataValue * returnData)230 bool PhysicsLoopBack::getCachedReturnData(struct b3UserDataValue* returnData)
231 {
232 	return m_data->m_physicsClient->getCachedReturnData(returnData);
233 }
234 
setTimeOut(double timeOutInSeconds)235 void PhysicsLoopBack::setTimeOut(double timeOutInSeconds)
236 {
237 	m_data->m_physicsClient->setTimeOut(timeOutInSeconds);
238 }
getTimeOut() const239 double PhysicsLoopBack::getTimeOut() const
240 {
241 	return m_data->m_physicsClient->getTimeOut();
242 }
243 
getCachedUserData(int userDataId,struct b3UserDataValue & valueOut) const244 bool PhysicsLoopBack::getCachedUserData(int userDataId, struct b3UserDataValue& valueOut) const
245 {
246 	return m_data->m_physicsClient->getCachedUserData(userDataId, valueOut);
247 }
248 
getCachedUserDataId(int bodyUniqueId,int linkIndex,int visualShapeIndex,const char * key) const249 int PhysicsLoopBack::getCachedUserDataId(int bodyUniqueId, int linkIndex, int visualShapeIndex, const char* key) const
250 {
251 	return m_data->m_physicsClient->getCachedUserDataId(bodyUniqueId, linkIndex, visualShapeIndex, key);
252 }
253 
getNumUserData(int bodyUniqueId) const254 int PhysicsLoopBack::getNumUserData(int bodyUniqueId) const
255 {
256 	return m_data->m_physicsClient->getNumUserData(bodyUniqueId);
257 }
258 
getUserDataInfo(int bodyUniqueId,int userDataIndex,const char ** keyOut,int * userDataIdOut,int * linkIndexOut,int * visualShapeIndexOut) const259 void PhysicsLoopBack::getUserDataInfo(int bodyUniqueId, int userDataIndex, const char** keyOut, int* userDataIdOut, int* linkIndexOut, int* visualShapeIndexOut) const
260 {
261 	m_data->m_physicsClient->getUserDataInfo(bodyUniqueId, userDataIndex, keyOut, userDataIdOut, linkIndexOut, visualShapeIndexOut);
262 }
263 
pushProfileTiming(const char * timingName)264 void PhysicsLoopBack::pushProfileTiming(const char* timingName)
265 {
266 	m_data->m_physicsClient->pushProfileTiming(timingName);
267 }
popProfileTiming()268 void PhysicsLoopBack::popProfileTiming()
269 {
270 	m_data->m_physicsClient->popProfileTiming();
271 }
272