Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 

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