PhysicsBody.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright (C) 2009-present, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <AnKi/Physics/Common.h>
  7. #include <AnKi/Util/ClassWrapper.h>
  8. namespace anki {
  9. /// @addtogroup physics
  10. /// @{
  11. /// An interface to process contacts.
  12. /// @memberof PhysicsBody
  13. class PhysicsTriggerCallbacks
  14. {
  15. public:
  16. /// Will be called whenever a contact first touches a trigger.
  17. virtual void onTriggerEnter([[maybe_unused]] const PhysicsBody& trigger, [[maybe_unused]] const PhysicsObjectBase& obj)
  18. {
  19. }
  20. /// Will be called whenever a contact stops touching a trigger.
  21. virtual void onTriggerExit([[maybe_unused]] const PhysicsBody& trigger, [[maybe_unused]] const PhysicsObjectBase& obj)
  22. {
  23. }
  24. };
  25. /// An interface to process contacts.
  26. /// @memberof PhysicsBody
  27. class PhysicsCollisionFilterCallback
  28. {
  29. public:
  30. virtual Bool collidesWith(const PhysicsBody& body1, const PhysicsBody& body2) = 0;
  31. };
  32. /// Init info for PhysicsBody.
  33. class PhysicsBodyInitInfo
  34. {
  35. public:
  36. PhysicsCollisionShape* m_shape = nullptr;
  37. F32 m_mass = 0.0f; ///< Zero mass means static object.
  38. Transform m_transform = Transform::getIdentity();
  39. F32 m_friction = 0.5f;
  40. PhysicsLayer m_layer = PhysicsLayer::kStatic;
  41. Bool m_isTrigger = false;
  42. void* m_userData = nullptr;
  43. };
  44. /// Rigid body.
  45. class PhysicsBody : public PhysicsObjectBase
  46. {
  47. ANKI_PHYSICS_COMMON_FRIENDS
  48. friend class PhysicsBodyPtrDeleter;
  49. public:
  50. const Transform& getTransform(U32* version = nullptr) const
  51. {
  52. if(version)
  53. {
  54. *version = m_worldTrfVersion;
  55. }
  56. return m_worldTrf;
  57. }
  58. void setPositionAndRotation(Vec3 position, const Mat3& rotation);
  59. /// @param force In Newton and in world space.
  60. /// @param relPos The position to apply the force. It's in the local space of the body.
  61. void applyForce(const Vec3& force, const Vec3& relPos);
  62. /// Apply force to the center of mass.
  63. void applyForce(const Vec3& force);
  64. void activate(Bool activate);
  65. /// Zero means no gravity, 1 means normal gravity.
  66. void setGravityFactor(F32 factor);
  67. void setLinearVelocity(Vec3 v);
  68. void setAngularVelocity(Vec3 v);
  69. void clearForcesAndTorque();
  70. void setPhysicsTriggerCallbacks(PhysicsTriggerCallbacks* callbacks)
  71. {
  72. ANKI_ASSERT(m_isTrigger);
  73. m_triggerCallbacks = callbacks;
  74. }
  75. void setCollisionFilterCallback(PhysicsCollisionFilterCallback* callback);
  76. F32 getMass() const
  77. {
  78. return m_mass;
  79. }
  80. private:
  81. class MyGroupFilter final : public JPH::GroupFilter
  82. {
  83. public:
  84. Bool CanCollide(const JPH::CollisionGroup& inGroup1, const JPH::CollisionGroup& inGroup2) const override;
  85. };
  86. static MyGroupFilter m_groupFilter;
  87. JPH::Body* m_jphBody = nullptr;
  88. PhysicsCollisionShapePtr m_primaryShape;
  89. PhysicsCollisionShapePtr m_scaledShape;
  90. PhysicsTriggerCallbacks* m_triggerCallbacks = nullptr;
  91. PhysicsCollisionFilterCallback* m_collisionFilterCallback = nullptr;
  92. F32 m_mass = 0.0f;
  93. U32 m_worldTrfVersion : 30 = 1;
  94. U32 m_activated : 1 = false;
  95. U32 m_isTrigger : 1 = false;
  96. Transform m_worldTrf;
  97. PhysicsBody();
  98. ~PhysicsBody();
  99. void init(const PhysicsBodyInitInfo& init);
  100. void postPhysicsUpdate();
  101. };
  102. /// @}
  103. } // end namespace anki