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.

easyphysics.h 5.9 KiB

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