Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

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