25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 

384 satır
10 KiB

  1. //
  2. // BtPhysTest
  3. //
  4. // Copyright: (c) 2009-2012 Benjamin Huet <huet.benjamin@gmail.com>
  5. // (c) 2012 Sam Hocevar <sam@hocevar.net>
  6. //
  7. #if defined HAVE_CONFIG_H
  8. # include "config.h"
  9. #endif
  10. #if defined _WIN32
  11. # include <direct.h>
  12. #endif
  13. #if defined _XBOX
  14. # define _USE_MATH_DEFINES /* for M_PI */
  15. # include <xtl.h>
  16. # undef near /* Fuck Microsoft */
  17. # undef far /* Fuck Microsoft again */
  18. #elif defined _WIN32
  19. # define _USE_MATH_DEFINES /* for M_PI */
  20. # define WIN32_LEAN_AND_MEAN
  21. # include <windows.h>
  22. # undef near /* Fuck Microsoft */
  23. # undef far /* Fuck Microsoft again */
  24. #else
  25. # include <cmath>
  26. #endif
  27. #if USE_SDL && defined __APPLE__
  28. # include <SDL_main.h>
  29. #endif
  30. #include <bullet/btBulletDynamicsCommon.h>
  31. #include <bullet/btBulletCollisionCommon.h>
  32. #include "core.h"
  33. #include "loldebug.h"
  34. using namespace lol;
  35. #ifndef HAVE_PHYS_USE_BULLET
  36. #define HAVE_PHYS_USE_BULLET
  37. #endif /* HAVE_PHYS_USE_BULLET */
  38. #include "Physics/LolPhysics.h"
  39. #include "Physics/EasyPhysics.h"
  40. #include "PhysicObject.h"
  41. #include "BtPhysTest.h"
  42. using namespace lol::phys;
  43. #define CUBE_HALF_EXTENTS .5f
  44. #define EXTRA_HEIGHT 1.f
  45. int gNumObjects = 64;
  46. BtPhysTest::BtPhysTest(bool editor)
  47. {
  48. /* Create a camera that matches the settings of XNA BtPhysTest */
  49. m_camera = new Camera(vec3(0.f, 600.f, 0.f),
  50. vec3(0.f, 0.f, 0.f),
  51. vec3(0, 1, 0));
  52. m_camera->SetRotation(quat::fromeuler_xyz(0.f, 0.f, 0.f));
  53. m_camera->SetPerspective(90.f, 1280.f, 960.f, .1f, 1000.f);
  54. //m_camera->SetOrtho(1280.f / 6, 960.f / 6, -1000.f, 1000.f);
  55. Ticker::Ref(m_camera);
  56. m_ready = false;
  57. m_simulation = new Simulation();
  58. m_simulation->InitContext();
  59. vec3 NewGravity = vec3(.0f, -10.0f, .0f);
  60. m_simulation->SetGravity(NewGravity);
  61. m_ground_object = new PhysicsObject(m_simulation);
  62. Ticker::Ref(m_ground_object);
  63. for (int x=0; x < 10; x++)
  64. {
  65. for (int y=0; y < 10; y++)
  66. {
  67. PhysicsObject* new_physobj = new PhysicsObject(m_simulation, 10.f, vec3(0.f, 20.f, -20.0f) + vec3(.0f, 4.f * (float)y, 4.f * (float)x));
  68. m_physobj_list << new_physobj;
  69. Ticker::Ref(new_physobj);
  70. }
  71. }
  72. #if 0
  73. //init Physics
  74. {
  75. m_bt_ccd_mode = USE_CCD;
  76. //collision configuration contains default setup for memory, collision setup
  77. m_bt_collision_config = new btDefaultCollisionConfiguration();
  78. //use the default collision dispatcher. For parallel processing you can use a diffent dispatcher (see Extras/BulletMultiThreaded)
  79. m_bt_dispatcher = new btCollisionDispatcher(m_bt_collision_config);
  80. m_bt_dispatcher->registerCollisionCreateFunc(BOX_SHAPE_PROXYTYPE,
  81. BOX_SHAPE_PROXYTYPE,
  82. m_bt_collision_config->getCollisionAlgorithmCreateFunc(CONVEX_SHAPE_PROXYTYPE,
  83. CONVEX_SHAPE_PROXYTYPE));
  84. m_bt_broadphase = new btDbvtBroadphase();
  85. ///the default constraint solver. For parallel processing you can use a different solver (see Extras/BulletMultiThreaded)
  86. m_bt_solver = new btSequentialImpulseConstraintSolver;
  87. m_bt_world = new btDiscreteDynamicsWorld(m_bt_dispatcher, m_bt_broadphase, m_bt_solver, m_bt_collision_config);
  88. //m_bt_world->setDebugDrawer(&sDebugDrawer);
  89. m_bt_world->getSolverInfo().m_splitImpulse = true;
  90. m_bt_world->getSolverInfo().m_numIterations = 20;
  91. m_bt_world->getDispatchInfo().m_useContinuous = (m_bt_ccd_mode == USE_CCD);
  92. m_bt_world->setGravity(btVector3(0,-10,0));
  93. ///create a few basic rigid bodies
  94. btBoxShape* box = new btBoxShape(btVector3(btScalar(110.),btScalar(1.),btScalar(110.)));
  95. btCollisionShape* groundShape = box;
  96. m_bt_collision_shapes << groundShape;
  97. m_ground_mesh.Compile("[sc#ddd afcb220 2 220 -1]");
  98. //m_bt_collision_shapes << new btCylinderShape(btVector3(.5f,.5f,.5f));
  99. btTransform groundTransform;
  100. groundTransform.setIdentity();
  101. //We can also use DemoApplication::localCreateRigidBody, but for clarity it is provided here:
  102. {
  103. btScalar mass(0.);
  104. //rigidbody is dynamic if and only if mass is non zero, otherwise static
  105. bool isDynamic = (mass != 0.f);
  106. btVector3 localInertia(0,0,0);
  107. if (isDynamic)
  108. groundShape->calculateLocalInertia(mass,localInertia);
  109. //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
  110. btDefaultMotionState* myMotionState = new btDefaultMotionState(groundTransform);
  111. btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,groundShape,localInertia);
  112. btRigidBody* body = new btRigidBody(rbInfo);
  113. //add the body to the dynamics world
  114. m_bt_world->addRigidBody(body);
  115. }
  116. //Adding Shapes
  117. {
  118. //create a few dynamic rigidbodies
  119. // Re-using the same collision is better for memory usage and performance
  120. btCollisionShape* colShape = new btBoxShape(btVector3(1,1,1));
  121. m_rigid_mesh[0].Compile("[sc#add afcb2 2 2 -.1]");
  122. m_rigid_mesh[1].Compile("[sc#dad afcb2 2 2 -.1]");
  123. m_rigid_mesh[2].Compile("[sc#dda afcb2 2 2 -.1]");
  124. m_rigid_mesh[3].Compile("[sc#daa afcb2 2 2 -.1]");
  125. m_rigid_mesh[4].Compile("[sc#ada afcb2 2 2 -.1]");
  126. m_rigid_mesh[5].Compile("[sc#aad afcb2 2 2 -.1]");
  127. m_bt_collision_shapes << colShape;
  128. m_bt_dynamic_shapes << colShape;
  129. /// Create Dynamic Objects
  130. btTransform startTransform;
  131. startTransform.setIdentity();
  132. btScalar mass(1.f);
  133. //rigidbody is dynamic if and only if mass is non zero, otherwise static
  134. bool isDynamic = (mass != 0.f);
  135. btVector3 localInertia(0,0,0);
  136. if (isDynamic)
  137. colShape->calculateLocalInertia(mass,localInertia);
  138. int i;
  139. for (i=0;i<gNumObjects;i++)
  140. {
  141. btCollisionShape* shape = colShape;
  142. btTransform trans;
  143. trans.setIdentity();
  144. //stack them
  145. int colsize = 10;
  146. int row = int(((float)i*CUBE_HALF_EXTENTS*2.0f)/((float)colsize*2.0f*CUBE_HALF_EXTENTS));
  147. int row2 = row;
  148. int col = (i)%(colsize)-colsize/2;
  149. if (col>3)
  150. {
  151. col=11;
  152. row2 |=1;
  153. }
  154. btVector3 pos(((row+col+row2) % 4)*CUBE_HALF_EXTENTS,
  155. 20.0f + row*8*CUBE_HALF_EXTENTS+CUBE_HALF_EXTENTS+EXTRA_HEIGHT,
  156. col*8*CUBE_HALF_EXTENTS + 2 * (row2%2)*CUBE_HALF_EXTENTS);
  157. trans.setOrigin(pos);
  158. float mass = 1.f;
  159. btAssert((!shape || shape->getShapeType() != INVALID_SHAPE_PROXYTYPE));
  160. //rigidbody is dynamic if and only if mass is non zero, otherwise static
  161. bool isDynamic = (mass != 0.f);
  162. btVector3 localInertia(0,0,0);
  163. if (isDynamic)
  164. shape->calculateLocalInertia(mass,localInertia);
  165. //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
  166. btDefaultMotionState* myMotionState = new btDefaultMotionState(trans);
  167. btRigidBody::btRigidBodyConstructionInfo cInfo(mass,myMotionState,shape,localInertia);
  168. btRigidBody* body = new btRigidBody(cInfo);
  169. body->setContactProcessingThreshold(BT_LARGE_FLOAT);
  170. m_bt_world->addRigidBody(body);
  171. ///when using m_ccdMode
  172. if (m_bt_ccd_mode == USE_CCD)
  173. {
  174. body->setCcdMotionThreshold(CUBE_HALF_EXTENTS);
  175. body->setCcdSweptSphereRadius(0.9*CUBE_HALF_EXTENTS);
  176. }
  177. }
  178. }
  179. }
  180. #endif
  181. }
  182. void BtPhysTest::TickGame(float seconds)
  183. {
  184. WorldEntity::TickGame(seconds);
  185. if (Input::GetButtonState(27 /*SDLK_ESCAPE*/))
  186. Ticker::Shutdown();
  187. m_simulation->TickContext(seconds);
  188. m_camera->SetTarget(vec3(.0f));
  189. m_camera->SetPosition(vec3(-30.0f, 10.0f, .0f));
  190. #if 0
  191. ///step the simulation
  192. if (m_bt_world)
  193. {
  194. //int steps = (int)(seconds / 0.005f);
  195. //for (int i = 0; i < steps; i++)
  196. m_bt_world->stepSimulation(seconds /*/ steps*/);
  197. //optional but useful: debug drawing
  198. //m_bt_world->debugDrawWorld();
  199. }
  200. #endif
  201. }
  202. void BtPhysTest::TickDraw(float seconds)
  203. {
  204. WorldEntity::TickDraw(seconds);
  205. if (!m_ready)
  206. {
  207. #if 0
  208. m_ground_mesh.MeshConvert();
  209. m_rigid_mesh[0].MeshConvert();
  210. m_rigid_mesh[1].MeshConvert();
  211. m_rigid_mesh[2].MeshConvert();
  212. m_rigid_mesh[3].MeshConvert();
  213. m_rigid_mesh[4].MeshConvert();
  214. m_rigid_mesh[5].MeshConvert();
  215. #endif
  216. /* FIXME: this object never cleans up */
  217. m_ready = true;
  218. }
  219. Video::SetClearColor(vec4(0.0f, 0.0f, 0.12f, 1.0f));
  220. #if 0
  221. vec3 BarycenterLocation = vec3(.0f);
  222. float BarycenterFactor = 0.0f;
  223. for(int i=0;i<gNumObjects;i++)
  224. {
  225. mat4 m(1.0f);
  226. btMatrix3x3 rot; rot.setIdentity();
  227. btCollisionObject* colObj = m_bt_world->getCollisionObjectArray()[i];
  228. btRigidBody* body = btRigidBody::upcast(colObj);
  229. if(body && body->getMotionState())
  230. {
  231. btDefaultMotionState* myMotionState = (btDefaultMotionState*)body->getMotionState();
  232. myMotionState->m_graphicsWorldTrans.getOpenGLMatrix(&m[0][0]);
  233. rot = myMotionState->m_graphicsWorldTrans.getBasis();
  234. }
  235. else
  236. {
  237. colObj->getWorldTransform().getOpenGLMatrix(&m[0][0]);
  238. rot = colObj->getWorldTransform().getBasis();
  239. }
  240. if (i > 0)
  241. {
  242. BarycenterLocation += m.v3.xyz;
  243. BarycenterFactor += 1.0f;
  244. }
  245. if (i == 0)
  246. m_ground_mesh.Render(m);
  247. else
  248. m_rigid_mesh[i % 6].Render(m);
  249. }
  250. if (BarycenterFactor > .0f)
  251. {
  252. BarycenterLocation /= BarycenterFactor;
  253. m_camera->SetTarget(BarycenterLocation);
  254. m_camera->SetPosition(BarycenterLocation + vec3(-20.0f, 8.0f, .0f));
  255. }
  256. #endif
  257. }
  258. BtPhysTest::~BtPhysTest()
  259. {
  260. Ticker::Unref(m_camera);
  261. Ticker::Unref(m_ground_object);
  262. while (m_physobj_list.Count())
  263. {
  264. PhysicsObject* CurPop = m_physobj_list.Last();
  265. m_physobj_list.Pop();
  266. Ticker::Unref(CurPop);
  267. }
  268. #if 0
  269. //Exit Physics
  270. {
  271. //cleanup in the reverse order of creation/initialization
  272. //remove the rigidbodies from the dynamics world and delete them
  273. for (int i = m_bt_world->getNumCollisionObjects() - 1; i >= 0 ;i--)
  274. {
  275. btCollisionObject* obj = m_bt_world->getCollisionObjectArray()[i];
  276. btRigidBody* body = btRigidBody::upcast(obj);
  277. if (body && body->getMotionState())
  278. delete body->getMotionState();
  279. m_bt_world->removeCollisionObject(obj);
  280. delete obj;
  281. }
  282. //delete collision shapes
  283. for (int j = 0; j < m_bt_collision_shapes.Count(); j++)
  284. {
  285. btCollisionShape* shape = m_bt_collision_shapes[j];
  286. delete shape;
  287. }
  288. m_bt_collision_shapes.Empty();
  289. delete m_bt_world;
  290. delete m_bt_solver;
  291. delete m_bt_broadphase;
  292. delete m_bt_dispatcher;
  293. delete m_bt_collision_config;
  294. }
  295. #endif
  296. }
  297. int main(int argc, char **argv)
  298. {
  299. Application app("BtPhysTest", ivec2(1280, 720), 60.0f);
  300. #if defined _MSC_VER && !defined _XBOX
  301. _chdir("..");
  302. #elif defined _WIN32 && !defined _XBOX
  303. _chdir("../..");
  304. #endif
  305. new BtPhysTest(argc > 1);
  306. app.ShowPointer(false);
  307. app.Run();
  308. return EXIT_SUCCESS;
  309. }