331 lines
9.4 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2012 Sam Hocevar <sam@hocevar.net>
  5. // (c) 2009-2012 Cédric Lecacheur <jordx@free.fr>
  6. // (c) 2009-2012 Benjamin 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://sam.zoy.org/projects/COPYING.WTFPL for more details.
  11. //
  12. #if defined HAVE_CONFIG_H
  13. # include "config.h"
  14. #endif
  15. #include "../Include/LolBtPhysicsIntegration.h"
  16. #include "../Include/LolPhysics.h"
  17. namespace lol
  18. {
  19. namespace phys
  20. {
  21. #ifdef HAVE_PHYS_USE_BULLET
  22. //-------------------------------------------------------------------------
  23. //EASY_PHYSIC
  24. //--
  25. EasyPhysic::EasyPhysic(WorldEntity* NewOwnerEntity) :
  26. m_collision_object(NULL),
  27. m_ghost_object(NULL),
  28. m_rigid_body(NULL),
  29. m_local_inertia(btVector3(.0f, .0f, .0f)),
  30. m_collision_shape(NULL),
  31. m_convex_shape(NULL),
  32. m_motion_state(NULL),
  33. m_mass(.0f),
  34. m_collision_group(1),
  35. m_collision_mask(1),
  36. m_owner_entity(NewOwnerEntity),
  37. m_base_physic(NULL)
  38. {
  39. }
  40. EasyPhysic::~EasyPhysic()
  41. {
  42. m_rigid_body = NULL;
  43. delete m_collision_object;
  44. delete m_collision_shape;
  45. delete m_motion_state;
  46. }
  47. //-------------------------------------------------------------------------
  48. //Set Shape functions
  49. //--
  50. void EasyPhysic::SetShapeTo(btCollisionShape* collision_shape)
  51. {
  52. bool bReinitToRigidBody = false;
  53. if (m_rigid_body)
  54. {
  55. bReinitToRigidBody = true;
  56. delete m_rigid_body;
  57. }
  58. if (m_collision_shape)
  59. delete m_collision_shape;
  60. m_collision_shape = collision_shape;
  61. if (bReinitToRigidBody)
  62. InitBodyToRigid();
  63. }
  64. //Box Shape support
  65. void EasyPhysic::SetShapeToBox(lol::vec3& box_size)
  66. {
  67. vec3 new_box_size = box_size * LOL2BT_UNIT * LOL2BT_SIZE;
  68. m_convex_shape = new btBoxShape(LOL2BT_VEC3(new_box_size));
  69. SetShapeTo(m_convex_shape);
  70. }
  71. void EasyPhysic::SetShapeToSphere(float radius)
  72. {
  73. m_convex_shape = new btSphereShape(radius * LOL2BT_UNIT * LOL2BT_SIZE);
  74. SetShapeTo(m_convex_shape);
  75. }
  76. void EasyPhysic::SetShapeToCone(float radius, float height)
  77. {
  78. m_convex_shape = new btConeShape( radius * LOL2BT_UNIT,
  79. height * LOL2BT_UNIT);
  80. SetShapeTo(m_convex_shape);
  81. }
  82. void EasyPhysic::SetShapeToCylinder(lol::vec3& cyl_size)
  83. {
  84. vec3 new_cyl_size = cyl_size * LOL2BT_UNIT;
  85. new_cyl_size.y *= LOL2BT_SIZE;
  86. m_convex_shape = new btCylinderShape(LOL2BT_VEC3(new_cyl_size));
  87. SetShapeTo(m_convex_shape);
  88. }
  89. void EasyPhysic::SetShapeToCapsule(float radius, float height)
  90. {
  91. m_convex_shape = new btCapsuleShape(radius * LOL2BT_UNIT * LOL2BT_SIZE,
  92. height * LOL2BT_UNIT * LOL2BT_SIZE);
  93. SetShapeTo(m_convex_shape);
  94. }
  95. //-------------------------------------------------------------------------
  96. //Base Location/Rotation setup
  97. //--
  98. //Getter
  99. mat4 EasyPhysic::GetTransform()
  100. {
  101. m_local_to_world = lol::mat4(1.0f);
  102. if (m_rigid_body && m_motion_state)
  103. {
  104. btTransform CurTransform;
  105. m_motion_state->getWorldTransform(CurTransform);
  106. CurTransform.getOpenGLMatrix(&m_local_to_world[0][0]);
  107. }
  108. else if (m_collision_object)
  109. m_collision_object->getWorldTransform().getOpenGLMatrix(&m_local_to_world[0][0]);
  110. return m_local_to_world;
  111. }
  112. //Setter
  113. void EasyPhysic::SetTransform(const lol::vec3& base_location, const lol::quat& base_rotation)
  114. {
  115. lol::mat4 PreviousMatrix = m_local_to_world;
  116. m_local_to_world = lol::mat4::translate(base_location) * lol::mat4(base_rotation);
  117. if (m_ghost_object)
  118. m_ghost_object->setWorldTransform(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(LOL2BT_UNIT * base_location)));
  119. else
  120. {
  121. if (m_motion_state)
  122. m_motion_state->setWorldTransform(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(LOL2BT_UNIT * base_location)));
  123. else
  124. m_motion_state = new btDefaultMotionState(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(LOL2BT_UNIT * base_location)));
  125. }
  126. for (int i = 0; i < m_based_physic_list.Count(); i++)
  127. {
  128. if (m_based_physic_list[i])
  129. m_based_physic_list[i]->BaseTransformChanged(PreviousMatrix, m_local_to_world);
  130. else
  131. m_based_physic_list.Remove(i--);
  132. }
  133. }
  134. //Internal callback when Base transform has changed.
  135. void EasyPhysic::BaseTransformChanged(const lol::mat4& PreviousMatrix, const lol::mat4& NewMatrix)
  136. {
  137. mat4 PreviousMatrixLoc = ((m_base_lock_location)?(PreviousMatrix):(lol::mat4::translate(PreviousMatrix.v3.xyz)));
  138. mat4 PreviousMatrixRot = ((m_base_lock_rotation)?(lol::mat4(lol::quat(PreviousMatrix))):(lol::mat4(1.f)));
  139. mat4 NewMatrixLoc = ((m_base_lock_location)?(NewMatrix):(lol::mat4::translate(NewMatrix.v3.xyz)));
  140. mat4 NewMatrixRot = ((m_base_lock_rotation)?(lol::mat4(lol::quat(NewMatrix))):(lol::mat4(1.f)));
  141. if (m_ghost_object || (m_rigid_body->getCollisionFlags() & btCollisionObject::CF_KINEMATIC_OBJECT))
  142. {
  143. mat4 ThisMatrixLoc = NewMatrixLoc * inverse(PreviousMatrixLoc) * lol::mat4::translate(m_local_to_world.v3.xyz);
  144. mat4 ThisMatrixRot = NewMatrixRot * inverse(PreviousMatrixRot) * lol::mat4(lol::quat(m_local_to_world));
  145. SetTransform(ThisMatrixLoc.v3.xyz, lol::mat4(lol::quat(ThisMatrixRot)));
  146. }
  147. }
  148. //-------------------------------------------------------------------------
  149. //Mass related functions
  150. //--
  151. //Set Shape functions
  152. void EasyPhysic::SetMass(float mass)
  153. {
  154. m_mass = mass;
  155. if (m_rigid_body)
  156. {
  157. SetLocalInertia(m_mass);
  158. m_rigid_body->setMassProps(mass, m_local_inertia);
  159. }
  160. }
  161. //-------------------------------------------------------------------------
  162. //Final conversion pass functons : Body related
  163. //--
  164. //Init to rigid body
  165. void EasyPhysic::InitBodyToRigid(bool SetToKinematic)
  166. {
  167. if (m_collision_object)
  168. delete m_collision_object;
  169. if (!m_motion_state)
  170. SetTransform(vec3(.0f));
  171. btRigidBody::btRigidBodyConstructionInfo NewInfos(m_mass, m_motion_state, m_collision_shape, m_local_inertia);
  172. m_rigid_body = new btRigidBody(NewInfos);
  173. m_collision_object = m_rigid_body;
  174. m_collision_object->setUserPointer(this);
  175. if (m_mass == .0f)
  176. {
  177. if (SetToKinematic)
  178. {
  179. m_rigid_body->setActivationState(DISABLE_DEACTIVATION);
  180. m_rigid_body->setCollisionFlags(m_rigid_body->getCollisionFlags() | btCollisionObject::CF_KINEMATIC_OBJECT);
  181. }
  182. }
  183. else
  184. SetMass(m_mass);
  185. }
  186. //Return correct Ghost Object
  187. btGhostObject* EasyPhysic::GetGhostObjectInstance()
  188. {
  189. return new btGhostObject();
  190. }
  191. //Init to Ghost object, for Overlap/Sweep Test/Touching logic
  192. void EasyPhysic::InitBodyToGhost()
  193. {
  194. if (m_collision_object)
  195. delete m_collision_object;
  196. m_ghost_object = GetGhostObjectInstance();
  197. m_ghost_object->setCollisionShape(m_collision_shape);
  198. m_collision_object = m_ghost_object;
  199. m_collision_object->setUserPointer(this);
  200. SetTransform(m_local_to_world.v3.xyz, lol::quat(m_local_to_world));
  201. m_ghost_object->setCollisionFlags(m_ghost_object->getCollisionFlags());
  202. }
  203. //-------------
  204. //Touch logic
  205. //-------------
  206. // btManifoldArray manifoldArray;
  207. // btBroadphasePairArray& pairArray = ghostObject->getOverlappingPairCache()->getOverlappingPairArray();
  208. // int numPairs = pairArray.size();
  209. // for (int i=0;i<numPairs;i++)
  210. // {
  211. // manifoldArray.clear();
  212. // const btBroadphasePair& pair = pairArray[i];
  213. //
  214. // //unless we manually perform collision detection on this pair, the contacts are in the dynamics world paircache:
  215. // btBroadphasePair* collisionPair = dynamicsWorld->getPairCache()->findPair(pair.m_pProxy0,pair.m_pProxy1);
  216. // if (!collisionPair)
  217. // continue;
  218. // if (collisionPair->m_algorithm)
  219. // collisionPair->m_algorithm->getAllContactManifolds(manifoldArray);
  220. // for (int j=0;j<manifoldArray.size();j++)
  221. // {
  222. // btPersistentManifold* manifold = manifoldArray[j];
  223. // btScalar directionSign = manifold->getBody0() == m_ghostObject ? btScalar(-1.0) : btScalar(1.0);
  224. // for (int p=0;p<manifold->getNumContacts();p++)
  225. // {
  226. // const btManifoldPoint&pt = manifold->getContactPoint(p);
  227. // if (pt.getDistance()<0.f)
  228. //{
  229. // const btVector3& ptA = pt.getPositionWorldOnA();
  230. // const btVector3& ptB = pt.getPositionWorldOnB();
  231. // const btVector3& normalOnB = pt.m_normalWorldOnB;
  232. // /// work here
  233. //}
  234. // }
  235. // }
  236. // }
  237. //Add Physic object to the simulation
  238. void EasyPhysic::AddToSimulation(class Simulation* current_simulation)
  239. {
  240. btDiscreteDynamicsWorld* dynamics_world = current_simulation->GetWorld();
  241. if (dynamics_world)
  242. {
  243. if (m_ghost_object)
  244. {
  245. dynamics_world->addCollisionObject(m_ghost_object, m_collision_group, m_collision_mask);
  246. current_simulation->AddToGhost(this);
  247. }
  248. else if (m_rigid_body)
  249. {
  250. dynamics_world->addRigidBody(m_rigid_body, m_collision_group, m_collision_mask);
  251. if (m_mass != .0f)
  252. current_simulation->AddToDynamic(this);
  253. else
  254. current_simulation->AddToStatic(this);
  255. }
  256. else
  257. dynamics_world->addCollisionObject(m_collision_object, m_collision_group, m_collision_mask);
  258. }
  259. }
  260. //Remove Physic object to the simulation
  261. void EasyPhysic::RemoveFromSimulation(class Simulation* current_simulation)
  262. {
  263. btDiscreteDynamicsWorld* dynamics_world = current_simulation->GetWorld();
  264. if (dynamics_world)
  265. {
  266. if (m_rigid_body)
  267. dynamics_world->removeRigidBody(m_rigid_body);
  268. else if (m_collision_object)
  269. dynamics_world->removeCollisionObject(m_collision_object);
  270. }
  271. }
  272. //Set Local Inertia
  273. void EasyPhysic::SetLocalInertia(float mass)
  274. {
  275. if (mass != .0f)
  276. m_collision_shape->calculateLocalInertia(mass, m_local_inertia);
  277. else
  278. m_local_inertia = btVector3(.0f, .0f, .0f);
  279. }
  280. #endif // HAVE_PHYS_USE_BULLET
  281. } /* namespace phys */
  282. } /* namespace lol */