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.

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