You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

преди 12 години
преди 12 години
преди 12 години
преди 12 години
преди 12 години
преди 12 години
преди 12 години
преди 12 години
преди 12 години
преди 12 години
преди 12 години
преди 12 години
преди 12 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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. float offset = 30.f;
  62. vec3 pos_offset = vec3(.0f, 30.f, .0f);
  63. for (int i=0; i < 6; i++)
  64. {
  65. int idx = i/2;
  66. vec3 NewPosition = pos_offset;
  67. NewPosition[idx] += offset;
  68. offset *= -1.f;
  69. quat NewRotation = quat(mat4(1.f));
  70. PhysicsObject* NewPhyobj = new PhysicsObject(m_simulation, NewPosition, NewRotation);
  71. if (idx != 1)
  72. {
  73. vec3 axis = vec3(.0f);
  74. axis[idx] = 1;
  75. NewRotation = quat::rotate(90.f, axis);
  76. }
  77. NewPhyobj->SetTransform(NewPosition, NewRotation);
  78. Ticker::Ref(NewPhyobj);
  79. m_ground_list << NewPhyobj;
  80. }
  81. for (int x=0; x < 5; x++)
  82. {
  83. for (int y=0; y < 5; y++)
  84. {
  85. for (int z=0; z < 4; z++)
  86. {
  87. PhysicsObject* new_physobj = new PhysicsObject(m_simulation, 10.f,
  88. vec3(-20.f, 40.f, -20.f) +
  89. vec3(4.f * (float)x, 4.f * (float)y, 4.f * (float)z));
  90. m_physobj_list << new_physobj;
  91. Ticker::Ref(new_physobj);
  92. }
  93. }
  94. }
  95. #if 0
  96. //init Physics
  97. {
  98. m_bt_ccd_mode = USE_CCD;
  99. //collision configuration contains default setup for memory, collision setup
  100. m_bt_collision_config = new btDefaultCollisionConfiguration();
  101. //use the default collision dispatcher. For parallel processing you can use a diffent dispatcher (see Extras/BulletMultiThreaded)
  102. m_bt_dispatcher = new btCollisionDispatcher(m_bt_collision_config);
  103. m_bt_dispatcher->registerCollisionCreateFunc(BOX_SHAPE_PROXYTYPE,
  104. BOX_SHAPE_PROXYTYPE,
  105. m_bt_collision_config->getCollisionAlgorithmCreateFunc(CONVEX_SHAPE_PROXYTYPE,
  106. CONVEX_SHAPE_PROXYTYPE));
  107. m_bt_broadphase = new btDbvtBroadphase();
  108. ///the default constraint solver. For parallel processing you can use a different solver (see Extras/BulletMultiThreaded)
  109. m_bt_solver = new btSequentialImpulseConstraintSolver;
  110. m_bt_world = new btDiscreteDynamicsWorld(m_bt_dispatcher, m_bt_broadphase, m_bt_solver, m_bt_collision_config);
  111. //m_bt_world->setDebugDrawer(&sDebugDrawer);
  112. m_bt_world->getSolverInfo().m_splitImpulse = true;
  113. m_bt_world->getSolverInfo().m_numIterations = 20;
  114. m_bt_world->getDispatchInfo().m_useContinuous = (m_bt_ccd_mode == USE_CCD);
  115. m_bt_world->setGravity(btVector3(0,-10,0));
  116. ///create a few basic rigid bodies
  117. btBoxShape* box = new btBoxShape(btVector3(btScalar(110.),btScalar(1.),btScalar(110.)));
  118. btCollisionShape* groundShape = box;
  119. m_bt_collision_shapes << groundShape;
  120. m_ground_mesh.Compile("[sc#ddd afcb220 2 220 -1]");
  121. //m_bt_collision_shapes << new btCylinderShape(btVector3(.5f,.5f,.5f));
  122. btTransform groundTransform;
  123. groundTransform.setIdentity();
  124. //We can also use DemoApplication::localCreateRigidBody, but for clarity it is provided here:
  125. {
  126. btScalar mass(0.);
  127. //rigidbody is dynamic if and only if mass is non zero, otherwise static
  128. bool isDynamic = (mass != 0.f);
  129. btVector3 localInertia(0,0,0);
  130. if (isDynamic)
  131. groundShape->calculateLocalInertia(mass,localInertia);
  132. //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
  133. btDefaultMotionState* myMotionState = new btDefaultMotionState(groundTransform);
  134. btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,groundShape,localInertia);
  135. btRigidBody* body = new btRigidBody(rbInfo);
  136. //add the body to the dynamics world
  137. m_bt_world->addRigidBody(body);
  138. }
  139. //Adding Shapes
  140. {
  141. //create a few dynamic rigidbodies
  142. // Re-using the same collision is better for memory usage and performance
  143. btCollisionShape* colShape = new btBoxShape(btVector3(1,1,1));
  144. m_rigid_mesh[0].Compile("[sc#add afcb2 2 2 -.1]");
  145. m_rigid_mesh[1].Compile("[sc#dad afcb2 2 2 -.1]");
  146. m_rigid_mesh[2].Compile("[sc#dda afcb2 2 2 -.1]");
  147. m_rigid_mesh[3].Compile("[sc#daa afcb2 2 2 -.1]");
  148. m_rigid_mesh[4].Compile("[sc#ada afcb2 2 2 -.1]");
  149. m_rigid_mesh[5].Compile("[sc#aad afcb2 2 2 -.1]");
  150. m_bt_collision_shapes << colShape;
  151. m_bt_dynamic_shapes << colShape;
  152. /// Create Dynamic Objects
  153. btTransform startTransform;
  154. startTransform.setIdentity();
  155. btScalar mass(1.f);
  156. //rigidbody is dynamic if and only if mass is non zero, otherwise static
  157. bool isDynamic = (mass != 0.f);
  158. btVector3 localInertia(0,0,0);
  159. if (isDynamic)
  160. colShape->calculateLocalInertia(mass,localInertia);
  161. int i;
  162. for (i=0;i<gNumObjects;i++)
  163. {
  164. btCollisionShape* shape = colShape;
  165. btTransform trans;
  166. trans.setIdentity();
  167. //stack them
  168. int colsize = 10;
  169. int row = int(((float)i*CUBE_HALF_EXTENTS*2.0f)/((float)colsize*2.0f*CUBE_HALF_EXTENTS));
  170. int row2 = row;
  171. int col = (i)%(colsize)-colsize/2;
  172. if (col>3)
  173. {
  174. col=11;
  175. row2 |=1;
  176. }
  177. btVector3 pos(((row+col+row2) % 4)*CUBE_HALF_EXTENTS,
  178. 20.0f + row*8*CUBE_HALF_EXTENTS+CUBE_HALF_EXTENTS+EXTRA_HEIGHT,
  179. col*8*CUBE_HALF_EXTENTS + 2 * (row2%2)*CUBE_HALF_EXTENTS);
  180. trans.setOrigin(pos);
  181. float mass = 1.f;
  182. btAssert((!shape || shape->getShapeType() != INVALID_SHAPE_PROXYTYPE));
  183. //rigidbody is dynamic if and only if mass is non zero, otherwise static
  184. bool isDynamic = (mass != 0.f);
  185. btVector3 localInertia(0,0,0);
  186. if (isDynamic)
  187. shape->calculateLocalInertia(mass,localInertia);
  188. //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
  189. btDefaultMotionState* myMotionState = new btDefaultMotionState(trans);
  190. btRigidBody::btRigidBodyConstructionInfo cInfo(mass,myMotionState,shape,localInertia);
  191. btRigidBody* body = new btRigidBody(cInfo);
  192. body->setContactProcessingThreshold(BT_LARGE_FLOAT);
  193. m_bt_world->addRigidBody(body);
  194. ///when using m_ccdMode
  195. if (m_bt_ccd_mode == USE_CCD)
  196. {
  197. body->setCcdMotionThreshold(CUBE_HALF_EXTENTS);
  198. body->setCcdSweptSphereRadius(0.9*CUBE_HALF_EXTENTS);
  199. }
  200. }
  201. }
  202. }
  203. #endif
  204. }
  205. void BtPhysTest::TickGame(float seconds)
  206. {
  207. WorldEntity::TickGame(seconds);
  208. if (Input::GetButtonState(27 /*SDLK_ESCAPE*/))
  209. Ticker::Shutdown();
  210. m_simulation->TickContext(seconds);
  211. vec3 barycenter = vec3(.0f);
  212. float factor = .0f;
  213. for (int i = 0; i < m_ground_list.Count(); i++)
  214. {
  215. PhysicsObject* PhysObj = m_ground_list[i];
  216. mat4 GroundMat = PhysObj->GetTransform();
  217. vec3 CenterToGround = GroundMat.v3.xyz - vec3(.0f, 50.f, .0f);
  218. vec3 CenterToCam = m_camera->m_position - vec3(.0f, 50.f, .0f);
  219. if (dot(CenterToCam, CenterToGround) > .0f)
  220. PhysObj->SetRender(false);
  221. else
  222. PhysObj->SetRender(true);
  223. barycenter += GroundMat.v3.xyz;
  224. factor += 1.f;
  225. }
  226. barycenter /= factor;
  227. for (int i = 0; i < m_ground_list.Count(); i++)
  228. {
  229. PhysicsObject* PhysObj = m_ground_list[i];
  230. mat4 GroundMat = PhysObj->GetTransform();
  231. mat4 CenterMx = mat4::translate(barycenter);
  232. //GroundMat = inverse(CenterMx) * GroundMat;
  233. //GroundMat = CenterMx * GroundMat;
  234. //mat4(quat::rotate(seconds * 10.0f, vec3(0, 1, 0))) * CenterMx;
  235. //PhysObj->SetTransform(GroundMat.v3.xyz, quat(GroundMat));
  236. }
  237. m_camera->SetTarget(barycenter);
  238. m_camera->SetPosition(vec3(-40.0f, 60.0f, -40.0f));
  239. #if 0
  240. ///step the simulation
  241. if (m_bt_world)
  242. {
  243. //int steps = (int)(seconds / 0.005f);
  244. //for (int i = 0; i < steps; i++)
  245. m_bt_world->stepSimulation(seconds /*/ steps*/);
  246. //optional but useful: debug drawing
  247. //m_bt_world->debugDrawWorld();
  248. }
  249. #endif
  250. }
  251. void BtPhysTest::TickDraw(float seconds)
  252. {
  253. WorldEntity::TickDraw(seconds);
  254. if (!m_ready)
  255. {
  256. #if 0
  257. m_ground_mesh.MeshConvert();
  258. m_rigid_mesh[0].MeshConvert();
  259. m_rigid_mesh[1].MeshConvert();
  260. m_rigid_mesh[2].MeshConvert();
  261. m_rigid_mesh[3].MeshConvert();
  262. m_rigid_mesh[4].MeshConvert();
  263. m_rigid_mesh[5].MeshConvert();
  264. #endif
  265. /* FIXME: this object never cleans up */
  266. m_ready = true;
  267. }
  268. Video::SetClearColor(vec4(0.0f, 0.0f, 0.12f, 1.0f));
  269. #if 0
  270. vec3 BarycenterLocation = vec3(.0f);
  271. float BarycenterFactor = 0.0f;
  272. for(int i=0;i<gNumObjects;i++)
  273. {
  274. mat4 m(1.0f);
  275. btMatrix3x3 rot; rot.setIdentity();
  276. btCollisionObject* colObj = m_bt_world->getCollisionObjectArray()[i];
  277. btRigidBody* body = btRigidBody::upcast(colObj);
  278. if(body && body->getMotionState())
  279. {
  280. btDefaultMotionState* myMotionState = (btDefaultMotionState*)body->getMotionState();
  281. myMotionState->m_graphicsWorldTrans.getOpenGLMatrix(&m[0][0]);
  282. rot = myMotionState->m_graphicsWorldTrans.getBasis();
  283. }
  284. else
  285. {
  286. colObj->getWorldTransform().getOpenGLMatrix(&m[0][0]);
  287. rot = colObj->getWorldTransform().getBasis();
  288. }
  289. if (i > 0)
  290. {
  291. BarycenterLocation += m.v3.xyz;
  292. BarycenterFactor += 1.0f;
  293. }
  294. if (i == 0)
  295. m_ground_mesh.Render(m);
  296. else
  297. m_rigid_mesh[i % 6].Render(m);
  298. }
  299. if (BarycenterFactor > .0f)
  300. {
  301. BarycenterLocation /= BarycenterFactor;
  302. m_camera->SetTarget(BarycenterLocation);
  303. m_camera->SetPosition(BarycenterLocation + vec3(-20.0f, 8.0f, .0f));
  304. }
  305. #endif
  306. }
  307. BtPhysTest::~BtPhysTest()
  308. {
  309. Ticker::Unref(m_camera);
  310. while (m_ground_list.Count())
  311. {
  312. PhysicsObject* CurPop = m_ground_list.Last();
  313. m_ground_list.Pop();
  314. Ticker::Unref(CurPop);
  315. }
  316. while (m_physobj_list.Count())
  317. {
  318. PhysicsObject* CurPop = m_physobj_list.Last();
  319. m_physobj_list.Pop();
  320. Ticker::Unref(CurPop);
  321. }
  322. #if 0
  323. //Exit Physics
  324. {
  325. //cleanup in the reverse order of creation/initialization
  326. //remove the rigidbodies from the dynamics world and delete them
  327. for (int i = m_bt_world->getNumCollisionObjects() - 1; i >= 0 ;i--)
  328. {
  329. btCollisionObject* obj = m_bt_world->getCollisionObjectArray()[i];
  330. btRigidBody* body = btRigidBody::upcast(obj);
  331. if (body && body->getMotionState())
  332. delete body->getMotionState();
  333. m_bt_world->removeCollisionObject(obj);
  334. delete obj;
  335. }
  336. //delete collision shapes
  337. for (int j = 0; j < m_bt_collision_shapes.Count(); j++)
  338. {
  339. btCollisionShape* shape = m_bt_collision_shapes[j];
  340. delete shape;
  341. }
  342. m_bt_collision_shapes.Empty();
  343. delete m_bt_world;
  344. delete m_bt_solver;
  345. delete m_bt_broadphase;
  346. delete m_bt_dispatcher;
  347. delete m_bt_collision_config;
  348. }
  349. #endif
  350. }
  351. int main(int argc, char **argv)
  352. {
  353. Application app("BtPhysTest", ivec2(1280, 720), 60.0f);
  354. #if defined _MSC_VER && !defined _XBOX
  355. _chdir("..");
  356. #elif defined _WIN32 && !defined _XBOX
  357. _chdir("../..");
  358. #endif
  359. new BtPhysTest(argc > 1);
  360. app.ShowPointer(false);
  361. app.Run();
  362. return EXIT_SUCCESS;
  363. }