You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

207 lines
7.9 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2013 Sam Hocevar <sam@hocevar.net>
  5. // (c) 2009-2013 Benjamin "Touky" 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://www.wtfpl.net/ 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. #endif //HAVE_PHYS_USE_BULLET
  23. namespace lol
  24. {
  25. namespace phys
  26. {
  27. class EasyPhysic
  28. {
  29. friend class Simulation;
  30. friend class EasyConstraint;
  31. #ifdef HAVE_PHYS_USE_BULLET
  32. public:
  33. EasyPhysic(WorldEntity* NewOwnerEntity);
  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 mat4 GetTransform();
  42. virtual void SetTransform(const lol::vec3& base_location, const lol::quat& base_rotation=lol::quat(lol::mat4(1.0f)));
  43. protected:
  44. virtual void BaseTransformChanged(const lol::mat4& PreviousMatrix, const lol::mat4& NewMatrix);
  45. public:
  46. virtual void SetMass(float mass);
  47. virtual float GetMass() { return m_mass; }
  48. virtual void SetHitRestitution(float hit_restitution);
  49. virtual void InitBodyToRigid(bool ZeroMassIsKinematic=false);
  50. virtual void InitBodyToGhost();
  51. virtual void AddToSimulation(class Simulation* current_simulation);
  52. virtual void RemoveFromSimulation(class Simulation* current_simulation);
  53. //Force/Impulse functions
  54. virtual void AddImpulse(const lol::vec3& impulse);
  55. virtual void AddImpulse(const lol::vec3& impulse, const lol::vec3& rel_pos);
  56. virtual void AddImpulseTorque(const lol::vec3& torque);
  57. virtual void AddForce(const lol::vec3& force);
  58. virtual void AddForce(const lol::vec3& force, const lol::vec3& rel_pos);
  59. virtual void AddForceTorque(const lol::vec3& torque);
  60. //Movements getter
  61. lol::vec3 GetLinearVelocity() const;
  62. lol::vec3 GetLinearForce() const;
  63. lol::vec3 GetAngularVelocity() const;
  64. lol::vec3 GetAngularForce() const;
  65. protected:
  66. virtual void SetLocalInertia(float mass);
  67. virtual void SetShapeTo(btCollisionShape* collision_shape);
  68. virtual btGhostObject* GetGhostObjectInstance();
  69. btCollisionObject* m_collision_object;
  70. btGhostObject* m_ghost_object;
  71. btRigidBody* m_rigid_body;
  72. btVector3 m_local_inertia;
  73. btCollisionShape* m_collision_shape;
  74. btConvexShape* m_convex_shape;
  75. btMotionState* m_motion_state;
  76. #else // NO PHYSIC IMPLEMENTATION
  77. public:
  78. EasyPhysic(WorldEntity* NewOwnerEntity) { m_owner_entity = NewOwnerEntity; }
  79. virtual void SetShapeToBox(lol::vec3& BoxSize) { }
  80. virtual void SetShapeToSphere(float radius) { }
  81. virtual void SetShapeToCone(float radius, float height) { }
  82. virtual void SetShapeToCylinder(lol::vec3& cyl_size) { }
  83. virtual void SetShapeToCapsule(float radius, float height) { }
  84. virtual bool CanChangeCollisionChannel() { return true; }
  85. virtual mat4 GetTransform() { return mat4(1.0f); }
  86. virtual void SetTransform(const lol::vec3& base_location, const lol::quat& base_rotation=lol::quat(lol::mat4(1.0f))) { }
  87. private:
  88. virtual void BaseTransformChanged(const lol::mat4& PreviousMatrix, const lol::mat4& NewMatrix) { }
  89. public:
  90. virtual void SetMass(float mass) { }
  91. virtual float GetMass() { return .0f; }
  92. virtual void SetHitRestitution(float hit_restitution) { }
  93. virtual void InitBodyToRigid() { }
  94. virtual void InitBodyToGhost() { }
  95. virtual void AddToSimulation(class Simulation* current_simulation) { }
  96. virtual void RemoveFromSimulation(class Simulation* current_simulation) { }
  97. //Force/Impulse functions
  98. virtual void AddImpulse(const lol::vec3& impulse) { }
  99. virtual void AddImpulse(const lol::vec3& impulse, const lol::vec3& rel_pos) { }
  100. virtual void AddImpulseTorque(const lol::vec3& torque) { }
  101. virtual void AddForce(const lol::vec3& force) { }
  102. virtual void AddForce(const lol::vec3& force, const lol::vec3& rel_pos) { }
  103. virtual void AddForceTorque(const lol::vec3& torque) { }
  104. //Movements getter
  105. lol::vec3 GetLinearVelocity() const { return lol::vec3(.0f); }
  106. lol::vec3 GetLinearForce() const { return lol::vec3(.0f); }
  107. lol::vec3 GetAngularVelocity() const { return lol::vec3(.0f); }
  108. lol::vec3 GetAngularForce() const { return lol::vec3(.0f); }
  109. virtual void InitBodyToGhost() { }
  110. #endif // PHYSIC IMPLEMENTATION
  111. public:
  112. //Sets the collision Group & Mask.
  113. //Mask can change at runtime, not group !
  114. virtual bool SetCollisionChannel(int NewGroup, int NewMask)
  115. {
  116. if (CanChangeCollisionChannel())
  117. {
  118. m_collision_group = (1 << NewGroup);
  119. m_collision_mask = NewMask;
  120. return true;
  121. }
  122. return false;
  123. }
  124. int GetCollisionGroup() { return m_collision_group; }
  125. int GetCollisionMask() { return m_collision_mask; }
  126. //Base/Attachment logic
  127. virtual void AttachTo(EasyPhysic* NewBase, bool NewBaseLockLocation = true, bool NewBaseLockRotation = true)
  128. {
  129. if (NewBase == this || (NewBase && NewBase->m_base_physic == this))
  130. return;
  131. if (NewBase)
  132. {
  133. bool bAlreadyExists = false;
  134. for (int i = 0; i < NewBase->m_based_physic_list.Count(); ++i)
  135. if (NewBase->m_based_physic_list[i] == this)
  136. bAlreadyExists = true;
  137. if (!bAlreadyExists)
  138. NewBase->m_based_physic_list << this;
  139. m_base_physic = NewBase;
  140. m_base_lock_location = NewBaseLockLocation;
  141. m_base_lock_rotation = NewBaseLockRotation;
  142. }
  143. else if (m_base_physic)
  144. {
  145. for (int i = 0; i < m_base_physic->m_based_physic_list.Count(); ++i)
  146. if (m_base_physic->m_based_physic_list[i] == this)
  147. m_base_physic->m_based_physic_list.Remove(i--);
  148. m_base_physic = NULL;
  149. }
  150. }
  151. protected:
  152. lol::mat4 m_local_to_world;
  153. float m_mass;
  154. float m_hit_restitution;
  155. int m_collision_group;
  156. int m_collision_mask;
  157. WorldEntity* m_owner_entity;
  158. Simulation* m_owner_simulation;
  159. //Base/Attachment logic
  160. Array<EasyPhysic*> m_based_physic_list; //List of objects based on this : this object moves, its based object MoveStep with it.
  161. EasyPhysic* m_base_physic; //Base for this object : The base moves, the object moves with it.
  162. bool m_base_lock_location; //when this is TRUE, location moves with rotation change.
  163. bool m_base_lock_rotation; //when this is TRUE, rotation moves with rotation change.
  164. //Touch logic
  165. Array<EasyPhysic*> m_touching_physic; //Maintained by ghost objects
  166. };
  167. } /* namespace phys */
  168. } /* namespace lol */
  169. #endif /* __EASYPHYSICS_EASYPHYSICS_H__ */