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ů.
 
 
 

349 řádky
10 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2012 Sam Hocevar <sam@hocevar.net>
  5. // (c) 2009-2012 Benjamin Huet <huet.benjamin@gmail.com>
  6. // This program is free software; you can redistribute it and/or
  7. // modify it under the terms of the Do What The Fuck You Want To
  8. // Public License, Version 2, as published by Sam Hocevar. See
  9. // http://sam.zoy.org/projects/COPYING.WTFPL for more details.
  10. //
  11. //
  12. // The EasyPhysic class
  13. // ------------------
  14. //
  15. #if !defined __EASYPHYSICS_EASYPHYSICS_H__
  16. #define __EASYPHYSICS_EASYPHYSICS_H__
  17. #ifdef HAVE_PHYS_USE_BULLET
  18. #include "core.h"
  19. #include <bullet/btBulletDynamicsCommon.h>
  20. #include <bullet/btBulletCollisionCommon.h>
  21. #include <bullet/BulletCollision/CollisionDispatch/btGhostObject.h>
  22. #include <BulletDynamics/Character/btKinematicCharacterController.h>
  23. #endif
  24. namespace lol
  25. {
  26. namespace phys
  27. {
  28. class EasyPhysic
  29. {
  30. friend class EasyConstraint;
  31. #ifdef HAVE_PHYS_USE_BULLET
  32. public:
  33. EasyPhysic();
  34. ~EasyPhysic();
  35. virtual void SetShapeToBox(lol::vec3& box_size);
  36. virtual void SetShapeToSphere(float radius);
  37. virtual void SetShapeToCone(float radius, float height);
  38. virtual void SetShapeToCylinder(lol::vec3& cyl_size);
  39. virtual void SetShapeToCapsule(float radius, float height);
  40. virtual bool CanChangeCollisionChannel() { return (m_rigid_body == NULL); }
  41. virtual void SetTransform(const lol::vec3& base_location, const lol::quat& base_rotation=lol::quat(lol::mat4(1.0f)));
  42. virtual void SetMass(float mass);
  43. virtual void InitBodyToRigid(bool ZeroMassIsKinematic=false);
  44. virtual void InitBodyToGhost();
  45. virtual void AddToSimulation(class Simulation* current_simulation);
  46. virtual void RemoveFromSimulation(class Simulation* current_simulation);
  47. virtual mat4 GetTransform();
  48. protected:
  49. virtual void SetLocalInertia(float mass);
  50. virtual void SetShapeTo(btCollisionShape* collision_shape);
  51. virtual btGhostObject* GetGhostObject();
  52. btCollisionObject* m_collision_object;
  53. btGhostObject* m_ghost_object;
  54. btRigidBody* m_rigid_body;
  55. btVector3 m_local_inertia;
  56. btCollisionShape* m_collision_shape;
  57. btConvexShape* m_convex_shape;
  58. btMotionState* m_motion_state;
  59. #else // NO PHYSIC IMPLEMENTATION
  60. public:
  61. EasyPhysic() { }
  62. virtual void SetShapeToBox(lol::vec3& BoxSize) { }
  63. virtual void SetShapeToSphere(float radius) { }
  64. virtual void SetShapeToCone(float radius, float height) { }
  65. virtual void SetShapeToCylinder(lol::vec3& cyl_size) { }
  66. virtual void SetShapeToCapsule(float radius, float height) { }
  67. virtual bool CanChangeCollisionChannel() { return true; }
  68. virtual void SetTransform(const lol::vec3& base_location, const lol::quat& base_rotation=lol::quat(lol::mat4(1.0f))) { }
  69. virtual void SetMass(float mass) { }
  70. virtual void InitBodyToRigid() { }
  71. virtual void InitBodyToGhost() { }
  72. virtual void AddToSimulation(class Simulation* current_simulation) { }
  73. virtual void RemoveFromSimulation(class Simulation* current_simulation) { }
  74. virtual mat4 GetTransform() { return mat4(1.0f); }
  75. virtual void InitBodyToGhost() { }
  76. #endif // PHYSIC IMPLEMENTATION
  77. public:
  78. //Sets the collision Group & Mask.
  79. //Mask can change at runtime, not group !
  80. virtual bool SetCollisionChannel(int NewGroup, int NewMask)
  81. {
  82. if (CanChangeCollisionChannel())
  83. {
  84. m_collision_group = (1<<NewGroup);
  85. m_collision_mask = NewMask;
  86. return true;
  87. }
  88. return false;
  89. }
  90. int GetCollisionGroup() { return m_collision_group; }
  91. int GetCollisionMask() { return m_collision_mask; }
  92. protected:
  93. lol::mat4 m_local_to_world;
  94. float m_mass;
  95. int m_collision_group;
  96. int m_collision_mask;
  97. };
  98. class EasyCharacterController : public EasyPhysic
  99. {
  100. #ifdef HAVE_PHYS_USE_BULLET
  101. public:
  102. EasyCharacterController() :
  103. EasyPhysic(),
  104. m_character(NULL)
  105. {
  106. m_up_axis = 1;
  107. }
  108. ~EasyCharacterController()
  109. {
  110. delete m_character;
  111. }
  112. virtual void InitBodyToRigid(bool ZeroMassIsKinematic=false);
  113. virtual void InitBodyToGhost();
  114. virtual void AddToSimulation(class Simulation* current_simulation);
  115. virtual void RemoveFromSimulation(class Simulation* current_simulation);
  116. virtual void SetMovementForFrame(vec3 const &MoveQuantity);
  117. protected:
  118. virtual btGhostObject* GetGhostObject();
  119. btPairCachingGhostObject* m_pair_caching_object;
  120. btKinematicCharacterController* m_character;
  121. float m_step_height;
  122. int m_up_axis;
  123. #else // NO PHYSIC IMPLEMENTATION
  124. virtual void InitBodyToRigid(bool ZeroMassIsKinematic=false) { }
  125. virtual void InitBodyToGhost() { }
  126. virtual void AddToSimulation(class Simulation* current_simulation) { }
  127. virtual void RemoveFromSimulation(class Simulation* current_simulation) { }
  128. virtual void SetMovementForFrame(vec3 const &MoveQuantity) { }
  129. #endif // PHYSIC IMPLEMENTATION
  130. };
  131. class EasyConstraint
  132. {
  133. #ifdef HAVE_PHYS_USE_BULLET
  134. public:
  135. EasyConstraint() :
  136. m_typed_constraint(NULL),
  137. m_p2p_constraint(NULL),
  138. m_hinge_constraint(NULL),
  139. m_slider_constraint(NULL),
  140. m_cone_twist_constraint(NULL),
  141. m_6dof_constraint(NULL),
  142. m_a_physobj(NULL),
  143. m_b_physobj(NULL),
  144. m_a_transform(lol::mat4(1.f)),
  145. m_b_transform(lol::mat4(1.f)),
  146. m_using_ref_a(false),
  147. m_disable_a2b_collision(false)
  148. {
  149. }
  150. ~EasyConstraint()
  151. {
  152. delete m_typed_constraint;
  153. m_p2p_constraint = NULL;
  154. m_hinge_constraint = NULL;
  155. m_slider_constraint = NULL;
  156. m_cone_twist_constraint = NULL;
  157. m_6dof_constraint = NULL;
  158. }
  159. void AddToSimulation(class Simulation* current_simulation);
  160. void RemoveFromSimulation(class Simulation* current_simulation);
  161. private:
  162. //check if Init can be done
  163. bool CanProceedWithInit()
  164. {
  165. if (!m_a_physobj || !m_b_physobj)
  166. return false;
  167. if (!m_a_physobj->m_rigid_body || !m_b_physobj->m_rigid_body)
  168. return false;
  169. return true;
  170. }
  171. //-------------------------------------------------------------------------
  172. //Init constraint functions
  173. //--
  174. void CustomInitConstraintToPoint2Point()
  175. {
  176. m_p2p_constraint = new btPoint2PointConstraint(*m_a_physobj->m_rigid_body, *m_b_physobj->m_rigid_body,
  177. LOL2BT_VEC3(m_a_transform.v3.xyz * LOL2BT_UNIT), LOL2BT_VEC3(m_b_transform.v3.xyz * LOL2BT_UNIT));
  178. m_typed_constraint = m_p2p_constraint;
  179. }
  180. void CustomInitConstraintToHinge()
  181. {
  182. m_hinge_constraint = new btHingeConstraint(*m_a_physobj->m_rigid_body, *m_b_physobj->m_rigid_body,
  183. btTransform(LOL2BT_QUAT(quat(m_a_transform)), LOL2BT_VEC3(m_a_transform.v3.xyz * LOL2BT_UNIT)),
  184. btTransform(LOL2BT_QUAT(quat(m_b_transform)), LOL2BT_VEC3(m_b_transform.v3.xyz * LOL2BT_UNIT)),
  185. m_using_ref_a);
  186. m_typed_constraint = m_hinge_constraint;
  187. }
  188. void CustomInitConstraintToSlider()
  189. {
  190. m_slider_constraint = new btSliderConstraint(*m_a_physobj->m_rigid_body, *m_b_physobj->m_rigid_body,
  191. btTransform(LOL2BT_QUAT(quat(m_a_transform)), LOL2BT_VEC3(m_a_transform.v3.xyz * LOL2BT_UNIT)),
  192. btTransform(LOL2BT_QUAT(quat(m_b_transform)), LOL2BT_VEC3(m_b_transform.v3.xyz * LOL2BT_UNIT)),
  193. m_using_ref_a);
  194. m_typed_constraint = m_slider_constraint;
  195. }
  196. void CustomInitConstraintToConeTwist()
  197. {
  198. m_cone_twist_constraint = new btConeTwistConstraint(*m_a_physobj->m_rigid_body, *m_b_physobj->m_rigid_body,
  199. btTransform(LOL2BT_QUAT(quat(m_a_transform)), LOL2BT_VEC3(m_a_transform.v3.xyz * LOL2BT_UNIT)),
  200. btTransform(LOL2BT_QUAT(quat(m_b_transform)), LOL2BT_VEC3(m_b_transform.v3.xyz * LOL2BT_UNIT)));
  201. m_typed_constraint = m_cone_twist_constraint;
  202. }
  203. void CustomInitConstraintTo6Dof()
  204. {
  205. m_6dof_constraint = new btGeneric6DofConstraint(*m_a_physobj->m_rigid_body, *m_b_physobj->m_rigid_body,
  206. btTransform(LOL2BT_QUAT(quat(m_a_transform)), LOL2BT_VEC3(m_a_transform.v3.xyz * LOL2BT_UNIT)),
  207. btTransform(LOL2BT_QUAT(quat(m_b_transform)), LOL2BT_VEC3(m_b_transform.v3.xyz * LOL2BT_UNIT)),
  208. m_using_ref_a);
  209. m_typed_constraint = m_6dof_constraint;
  210. }
  211. btTypedConstraint* m_typed_constraint;
  212. btPoint2PointConstraint* m_p2p_constraint;
  213. btHingeConstraint* m_hinge_constraint;
  214. btSliderConstraint* m_slider_constraint;
  215. btConeTwistConstraint* m_cone_twist_constraint;
  216. btGeneric6DofConstraint* m_6dof_constraint;
  217. #else // NO PHYSIC IMPLEMENTATION
  218. public:
  219. EasyConstraint() :
  220. m_a_physobj(NULL),
  221. m_b_physobj(NULL),
  222. m_a_transform(lol::mat4(1.f)),
  223. m_b_transform(lol::mat4(1.f)),
  224. m_using_ref_a(false),
  225. m_disable_a2b_collision(false)
  226. {
  227. }
  228. private:
  229. void AddToSimulation(class Simulation* current_simulation) { }
  230. void RemoveFromSimulation(class Simulation* current_simulation) { }
  231. //check if Init can be done
  232. bool CanProceedWithInit() { return false; }
  233. void CustomInitConstraintToPoint2Point() { }
  234. void CustomInitConstraintToHinge() { }
  235. void CustomInitConstraintToSlider() { }
  236. void CustomInitConstraintToConeTwist() { }
  237. void CustomInitConstraintTo6Dof() { }
  238. #endif // PHYSIC IMPLEMENTATION
  239. public:
  240. void InitConstraintToPoint2Point() { if (CanProceedWithInit()) CustomInitConstraintToPoint2Point(); }
  241. void InitConstraintToHinge() { if (CanProceedWithInit()) CustomInitConstraintToHinge(); }
  242. void InitConstraintToSlider() { if (CanProceedWithInit()) CustomInitConstraintToSlider(); }
  243. void InitConstraintToConeTwist() { if (CanProceedWithInit()) CustomInitConstraintToConeTwist(); }
  244. void InitConstraintTo6Dof() { if (CanProceedWithInit()) CustomInitConstraintTo6Dof(); }
  245. //Set given physic object to the proper slot.
  246. void SetPhysObjA(EasyPhysic* NewPhysObj, lol::mat4 NewTransform) { SetPhysObj(false, NewPhysObj, NewTransform); }
  247. void SetPhysObjB(EasyPhysic* NewPhysObj, lol::mat4 NewTransform) { SetPhysObj(true, NewPhysObj, NewTransform); }
  248. void SetPhysObj(bool SetToB, EasyPhysic* NewPhysObj, lol::mat4 NewTransform)
  249. {
  250. if (SetToB)
  251. {
  252. m_b_physobj = NewPhysObj;
  253. m_b_transform = NewTransform;
  254. }
  255. else
  256. {
  257. m_a_physobj = NewPhysObj;
  258. m_a_transform = NewTransform;
  259. }
  260. }
  261. //Set whether or not the physic engine should use the A object as the reference (most constraint transform are local).
  262. void SetRefAsA(bool NewUseRefA)
  263. {
  264. m_using_ref_a = NewUseRefA;
  265. }
  266. //Set whether or not to disable the collision between the bodies
  267. void DisableCollisionBetweenObjs(bool DisableCollision)
  268. {
  269. m_disable_a2b_collision = DisableCollision;
  270. }
  271. private:
  272. EasyPhysic* m_a_physobj;
  273. EasyPhysic* m_b_physobj;
  274. lol::mat4 m_a_transform;
  275. lol::mat4 m_b_transform;
  276. bool m_using_ref_a;
  277. bool m_disable_a2b_collision;
  278. };
  279. } /* namespace phys */
  280. } /* namespace lol */
  281. #endif /* __EASYPHYSICS_EASYPHYSICS_H__ */