Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

easyphysics.h 6.3 KiB

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