Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

easyphysics.cpp 13 KiB

vor 12 Jahren
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2013 Sam Hocevar <sam@hocevar.net>
  5. // (c) 2009-2013 Cdric Lecacheur <jordx@free.fr>
  6. // (c) 2009-2013 Benjamin "Touky" Huet <huet.benjamin@gmail.com>
  7. // This program is free software; you can redistribute it and/or
  8. // modify it under the terms of the Do What The Fuck You Want To
  9. // Public License, Version 2, as published by Sam Hocevar. See
  10. // http://www.wtfpl.net/ for more details.
  11. //
  12. #if HAVE_CONFIG_H
  13. # include "config.h"
  14. #endif
  15. #include "lolbtphysicsintegration.h"
  16. #include "lolphysics.h"
  17. namespace lol
  18. {
  19. namespace phys
  20. {
  21. //-------------------------------------------------------------------------
  22. //EASY_PHYSIC
  23. //--
  24. EasyPhysic::EasyPhysic(WorldEntity* NewOwnerEntity) :
  25. m_collision_object(NULL),
  26. m_ghost_object(NULL),
  27. m_rigid_body(NULL),
  28. m_local_inertia(btVector3(.0f, .0f, .0f)),
  29. m_collision_shape(NULL),
  30. m_convex_shape(NULL),
  31. m_motion_state(NULL),
  32. m_mass(.0f),
  33. m_hit_restitution(.0f),
  34. m_collision_group(1),
  35. m_collision_mask(1),
  36. m_owner_entity(NewOwnerEntity),
  37. m_owner_simulation(NULL),
  38. m_base_physic(NULL)
  39. {
  40. }
  41. EasyPhysic::~EasyPhysic()
  42. {
  43. m_rigid_body = NULL;
  44. delete m_collision_object;
  45. delete m_collision_shape;
  46. delete m_motion_state;
  47. }
  48. //-------------------------------------------------------------------------
  49. //Set Shape functions
  50. //--
  51. void EasyPhysic::SetShapeTo(btCollisionShape* collision_shape)
  52. {
  53. bool bReinitToRigidBody = false;
  54. if (m_rigid_body)
  55. {
  56. bReinitToRigidBody = true;
  57. delete m_rigid_body;
  58. }
  59. if (m_collision_shape)
  60. delete m_collision_shape;
  61. m_collision_shape = collision_shape;
  62. if (bReinitToRigidBody)
  63. InitBodyToRigid();
  64. }
  65. //Box Shape support
  66. void EasyPhysic::SetShapeToBox(lol::vec3& box_size)
  67. {
  68. vec3 new_box_size = box_size * LOL2BT_UNIT * LOL2BT_SIZE;
  69. m_convex_shape = new btBoxShape(LOL2BT_VEC3(new_box_size));
  70. SetShapeTo(m_convex_shape);
  71. }
  72. void EasyPhysic::SetShapeToSphere(float radius)
  73. {
  74. m_convex_shape = new btSphereShape(radius * LOL2BT_UNIT * LOL2BT_SIZE);
  75. SetShapeTo(m_convex_shape);
  76. }
  77. void EasyPhysic::SetShapeToCone(float radius, float height)
  78. {
  79. m_convex_shape = new btConeShape( radius * LOL2BT_UNIT,
  80. height * LOL2BT_UNIT);
  81. SetShapeTo(m_convex_shape);
  82. }
  83. void EasyPhysic::SetShapeToCylinder(lol::vec3& cyl_size)
  84. {
  85. vec3 new_cyl_size = cyl_size * LOL2BT_UNIT;
  86. new_cyl_size.y *= LOL2BT_SIZE;
  87. m_convex_shape = new btCylinderShape(LOL2BT_VEC3(new_cyl_size));
  88. SetShapeTo(m_convex_shape);
  89. }
  90. void EasyPhysic::SetShapeToCapsule(float radius, float height)
  91. {
  92. m_convex_shape = new btCapsuleShape(radius * LOL2BT_UNIT * LOL2BT_SIZE,
  93. height * LOL2BT_UNIT * LOL2BT_SIZE);
  94. SetShapeTo(m_convex_shape);
  95. }
  96. //-------------------------------------------------------------------------
  97. //Base Location/Rotation setup
  98. //--
  99. //Getter
  100. mat4 EasyPhysic::GetTransform()
  101. {
  102. m_local_to_world = lol::mat4(1.0f);
  103. if (m_rigid_body && m_motion_state)
  104. {
  105. btTransform CurTransform;
  106. m_motion_state->getWorldTransform(CurTransform);
  107. CurTransform.getOpenGLMatrix(&m_local_to_world[0][0]);
  108. }
  109. else if (m_collision_object)
  110. m_collision_object->getWorldTransform().getOpenGLMatrix(&m_local_to_world[0][0]);
  111. return m_local_to_world;
  112. }
  113. //Setter
  114. void EasyPhysic::SetTransform(const lol::vec3& base_location, const lol::quat& base_rotation)
  115. {
  116. lol::mat4 PreviousMatrix = m_local_to_world;
  117. m_local_to_world = lol::mat4::translate(base_location) * lol::mat4(base_rotation);
  118. if (m_ghost_object)
  119. m_ghost_object->setWorldTransform(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(LOL2BT_UNIT * base_location)));
  120. else
  121. {
  122. if (m_motion_state)
  123. m_motion_state->setWorldTransform(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(LOL2BT_UNIT * base_location)));
  124. else
  125. m_motion_state = new btDefaultMotionState(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(LOL2BT_UNIT * base_location)));
  126. }
  127. for (int i = 0; i < m_based_physic_list.count(); i++)
  128. {
  129. if (m_based_physic_list[i])
  130. m_based_physic_list[i]->BaseTransformChanged(PreviousMatrix, m_local_to_world);
  131. else
  132. m_based_physic_list.remove(i--);
  133. }
  134. }
  135. //Internal callback when Base transform has changed.
  136. void EasyPhysic::BaseTransformChanged(const lol::mat4& PreviousMatrix, const lol::mat4& NewMatrix)
  137. {
  138. mat4 PreviousMatrixLoc = ((m_base_lock_location)?(PreviousMatrix):(lol::mat4::translate(PreviousMatrix[3].xyz)));
  139. mat4 PreviousMatrixRot = ((m_base_lock_rotation)?(lol::mat4(lol::quat(PreviousMatrix))):(lol::mat4(1.f)));
  140. mat4 NewMatrixLoc = ((m_base_lock_location)?(NewMatrix):(lol::mat4::translate(NewMatrix[3].xyz)));
  141. mat4 NewMatrixRot = ((m_base_lock_rotation)?(lol::mat4(lol::quat(NewMatrix))):(lol::mat4(1.f)));
  142. if (m_ghost_object || (m_rigid_body->getCollisionFlags() & btCollisionObject::CF_KINEMATIC_OBJECT))
  143. {
  144. mat4 ThisMatrixLoc = NewMatrixLoc * inverse(PreviousMatrixLoc) * lol::mat4::translate(m_local_to_world[3].xyz);
  145. mat4 ThisMatrixRot = NewMatrixRot * inverse(PreviousMatrixRot) * lol::mat4(lol::quat(m_local_to_world));
  146. SetTransform(ThisMatrixLoc[3].xyz, lol::quat(ThisMatrixRot));
  147. }
  148. }
  149. //-------------------------------------------------------------------------
  150. //Mass related functions
  151. //--
  152. //Set Mass functions
  153. void EasyPhysic::SetMass(float mass)
  154. {
  155. m_mass = mass;
  156. if (m_rigid_body)
  157. {
  158. SetLocalInertia(m_mass);
  159. m_rigid_body->setMassProps(m_mass, m_local_inertia);
  160. }
  161. }
  162. //-------------------------------------------------------------------------
  163. //Hit restitution functions
  164. //--
  165. //Set Hit Restitution functions
  166. void EasyPhysic::SetHitRestitution(float hit_restitution)
  167. {
  168. m_hit_restitution = hit_restitution;
  169. if (m_rigid_body)
  170. {
  171. m_rigid_body->setRestitution(m_hit_restitution);
  172. }
  173. }
  174. //-------------------------------------------------------------------------
  175. //Final conversion pass functions : Body related
  176. //--
  177. //Init to rigid body
  178. void EasyPhysic::InitBodyToRigid(bool SetToKinematic)
  179. {
  180. if (m_collision_object)
  181. delete m_collision_object;
  182. if (!m_motion_state)
  183. SetTransform(vec3(.0f));
  184. btRigidBody::btRigidBodyConstructionInfo NewInfos(m_mass, m_motion_state, m_collision_shape, m_local_inertia);
  185. NewInfos.m_restitution = m_hit_restitution;
  186. m_rigid_body = new btRigidBody(NewInfos);
  187. m_collision_object = m_rigid_body;
  188. m_collision_object->setUserPointer(this);
  189. if (m_mass == .0f)
  190. {
  191. if (SetToKinematic)
  192. {
  193. m_rigid_body->setActivationState(DISABLE_DEACTIVATION);
  194. m_rigid_body->setCollisionFlags(m_rigid_body->getCollisionFlags() | btCollisionObject::CF_KINEMATIC_OBJECT);
  195. }
  196. }
  197. else
  198. SetMass(m_mass);
  199. }
  200. //Return correct Ghost Object
  201. btGhostObject* EasyPhysic::GetGhostObjectInstance()
  202. {
  203. return new btGhostObject();
  204. }
  205. //Init to Ghost object, for Overlap/Sweep Test/Touching logic
  206. void EasyPhysic::InitBodyToGhost()
  207. {
  208. if (m_collision_object)
  209. delete m_collision_object;
  210. m_ghost_object = GetGhostObjectInstance();
  211. m_ghost_object->setCollisionShape(m_collision_shape);
  212. m_collision_object = m_ghost_object;
  213. m_collision_object->setUserPointer(this);
  214. SetTransform(m_local_to_world[3].xyz, lol::quat(m_local_to_world));
  215. m_ghost_object->setCollisionFlags(m_ghost_object->getCollisionFlags());
  216. }
  217. //-------------
  218. //Touch logic
  219. //-------------
  220. // btManifoldArray manifoldArray;
  221. // btBroadphasePairArray& pairArray = ghostObject->getOverlappingPairCache()->getOverlappingPairArray();
  222. // int numPairs = pairArray.size();
  223. // for (int i=0;i<numPairs;i++)
  224. // {
  225. // manifoldArray.clear();
  226. // const btBroadphasePair& pair = pairArray[i];
  227. //
  228. // //unless we manually perform collision detection on this pair, the contacts are in the dynamics world paircache:
  229. // btBroadphasePair* collisionPair = dynamicsWorld->getPairCache()->findPair(pair.m_pProxy0,pair.m_pProxy1);
  230. // if (!collisionPair)
  231. // continue;
  232. // if (collisionPair->m_algorithm)
  233. // collisionPair->m_algorithm->getAllContactManifolds(manifoldArray);
  234. // for (int j=0;j<manifoldArray.size();j++)
  235. // {
  236. // btPersistentManifold* manifold = manifoldArray[j];
  237. // btScalar directionSign = manifold->getBody0() == m_ghostObject ? btScalar(-1.0) : btScalar(1.0);
  238. // for (int p=0;p<manifold->getNumContacts();p++)
  239. // {
  240. // const btManifoldPoint&pt = manifold->getContactPoint(p);
  241. // if (pt.getDistance()<0.f)
  242. //{
  243. // const btVector3& ptA = pt.getPositionWorldOnA();
  244. // const btVector3& ptB = pt.getPositionWorldOnB();
  245. // const btVector3& normalOnB = pt.m_normalWorldOnB;
  246. // /// work here
  247. //}
  248. // }
  249. // }
  250. // }
  251. //Add Physic object to the simulation
  252. void EasyPhysic::AddToSimulation(class Simulation* current_simulation)
  253. {
  254. btDiscreteDynamicsWorld* dynamics_world = current_simulation->GetWorld();
  255. if (dynamics_world)
  256. {
  257. if (m_ghost_object)
  258. {
  259. dynamics_world->addCollisionObject(m_ghost_object, m_collision_group, m_collision_mask);
  260. current_simulation->ObjectRegistration(true, this, Simulation::EEPT_Ghost);
  261. }
  262. else if (m_rigid_body)
  263. {
  264. dynamics_world->addRigidBody(m_rigid_body, m_collision_group, m_collision_mask);
  265. if (m_mass != .0f)
  266. current_simulation->ObjectRegistration(true, this, Simulation::EEPT_Dynamic);
  267. else
  268. current_simulation->ObjectRegistration(true, this, Simulation::EEPT_Static);
  269. }
  270. else
  271. {
  272. dynamics_world->addCollisionObject(m_collision_object, m_collision_group, m_collision_mask);
  273. current_simulation->ObjectRegistration(true, this, Simulation::EEPT_CollisionObject);
  274. }
  275. }
  276. }
  277. //Remove Physic object to the simulation
  278. void EasyPhysic::RemoveFromSimulation(class Simulation* current_simulation)
  279. {
  280. btDiscreteDynamicsWorld* dynamics_world = current_simulation->GetWorld();
  281. if (dynamics_world)
  282. {
  283. if (m_rigid_body)
  284. {
  285. dynamics_world->removeRigidBody(m_rigid_body);
  286. if (m_mass != .0f)
  287. current_simulation->ObjectRegistration(false, this, Simulation::EEPT_Dynamic);
  288. else
  289. current_simulation->ObjectRegistration(false, this, Simulation::EEPT_Static);
  290. }
  291. else
  292. {
  293. dynamics_world->removeCollisionObject(m_collision_object);
  294. if (m_ghost_object)
  295. current_simulation->ObjectRegistration(false, this, Simulation::EEPT_Ghost);
  296. current_simulation->ObjectRegistration(false, this, Simulation::EEPT_CollisionObject);
  297. }
  298. }
  299. }
  300. //-------------------------------------------------------------------------
  301. //Force/Impulse functions
  302. //--
  303. void EasyPhysic::AddImpulse(const lol::vec3& impulse)
  304. {
  305. if (m_rigid_body)
  306. m_rigid_body->applyCentralImpulse(LOL2BT_VEC3(impulse));
  307. }
  308. void EasyPhysic::AddImpulse(const lol::vec3& impulse, const lol::vec3& rel_pos)
  309. {
  310. if (m_rigid_body)
  311. m_rigid_body->applyImpulse(LOL2BT_VEC3(impulse), LOL2BTU_VEC3(rel_pos));
  312. }
  313. void EasyPhysic::AddImpulseTorque(const lol::vec3& torque)
  314. {
  315. if (m_rigid_body)
  316. m_rigid_body->applyTorqueImpulse(LOL2BT_VEC3(torque));
  317. }
  318. //--
  319. void EasyPhysic::AddForce(const lol::vec3& force)
  320. {
  321. if (m_rigid_body)
  322. m_rigid_body->applyCentralForce(LOL2BT_VEC3(force));
  323. }
  324. void EasyPhysic::AddForce(const lol::vec3& force, const lol::vec3& rel_pos)
  325. {
  326. if (m_rigid_body)
  327. m_rigid_body->applyForce(LOL2BT_VEC3(force), LOL2BTU_VEC3(rel_pos));
  328. }
  329. void EasyPhysic::AddForceTorque(const lol::vec3& torque)
  330. {
  331. if (m_rigid_body)
  332. m_rigid_body->applyTorque(LOL2BT_VEC3(torque));
  333. }
  334. //-------------------------------------------------------------------------
  335. //Movements getter
  336. //--
  337. lol::vec3 EasyPhysic::GetLinearVelocity() const
  338. {
  339. if (m_rigid_body)
  340. return BT2LOL_VEC3(m_rigid_body->getLinearVelocity());
  341. return lol::vec3(.0f);
  342. }
  343. lol::vec3 EasyPhysic::GetLinearForce() const
  344. {
  345. if (m_rigid_body)
  346. return BT2LOL_VEC3(m_rigid_body->getTotalForce());
  347. return lol::vec3(.0f);
  348. }
  349. lol::vec3 EasyPhysic::GetAngularVelocity() const
  350. {
  351. if (m_rigid_body)
  352. return BT2LOL_VEC3(m_rigid_body->getAngularVelocity());
  353. return lol::vec3(.0f);
  354. }
  355. lol::vec3 EasyPhysic::GetAngularForce() const
  356. {
  357. if (m_rigid_body)
  358. return BT2LOL_VEC3(m_rigid_body->getTotalTorque());
  359. return lol::vec3(.0f);
  360. }
  361. //Set Local Inertia
  362. void EasyPhysic::SetLocalInertia(float mass)
  363. {
  364. if (mass != .0f)
  365. m_collision_shape->calculateLocalInertia(mass, m_local_inertia);
  366. else
  367. m_local_inertia = btVector3(.0f, .0f, .0f);
  368. }
  369. } /* namespace phys */
  370. } /* namespace lol */