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