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.

161 lines
5.2 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright © 2010—2020 Sam Hocevar <sam@hocevar.net>
  5. // © 2009—2013 Benjamin “Touky” Huet <huet.benjamin@gmail.com>
  6. //
  7. // This library is free software. It comes without any warranty, to
  8. // the extent permitted by applicable law. You can redistribute it
  9. // and/or modify it under the terms of the Do What the Fuck You Want
  10. // to Public License, Version 2, as published by the WTFPL Task Force.
  11. // See http://www.wtfpl.net/ for more details.
  12. //
  13. #pragma once
  14. //
  15. // The EasyPhysic class
  16. // ------------------
  17. //
  18. #include <lol/engine.h>
  19. #include <vector>
  20. #include <btBulletDynamicsCommon.h>
  21. #include <btBulletCollisionCommon.h>
  22. #include <BulletCollision/CollisionDispatch/btGhostObject.h>
  23. namespace lol
  24. {
  25. namespace phys
  26. {
  27. class EasyPhysic
  28. {
  29. friend class Simulation;
  30. friend class EasyConstraint;
  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 == nullptr); }
  40. virtual mat4 GetTransform();
  41. virtual void SetTransform(const vec3& base_location, const quat& base_rotation = quat(mat3(1.0f)));
  42. protected:
  43. virtual void BaseTransformChanged(const mat4& PreviousMatrix, const mat4& NewMatrix);
  44. public:
  45. virtual void SetMass(float mass);
  46. virtual float GetMass() { return m_mass; }
  47. virtual void SetHitRestitution(float hit_restitution);
  48. virtual void InitBodyToRigid(bool ZeroMassIsKinematic=false);
  49. virtual void InitBodyToGhost();
  50. virtual void AddToSimulation(class Simulation* current_simulation);
  51. virtual void RemoveFromSimulation(class Simulation* current_simulation);
  52. //Force/Impulse functions
  53. virtual void AddImpulse(const lol::vec3& impulse);
  54. virtual void AddImpulse(const lol::vec3& impulse, const lol::vec3& rel_pos);
  55. virtual void AddImpulseTorque(const lol::vec3& torque);
  56. virtual void AddForce(const lol::vec3& force);
  57. virtual void AddForce(const lol::vec3& force, const lol::vec3& rel_pos);
  58. virtual void AddForceTorque(const lol::vec3& torque);
  59. //Movements getter
  60. lol::vec3 GetLinearVelocity() const;
  61. lol::vec3 GetLinearForce() const;
  62. lol::vec3 GetAngularVelocity() const;
  63. lol::vec3 GetAngularForce() const;
  64. protected:
  65. virtual void SetLocalInertia(float mass);
  66. virtual void SetShapeTo(btCollisionShape* collision_shape);
  67. virtual btGhostObject* GetGhostObjectInstance();
  68. btCollisionObject *m_collision_object;
  69. btGhostObject *m_ghost_object;
  70. btRigidBody *m_rigid_body;
  71. btVector3 m_local_inertia;
  72. btCollisionShape *m_collision_shape;
  73. btConvexShape *m_convex_shape;
  74. btMotionState *m_motion_state;
  75. public:
  76. //Sets the collision Group & Mask.
  77. //Mask can change at runtime, not group !
  78. virtual bool SetCollisionChannel(int NewGroup, int NewMask)
  79. {
  80. if (CanChangeCollisionChannel())
  81. {
  82. m_collision_group = (1 << NewGroup);
  83. m_collision_mask = NewMask;
  84. return true;
  85. }
  86. return false;
  87. }
  88. int GetCollisionGroup() { return m_collision_group; }
  89. int GetCollisionMask() { return m_collision_mask; }
  90. //Base/Attachment logic
  91. virtual void AttachTo(EasyPhysic* NewBase, bool NewBaseLockLocation = true, bool NewBaseLockRotation = true)
  92. {
  93. if (NewBase == this || (NewBase && NewBase->m_base_physic == this))
  94. return;
  95. if (NewBase)
  96. {
  97. bool bAlreadyExists = false;
  98. for (size_t i = 0; i < NewBase->m_based_physic_list.size(); ++i)
  99. if (NewBase->m_based_physic_list[i] == this)
  100. bAlreadyExists = true;
  101. if (!bAlreadyExists)
  102. NewBase->m_based_physic_list.push_back(this);
  103. m_base_physic = NewBase;
  104. m_base_lock_location = NewBaseLockLocation;
  105. m_base_lock_rotation = NewBaseLockRotation;
  106. }
  107. else if (m_base_physic)
  108. {
  109. for (size_t i = 0; i < m_base_physic->m_based_physic_list.size(); ++i)
  110. if (m_base_physic->m_based_physic_list[i] == this)
  111. remove_at(m_base_physic->m_based_physic_list, i--);
  112. m_base_physic = nullptr;
  113. }
  114. }
  115. protected:
  116. lol::mat4 m_local_to_world;
  117. float m_mass;
  118. float m_hit_restitution;
  119. int m_collision_group;
  120. int m_collision_mask;
  121. WorldEntity *m_owner_entity;
  122. Simulation *m_owner_simulation;
  123. //Base/Attachment logic
  124. std::vector<EasyPhysic*> m_based_physic_list; //List of objects based on this : this object moves, its based object MoveStep with it.
  125. EasyPhysic *m_base_physic; //Base for this object : The base moves, the object moves with it.
  126. bool m_base_lock_location; //when this is TRUE, location moves with rotation change.
  127. bool m_base_lock_rotation; //when this is TRUE, rotation moves with rotation change.
  128. //Touch logic
  129. std::vector<EasyPhysic*> m_touching_physic; //Maintained by ghost objects
  130. };
  131. } /* namespace phys */
  132. } /* namespace lol */