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.
 
 
 

172 line
5.3 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. #endif
  23. namespace lol
  24. {
  25. namespace phys
  26. {
  27. class EasyPhysic
  28. {
  29. friend class EasyConstraint;
  30. #ifdef HAVE_PHYS_USE_BULLET
  31. public:
  32. EasyPhysic(WorldEntity* NewOwnerEntity);
  33. ~EasyPhysic();
  34. virtual void SetShapeToBox(lol::vec3& box_size);
  35. virtual void SetShapeToSphere(float radius);
  36. virtual void SetShapeToCone(float radius, float height);
  37. virtual void SetShapeToCylinder(lol::vec3& cyl_size);
  38. virtual void SetShapeToCapsule(float radius, float height);
  39. virtual bool CanChangeCollisionChannel() { return (m_rigid_body == NULL); }
  40. virtual void SetTransform(const lol::vec3& base_location, const lol::quat& base_rotation=lol::quat(lol::mat4(1.0f)));
  41. private:
  42. virtual void BaseTransformChanged(const lol::mat4& PreviousMatrix, const lol::mat4& NewMatrix);
  43. public:
  44. virtual void SetMass(float mass);
  45. virtual void InitBodyToRigid(bool ZeroMassIsKinematic=false);
  46. virtual void InitBodyToGhost();
  47. virtual void AddToSimulation(class Simulation* current_simulation);
  48. virtual void RemoveFromSimulation(class Simulation* current_simulation);
  49. virtual mat4 GetTransform();
  50. protected:
  51. virtual void SetLocalInertia(float mass);
  52. virtual void SetShapeTo(btCollisionShape* collision_shape);
  53. virtual btGhostObject* GetGhostObject();
  54. btCollisionObject* m_collision_object;
  55. btGhostObject* m_ghost_object;
  56. btRigidBody* m_rigid_body;
  57. btVector3 m_local_inertia;
  58. btCollisionShape* m_collision_shape;
  59. btConvexShape* m_convex_shape;
  60. btMotionState* m_motion_state;
  61. #else // NO PHYSIC IMPLEMENTATION
  62. public:
  63. EasyPhysic(WorldEntity* NewOwnerEntity) { m_owner_entity = NewOwnerEntity; }
  64. virtual void SetShapeToBox(lol::vec3& BoxSize) { }
  65. virtual void SetShapeToSphere(float radius) { }
  66. virtual void SetShapeToCone(float radius, float height) { }
  67. virtual void SetShapeToCylinder(lol::vec3& cyl_size) { }
  68. virtual void SetShapeToCapsule(float radius, float height) { }
  69. virtual bool CanChangeCollisionChannel() { return true; }
  70. virtual void SetTransform(const lol::vec3& base_location, const lol::quat& base_rotation=lol::quat(lol::mat4(1.0f))) { }
  71. private:
  72. virtual void BaseTransformChanged(const lol::mat4& PreviousMatrix, const lol::mat4& NewMatrix) { }
  73. public:
  74. virtual void SetMass(float mass) { }
  75. virtual void InitBodyToRigid() { }
  76. virtual void InitBodyToGhost() { }
  77. virtual void AddToSimulation(class Simulation* current_simulation) { }
  78. virtual void RemoveFromSimulation(class Simulation* current_simulation) { }
  79. virtual mat4 GetTransform() { return mat4(1.0f); }
  80. virtual void InitBodyToGhost() { }
  81. #endif // PHYSIC IMPLEMENTATION
  82. public:
  83. //Sets the collision Group & Mask.
  84. //Mask can change at runtime, not group !
  85. virtual bool SetCollisionChannel(int NewGroup, int NewMask)
  86. {
  87. if (CanChangeCollisionChannel())
  88. {
  89. m_collision_group = (1<<NewGroup);
  90. m_collision_mask = NewMask;
  91. return true;
  92. }
  93. return false;
  94. }
  95. int GetCollisionGroup() { return m_collision_group; }
  96. int GetCollisionMask() { return m_collision_mask; }
  97. //Base/Attachment logic
  98. virtual void AttachTo(EasyPhysic* NewBase, bool NewBaseLockLocation = true, bool NewBaseLockRotation = true)
  99. {
  100. if (NewBase == this || NewBase->m_base_physic == this)
  101. return;
  102. if (NewBase)
  103. {
  104. bool bAlreadyExists = false;
  105. for (int i = 0; i < NewBase->m_based_physic_list.Count(); ++i)
  106. if (NewBase->m_based_physic_list[i] == this)
  107. bAlreadyExists = true;
  108. if (!bAlreadyExists)
  109. NewBase->m_based_physic_list << this;
  110. m_base_physic = NewBase;
  111. m_base_lock_location = NewBaseLockLocation;
  112. m_base_lock_rotation = NewBaseLockRotation;
  113. }
  114. else
  115. {
  116. for (int i = 0; i < NewBase->m_based_physic_list.Count(); ++i)
  117. if (NewBase->m_based_physic_list[i] == this)
  118. NewBase->m_based_physic_list.Remove(i--);
  119. m_base_physic = NULL;
  120. }
  121. }
  122. protected:
  123. lol::mat4 m_local_to_world;
  124. float m_mass;
  125. int m_collision_group;
  126. int m_collision_mask;
  127. WorldEntity* m_owner_entity;
  128. //Base/Attachment logic
  129. Array<EasyPhysic*> m_based_physic_list; //List of objects based on this : this object moves, its based object move with it.
  130. EasyPhysic* m_base_physic; //Base for this object : The base moves, the object moves with it.
  131. bool m_base_lock_location; //when this is TRUE, location moves with rotation change.
  132. bool m_base_lock_rotation; //when this is TRUE, rotation moves with rotation change.
  133. //Touch logic
  134. Array<EasyPhysic*> m_touching_physic; //Maintained by ghost objects
  135. };
  136. } /* namespace phys */
  137. } /* namespace lol */
  138. #endif /* __EASYPHYSICS_EASYPHYSICS_H__ */