PhysicsServerExample.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. #include "PhysicsServerExample.h"
  2. #include "PhysicsServerSharedMemory.h"
  3. #include "SharedMemoryCommon.h"
  4. class PhysicsServerExample : public SharedMemoryCommon
  5. {
  6. PhysicsServerSharedMemory m_physicsServer;
  7. bool m_wantsShutdown;
  8. bool m_isConnected;
  9. btClock m_clock;
  10. bool m_replay;
  11. public:
  12. PhysicsServerExample(GUIHelperInterface* helper);
  13. virtual ~PhysicsServerExample();
  14. virtual void initPhysics();
  15. virtual void stepSimulation(float deltaTime);
  16. void enableCommandLogging()
  17. {
  18. m_physicsServer.enableCommandLogging(true,"BulletPhysicsCommandLog.bin");
  19. }
  20. void replayFromLogFile()
  21. {
  22. m_replay = true;
  23. m_physicsServer.replayFromLogFile("BulletPhysicsCommandLog.bin");
  24. }
  25. virtual void resetCamera()
  26. {
  27. float dist = 5;
  28. float pitch = 50;
  29. float yaw = 35;
  30. float targetPos[3]={0,0,0};//-3,2.8,-2.5};
  31. m_guiHelper->resetCamera(dist,pitch,yaw,targetPos[0],targetPos[1],targetPos[2]);
  32. }
  33. virtual bool wantsTermination();
  34. virtual bool isConnected();
  35. virtual void renderScene();
  36. virtual void exitPhysics(){}
  37. virtual void physicsDebugDraw(int debugFlags);
  38. btVector3 getRayTo(int x,int y);
  39. virtual bool mouseMoveCallback(float x,float y)
  40. {
  41. if (m_replay)
  42. return false;
  43. CommonRenderInterface* renderer = m_guiHelper->getRenderInterface();
  44. if (!renderer)
  45. {
  46. btAssert(0);
  47. return false;
  48. }
  49. btVector3 rayTo = getRayTo(int(x), int(y));
  50. btVector3 rayFrom;
  51. renderer->getActiveCamera()->getCameraPosition(rayFrom);
  52. m_physicsServer.movePickedBody(rayFrom,rayTo);
  53. return false;
  54. };
  55. virtual bool mouseButtonCallback(int button, int state, float x, float y)
  56. {
  57. if (m_replay)
  58. return false;
  59. CommonRenderInterface* renderer = m_guiHelper->getRenderInterface();
  60. if (!renderer)
  61. {
  62. btAssert(0);
  63. return false;
  64. }
  65. CommonWindowInterface* window = m_guiHelper->getAppInterface()->m_window;
  66. if (state==1)
  67. {
  68. if(button==0 && (!window->isModifierKeyPressed(B3G_ALT) && !window->isModifierKeyPressed(B3G_CONTROL) ))
  69. {
  70. btVector3 camPos;
  71. renderer->getActiveCamera()->getCameraPosition(camPos);
  72. btVector3 rayFrom = camPos;
  73. btVector3 rayTo = getRayTo(int(x),int(y));
  74. m_physicsServer.pickBody(rayFrom, rayTo);
  75. }
  76. } else
  77. {
  78. if (button==0)
  79. {
  80. m_physicsServer.removePickingConstraint();
  81. //remove p2p
  82. }
  83. }
  84. //printf("button=%d, state=%d\n",button,state);
  85. return false;
  86. }
  87. virtual bool keyboardCallback(int key, int state){return false;}
  88. virtual void setSharedMemoryKey(int key)
  89. {
  90. m_physicsServer.setSharedMemoryKey(key);
  91. }
  92. };
  93. PhysicsServerExample::PhysicsServerExample(GUIHelperInterface* helper)
  94. :SharedMemoryCommon(helper),
  95. m_wantsShutdown(false),
  96. m_isConnected(false),
  97. m_replay(false)
  98. {
  99. b3Printf("Started PhysicsServer\n");
  100. }
  101. PhysicsServerExample::~PhysicsServerExample()
  102. {
  103. bool deInitializeSharedMemory = true;
  104. m_physicsServer.disconnectSharedMemory(deInitializeSharedMemory);
  105. m_isConnected = false;
  106. }
  107. bool PhysicsServerExample::isConnected()
  108. {
  109. return m_isConnected;
  110. }
  111. void PhysicsServerExample::initPhysics()
  112. {
  113. ///for this testing we use Z-axis up
  114. int upAxis = 2;
  115. m_guiHelper->setUpAxis(upAxis);
  116. #if 0
  117. createEmptyDynamicsWorld();
  118. //todo: create a special debug drawer that will cache the lines, so we can send the debug info over the wire
  119. btVector3 grav(0,0,0);
  120. grav[upAxis] = 0;//-9.8;
  121. this->m_dynamicsWorld->setGravity(grav);
  122. #endif
  123. m_isConnected = m_physicsServer.connectSharedMemory( m_guiHelper);
  124. }
  125. bool PhysicsServerExample::wantsTermination()
  126. {
  127. return m_wantsShutdown;
  128. }
  129. void PhysicsServerExample::stepSimulation(float deltaTime)
  130. {
  131. if (m_replay)
  132. {
  133. for (int i=0;i<100;i++)
  134. m_physicsServer.processClientCommands();
  135. } else
  136. {
  137. btClock rtc;
  138. btScalar endTime = rtc.getTimeMilliseconds() + deltaTime*btScalar(800);
  139. while (rtc.getTimeMilliseconds()<endTime)
  140. {
  141. m_physicsServer.processClientCommands();
  142. }
  143. }
  144. }
  145. void PhysicsServerExample::renderScene()
  146. {
  147. ///debug rendering
  148. m_physicsServer.renderScene();
  149. }
  150. void PhysicsServerExample::physicsDebugDraw(int debugDrawFlags)
  151. {
  152. ///debug rendering
  153. m_physicsServer.physicsDebugDraw(debugDrawFlags);
  154. }
  155. btVector3 PhysicsServerExample::getRayTo(int x,int y)
  156. {
  157. CommonRenderInterface* renderer = m_guiHelper->getRenderInterface();
  158. if (!renderer)
  159. {
  160. btAssert(0);
  161. return btVector3(0,0,0);
  162. }
  163. float top = 1.f;
  164. float bottom = -1.f;
  165. float nearPlane = 1.f;
  166. float tanFov = (top-bottom)*0.5f / nearPlane;
  167. float fov = btScalar(2.0) * btAtan(tanFov);
  168. btVector3 camPos,camTarget;
  169. renderer->getActiveCamera()->getCameraPosition(camPos);
  170. renderer->getActiveCamera()->getCameraTargetPosition(camTarget);
  171. btVector3 rayFrom = camPos;
  172. btVector3 rayForward = (camTarget-camPos);
  173. rayForward.normalize();
  174. float farPlane = 10000.f;
  175. rayForward*= farPlane;
  176. btVector3 rightOffset;
  177. btVector3 cameraUp=btVector3(0,0,0);
  178. cameraUp[m_guiHelper->getAppInterface()->getUpAxis()]=1;
  179. btVector3 vertical = cameraUp;
  180. btVector3 hor;
  181. hor = rayForward.cross(vertical);
  182. hor.normalize();
  183. vertical = hor.cross(rayForward);
  184. vertical.normalize();
  185. float tanfov = tanf(0.5f*fov);
  186. hor *= 2.f * farPlane * tanfov;
  187. vertical *= 2.f * farPlane * tanfov;
  188. btScalar aspect;
  189. float width = float(renderer->getScreenWidth());
  190. float height = float (renderer->getScreenHeight());
  191. aspect = width / height;
  192. hor*=aspect;
  193. btVector3 rayToCenter = rayFrom + rayForward;
  194. btVector3 dHor = hor * 1.f/width;
  195. btVector3 dVert = vertical * 1.f/height;
  196. btVector3 rayTo = rayToCenter - 0.5f * hor + 0.5f * vertical;
  197. rayTo += btScalar(x) * dHor;
  198. rayTo -= btScalar(y) * dVert;
  199. return rayTo;
  200. }
  201. extern int gSharedMemoryKey;
  202. class CommonExampleInterface* PhysicsServerCreateFunc(struct CommonExampleOptions& options)
  203. {
  204. PhysicsServerExample* example = new PhysicsServerExample(options.m_guiHelper);
  205. if (gSharedMemoryKey>=0)
  206. {
  207. example->setSharedMemoryKey(gSharedMemoryKey);
  208. }
  209. if (options.m_option & PHYSICS_SERVER_ENABLE_COMMAND_LOGGING)
  210. {
  211. example->enableCommandLogging();
  212. }
  213. if (options.m_option & PHYSICS_SERVER_REPLAY_FROM_COMMAND_LOG)
  214. {
  215. example->replayFromLogFile();
  216. }
  217. return example;
  218. }