Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 

433 wiersze
13 KiB

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