Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 

293 Zeilen
8.6 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. #endif
  22. namespace lol
  23. {
  24. namespace phys
  25. {
  26. class EasyPhysic
  27. {
  28. friend class EasyConstraint;
  29. #ifdef HAVE_PHYS_USE_BULLET
  30. public:
  31. EasyPhysic();
  32. ~EasyPhysic();
  33. void SetShapeToBox(lol::vec3& box_size);
  34. void SetShapeToSphere(float radius);
  35. void SetShapeToCone(float radius, float height);
  36. void SetShapeToCylinder(lol::vec3& cyl_size);
  37. void SetShapeToCapsule(float radius, float height);
  38. bool CanChangeCollisionChannel() { return (m_rigid_body == NULL); }
  39. void SetTransform(const lol::vec3& base_location, const lol::quat& base_rotation=lol::quat(lol::mat4(1.0f)));
  40. void SetMass(float mass);
  41. void InitBodyToRigid(bool ZeroMassIsKinematic=false);
  42. void AddToSimulation(class Simulation* current_simulation);
  43. void RemoveFromSimulation(class Simulation* current_simulation);
  44. mat4 GetTransform();
  45. protected:
  46. void SetLocalInertia(float mass);
  47. void SetShapeTo(btCollisionShape* collision_shape);
  48. btCollisionObject* m_collision_object;
  49. btRigidBody* m_rigid_body;
  50. btVector3 m_local_inertia;
  51. btCollisionShape* m_collision_shape;
  52. btMotionState* m_motion_state;
  53. #else // NO PHYSIC IMPLEMENTATION
  54. public:
  55. EasyPhysic() { }
  56. void SetShapeToBox(lol::vec3& BoxSize) { }
  57. void SetShapeToSphere(float radius) { }
  58. void SetShapeToCone(float radius, float height) { }
  59. void SetShapeToCylinder(lol::vec3& cyl_size) { }
  60. void SetShapeToCapsule(float radius, float height) { }
  61. bool CanChangeCollisionChannel() { return true; }
  62. void SetTransform(const lol::vec3& base_location, const lol::quat& base_rotation=lol::quat(lol::mat4(1.0f))) { }
  63. void SetMass(float mass) { }
  64. void InitBodyToRigid() { }
  65. void AddToSimulation(class Simulation* current_simulation) { }
  66. void RemoveFromSimulation(class Simulation* current_simulation) { }
  67. mat4 GetTransform() { return mat4(1.0f); }
  68. #endif // PHYSIC IMPLEMENTATION
  69. public:
  70. //Sets the collision Group & Mask.
  71. //Mask can change at runtime, not group !
  72. bool SetCollisionChannel(int NewGroup, int NewMask)
  73. {
  74. if (CanChangeCollisionChannel())
  75. {
  76. m_collision_group = (1<<NewGroup);
  77. m_collision_mask = NewMask;
  78. return true;
  79. }
  80. return false;
  81. }
  82. int GetCollisionGroup() { return m_collision_group; }
  83. int GetCollisionMask() { return m_collision_mask; }
  84. protected:
  85. lol::mat4 m_local_to_world;
  86. float m_mass;
  87. int m_collision_group;
  88. int m_collision_mask;
  89. };
  90. class EasyConstraint
  91. {
  92. #ifdef HAVE_PHYS_USE_BULLET
  93. public:
  94. EasyConstraint() :
  95. m_typed_constraint(NULL),
  96. m_p2p_constraint(NULL),
  97. m_hinge_constraint(NULL),
  98. m_slider_constraint(NULL),
  99. m_cone_twist_constraint(NULL),
  100. m_6dof_constraint(NULL),
  101. m_a_physobj(NULL),
  102. m_b_physobj(NULL),
  103. m_a_transform(lol::mat4(1.f)),
  104. m_b_transform(lol::mat4(1.f)),
  105. m_using_ref_a(false),
  106. m_disable_a2b_collision(false)
  107. {
  108. }
  109. ~EasyConstraint()
  110. {
  111. delete m_typed_constraint;
  112. m_p2p_constraint = NULL;
  113. m_hinge_constraint = NULL;
  114. m_slider_constraint = NULL;
  115. m_cone_twist_constraint = NULL;
  116. m_6dof_constraint = NULL;
  117. }
  118. void AddToSimulation(class Simulation* current_simulation);
  119. void RemoveFromSimulation(class Simulation* current_simulation);
  120. private:
  121. //check if Init can be done
  122. bool CanProceedWithInit()
  123. {
  124. if (!m_a_physobj || !m_b_physobj)
  125. return false;
  126. if (!m_a_physobj->m_rigid_body || !m_b_physobj->m_rigid_body)
  127. return false;
  128. return true;
  129. }
  130. //-------------------------------------------------------------------------
  131. //Init constraint functions
  132. //--
  133. void CustomInitConstraintToPoint2Point()
  134. {
  135. m_p2p_constraint = new btPoint2PointConstraint(*m_a_physobj->m_rigid_body, *m_b_physobj->m_rigid_body,
  136. LOL2BT_VEC3(m_a_transform.v3.xyz * LOL2BT_UNIT), LOL2BT_VEC3(m_b_transform.v3.xyz * LOL2BT_UNIT));
  137. m_typed_constraint = m_p2p_constraint;
  138. }
  139. void CustomInitConstraintToHinge()
  140. {
  141. m_hinge_constraint = new btHingeConstraint(*m_a_physobj->m_rigid_body, *m_b_physobj->m_rigid_body,
  142. btTransform(LOL2BT_QUAT(quat(m_a_transform)), LOL2BT_VEC3(m_a_transform.v3.xyz * LOL2BT_UNIT)),
  143. btTransform(LOL2BT_QUAT(quat(m_b_transform)), LOL2BT_VEC3(m_b_transform.v3.xyz * LOL2BT_UNIT)),
  144. m_using_ref_a);
  145. m_typed_constraint = m_hinge_constraint;
  146. }
  147. void CustomInitConstraintToSlider()
  148. {
  149. m_slider_constraint = new btSliderConstraint(*m_a_physobj->m_rigid_body, *m_b_physobj->m_rigid_body,
  150. btTransform(LOL2BT_QUAT(quat(m_a_transform)), LOL2BT_VEC3(m_a_transform.v3.xyz * LOL2BT_UNIT)),
  151. btTransform(LOL2BT_QUAT(quat(m_b_transform)), LOL2BT_VEC3(m_b_transform.v3.xyz * LOL2BT_UNIT)),
  152. m_using_ref_a);
  153. m_typed_constraint = m_slider_constraint;
  154. }
  155. void CustomInitConstraintToConeTwist()
  156. {
  157. m_cone_twist_constraint = new btConeTwistConstraint(*m_a_physobj->m_rigid_body, *m_b_physobj->m_rigid_body,
  158. btTransform(LOL2BT_QUAT(quat(m_a_transform)), LOL2BT_VEC3(m_a_transform.v3.xyz * LOL2BT_UNIT)),
  159. btTransform(LOL2BT_QUAT(quat(m_b_transform)), LOL2BT_VEC3(m_b_transform.v3.xyz * LOL2BT_UNIT)));
  160. m_typed_constraint = m_cone_twist_constraint;
  161. }
  162. void CustomInitConstraintTo6Dof()
  163. {
  164. m_6dof_constraint = new btGeneric6DofConstraint(*m_a_physobj->m_rigid_body, *m_b_physobj->m_rigid_body,
  165. btTransform(LOL2BT_QUAT(quat(m_a_transform)), LOL2BT_VEC3(m_a_transform.v3.xyz * LOL2BT_UNIT)),
  166. btTransform(LOL2BT_QUAT(quat(m_b_transform)), LOL2BT_VEC3(m_b_transform.v3.xyz * LOL2BT_UNIT)),
  167. m_using_ref_a);
  168. m_typed_constraint = m_6dof_constraint;
  169. }
  170. btTypedConstraint* m_typed_constraint;
  171. btPoint2PointConstraint* m_p2p_constraint;
  172. btHingeConstraint* m_hinge_constraint;
  173. btSliderConstraint* m_slider_constraint;
  174. btConeTwistConstraint* m_cone_twist_constraint;
  175. btGeneric6DofConstraint* m_6dof_constraint;
  176. #else // NO PHYSIC IMPLEMENTATION
  177. public:
  178. EasyConstraint() :
  179. m_a_physobj(NULL),
  180. m_b_physobj(NULL),
  181. m_a_transform(lol::mat4(1.f)),
  182. m_b_transform(lol::mat4(1.f)),
  183. m_using_ref_a(false),
  184. m_disable_a2b_collision(false)
  185. {
  186. }
  187. private:
  188. void AddToSimulation(class Simulation* current_simulation) { }
  189. void RemoveFromSimulation(class Simulation* current_simulation) { }
  190. //check if Init can be done
  191. bool CanProceedWithInit() { return false; }
  192. void CustomInitConstraintToPoint2Point() { }
  193. void CustomInitConstraintToHinge() { }
  194. void CustomInitConstraintToSlider() { }
  195. void CustomInitConstraintToConeTwist() { }
  196. void CustomInitConstraintTo6Dof() { }
  197. #endif // PHYSIC IMPLEMENTATION
  198. public:
  199. void InitConstraintToPoint2Point() { if (CanProceedWithInit()) CustomInitConstraintToPoint2Point(); }
  200. void InitConstraintToHinge() { if (CanProceedWithInit()) CustomInitConstraintToHinge(); }
  201. void InitConstraintToSlider() { if (CanProceedWithInit()) CustomInitConstraintToSlider(); }
  202. void InitConstraintToConeTwist() { if (CanProceedWithInit()) CustomInitConstraintToConeTwist(); }
  203. void InitConstraintTo6Dof() { if (CanProceedWithInit()) CustomInitConstraintTo6Dof(); }
  204. //Set given physic object to the proper slot.
  205. void SetPhysObjA(EasyPhysic* NewPhysObj, lol::mat4 NewTransform) { SetPhysObj(false, NewPhysObj, NewTransform); }
  206. void SetPhysObjB(EasyPhysic* NewPhysObj, lol::mat4 NewTransform) { SetPhysObj(true, NewPhysObj, NewTransform); }
  207. void SetPhysObj(bool SetToB, EasyPhysic* NewPhysObj, lol::mat4 NewTransform)
  208. {
  209. if (SetToB)
  210. {
  211. m_b_physobj = NewPhysObj;
  212. m_b_transform = NewTransform;
  213. }
  214. else
  215. {
  216. m_a_physobj = NewPhysObj;
  217. m_a_transform = NewTransform;
  218. }
  219. }
  220. //Set whether or not the physic engine should use the A object as the reference (most constraint transform are local).
  221. void SetRefAsA(bool NewUseRefA)
  222. {
  223. m_using_ref_a = NewUseRefA;
  224. }
  225. //Set whether or not to disable the collision between the bodies
  226. void DisableCollisionBetweenObjs(bool DisableCollision)
  227. {
  228. m_disable_a2b_collision = DisableCollision;
  229. }
  230. private:
  231. EasyPhysic* m_a_physobj;
  232. EasyPhysic* m_b_physobj;
  233. lol::mat4 m_a_transform;
  234. lol::mat4 m_b_transform;
  235. bool m_using_ref_a;
  236. bool m_disable_a2b_collision;
  237. };
  238. } /* namespace phys */
  239. } /* namespace lol */
  240. #endif /* __EASYPHYSICS_EASYPHYSICS_H__ */