PhysicsLoopBack.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. PhysicsLoopBackInternalData()
  14. : m_commandProcessor(0),
  15. m_physicsClient(0),
  16. m_physicsServer(0)
  17. {
  18. }
  19. };
  20. struct Bullet2CommandProcessorCreation2 : public CommandProcessorCreationInterface
  21. {
  22. virtual class CommandProcessorInterface* createCommandProcessor()
  23. {
  24. PhysicsServerCommandProcessor* proc = new PhysicsServerCommandProcessor;
  25. return proc;
  26. }
  27. virtual void deleteCommandProcessor(CommandProcessorInterface* proc)
  28. {
  29. delete proc;
  30. }
  31. };
  32. static Bullet2CommandProcessorCreation2 sB2Proc;
  33. PhysicsLoopBack::PhysicsLoopBack()
  34. {
  35. m_data = new PhysicsLoopBackInternalData;
  36. m_data->m_physicsServer = new PhysicsServerSharedMemory(&sB2Proc, 0, 0);
  37. m_data->m_physicsClient = new PhysicsClientSharedMemory();
  38. }
  39. PhysicsLoopBack::~PhysicsLoopBack()
  40. {
  41. delete m_data->m_physicsClient;
  42. delete m_data->m_physicsServer;
  43. delete m_data->m_commandProcessor;
  44. delete m_data;
  45. }
  46. // return true if connection succesfull, can also check 'isConnected'
  47. bool PhysicsLoopBack::connect()
  48. {
  49. m_data->m_physicsServer->connectSharedMemory(&m_data->m_noGfx);
  50. m_data->m_physicsClient->connect();
  51. return m_data->m_physicsClient->isConnected();
  52. }
  53. ////todo: rename to 'disconnect'
  54. void PhysicsLoopBack::disconnectSharedMemory()
  55. {
  56. m_data->m_physicsClient->disconnectSharedMemory();
  57. m_data->m_physicsServer->disconnectSharedMemory(true);
  58. }
  59. bool PhysicsLoopBack::isConnected() const
  60. {
  61. return m_data->m_physicsClient->isConnected();
  62. }
  63. // return non-null if there is a status, nullptr otherwise
  64. const SharedMemoryStatus* PhysicsLoopBack::processServerStatus()
  65. {
  66. m_data->m_physicsServer->processClientCommands();
  67. return m_data->m_physicsClient->processServerStatus();
  68. }
  69. SharedMemoryCommand* PhysicsLoopBack::getAvailableSharedMemoryCommand()
  70. {
  71. return m_data->m_physicsClient->getAvailableSharedMemoryCommand();
  72. }
  73. bool PhysicsLoopBack::canSubmitCommand() const
  74. {
  75. return m_data->m_physicsClient->canSubmitCommand();
  76. }
  77. bool PhysicsLoopBack::submitClientCommand(const struct SharedMemoryCommand& command)
  78. {
  79. return m_data->m_physicsClient->submitClientCommand(command);
  80. }
  81. int PhysicsLoopBack::getNumBodies() const
  82. {
  83. return m_data->m_physicsClient->getNumBodies();
  84. }
  85. int PhysicsLoopBack::getBodyUniqueId(int serialIndex) const
  86. {
  87. return m_data->m_physicsClient->getBodyUniqueId(serialIndex);
  88. }
  89. bool PhysicsLoopBack::getBodyInfo(int bodyUniqueId, struct b3BodyInfo& info) const
  90. {
  91. return m_data->m_physicsClient->getBodyInfo(bodyUniqueId, info);
  92. }
  93. int PhysicsLoopBack::getNumJoints(int bodyUniqueId) const
  94. {
  95. return m_data->m_physicsClient->getNumJoints(bodyUniqueId);
  96. }
  97. int PhysicsLoopBack::getNumDofs(int bodyUniqueId) const
  98. {
  99. return m_data->m_physicsClient->getNumDofs(bodyUniqueId);
  100. }
  101. bool PhysicsLoopBack::getJointInfo(int bodyIndex, int jointIndex, struct b3JointInfo& info) const
  102. {
  103. return m_data->m_physicsClient->getJointInfo(bodyIndex, jointIndex, info);
  104. }
  105. int PhysicsLoopBack::getNumUserConstraints() const
  106. {
  107. return m_data->m_physicsClient->getNumUserConstraints();
  108. }
  109. int PhysicsLoopBack::getUserConstraintInfo(int constraintUniqueId, struct b3UserConstraint& info) const
  110. {
  111. return m_data->m_physicsClient->getUserConstraintInfo(constraintUniqueId, info);
  112. }
  113. int PhysicsLoopBack::getUserConstraintId(int serialIndex) const
  114. {
  115. return m_data->m_physicsClient->getUserConstraintId(serialIndex);
  116. }
  117. ///todo: move this out of the interface
  118. void PhysicsLoopBack::setSharedMemoryKey(int key)
  119. {
  120. m_data->m_physicsServer->setSharedMemoryKey(key);
  121. m_data->m_physicsClient->setSharedMemoryKey(key);
  122. }
  123. void PhysicsLoopBack::uploadBulletFileToSharedMemory(const char* data, int len)
  124. {
  125. m_data->m_physicsClient->uploadBulletFileToSharedMemory(data, len);
  126. }
  127. void PhysicsLoopBack::uploadRaysToSharedMemory(struct SharedMemoryCommand& command, const double* rayFromWorldArray, const double* rayToWorldArray, int numRays)
  128. {
  129. m_data->m_physicsClient->uploadRaysToSharedMemory(command, rayFromWorldArray, rayToWorldArray, numRays);
  130. }
  131. int PhysicsLoopBack::getNumDebugLines() const
  132. {
  133. return m_data->m_physicsClient->getNumDebugLines();
  134. }
  135. const float* PhysicsLoopBack::getDebugLinesFrom() const
  136. {
  137. return m_data->m_physicsClient->getDebugLinesFrom();
  138. }
  139. const float* PhysicsLoopBack::getDebugLinesTo() const
  140. {
  141. return m_data->m_physicsClient->getDebugLinesTo();
  142. }
  143. const float* PhysicsLoopBack::getDebugLinesColor() const
  144. {
  145. return m_data->m_physicsClient->getDebugLinesColor();
  146. }
  147. void PhysicsLoopBack::getCachedCameraImage(struct b3CameraImageData* cameraData)
  148. {
  149. return m_data->m_physicsClient->getCachedCameraImage(cameraData);
  150. }
  151. void PhysicsLoopBack::getCachedMeshData(struct b3MeshData* meshData)
  152. {
  153. return m_data->m_physicsClient->getCachedMeshData(meshData);
  154. }
  155. void PhysicsLoopBack::getCachedContactPointInformation(struct b3ContactInformation* contactPointData)
  156. {
  157. return m_data->m_physicsClient->getCachedContactPointInformation(contactPointData);
  158. }
  159. void PhysicsLoopBack::getCachedVisualShapeInformation(struct b3VisualShapeInformation* visualShapesInfo)
  160. {
  161. return m_data->m_physicsClient->getCachedVisualShapeInformation(visualShapesInfo);
  162. }
  163. void PhysicsLoopBack::getCachedCollisionShapeInformation(struct b3CollisionShapeInformation* collisionShapesInfo)
  164. {
  165. return m_data->m_physicsClient->getCachedCollisionShapeInformation(collisionShapesInfo);
  166. }
  167. void PhysicsLoopBack::getCachedVREvents(struct b3VREventsData* vrEventsData)
  168. {
  169. return m_data->m_physicsClient->getCachedVREvents(vrEventsData);
  170. }
  171. void PhysicsLoopBack::getCachedKeyboardEvents(struct b3KeyboardEventsData* keyboardEventsData)
  172. {
  173. return m_data->m_physicsClient->getCachedKeyboardEvents(keyboardEventsData);
  174. }
  175. void PhysicsLoopBack::getCachedMouseEvents(struct b3MouseEventsData* mouseEventsData)
  176. {
  177. return m_data->m_physicsClient->getCachedMouseEvents(mouseEventsData);
  178. }
  179. void PhysicsLoopBack::getCachedOverlappingObjects(struct b3AABBOverlapData* overlappingObjects)
  180. {
  181. return m_data->m_physicsClient->getCachedOverlappingObjects(overlappingObjects);
  182. }
  183. void PhysicsLoopBack::getCachedRaycastHits(struct b3RaycastInformation* raycastHits)
  184. {
  185. return m_data->m_physicsClient->getCachedRaycastHits(raycastHits);
  186. }
  187. void PhysicsLoopBack::getCachedMassMatrix(int dofCountCheck, double* massMatrix)
  188. {
  189. m_data->m_physicsClient->getCachedMassMatrix(dofCountCheck, massMatrix);
  190. }
  191. bool PhysicsLoopBack::getCachedReturnData(struct b3UserDataValue* returnData)
  192. {
  193. return m_data->m_physicsClient->getCachedReturnData(returnData);
  194. }
  195. void PhysicsLoopBack::setTimeOut(double timeOutInSeconds)
  196. {
  197. m_data->m_physicsClient->setTimeOut(timeOutInSeconds);
  198. }
  199. double PhysicsLoopBack::getTimeOut() const
  200. {
  201. return m_data->m_physicsClient->getTimeOut();
  202. }
  203. bool PhysicsLoopBack::getCachedUserData(int userDataId, struct b3UserDataValue& valueOut) const
  204. {
  205. return m_data->m_physicsClient->getCachedUserData(userDataId, valueOut);
  206. }
  207. int PhysicsLoopBack::getCachedUserDataId(int bodyUniqueId, int linkIndex, int visualShapeIndex, const char* key) const
  208. {
  209. return m_data->m_physicsClient->getCachedUserDataId(bodyUniqueId, linkIndex, visualShapeIndex, key);
  210. }
  211. int PhysicsLoopBack::getNumUserData(int bodyUniqueId) const
  212. {
  213. return m_data->m_physicsClient->getNumUserData(bodyUniqueId);
  214. }
  215. void PhysicsLoopBack::getUserDataInfo(int bodyUniqueId, int userDataIndex, const char** keyOut, int* userDataIdOut, int* linkIndexOut, int* visualShapeIndexOut) const
  216. {
  217. m_data->m_physicsClient->getUserDataInfo(bodyUniqueId, userDataIndex, keyOut, userDataIdOut, linkIndexOut, visualShapeIndexOut);
  218. }
  219. void PhysicsLoopBack::pushProfileTiming(const char* timingName)
  220. {
  221. m_data->m_physicsClient->pushProfileTiming(timingName);
  222. }
  223. void PhysicsLoopBack::popProfileTiming()
  224. {
  225. m_data->m_physicsClient->popProfileTiming();
  226. }