PhysicsObject.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // Copyright (C) 2009-2020, 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/List.h>
  8. namespace anki
  9. {
  10. /// @addtogroup physics
  11. /// @{
  12. /// Type of the physics object.
  13. enum class PhysicsObjectType : U8
  14. {
  15. COLLISION_SHAPE,
  16. JOINT,
  17. BODY,
  18. PLAYER_CONTROLLER,
  19. TRIGGER,
  20. COUNT,
  21. FIRST = 0,
  22. LAST = COUNT - 1,
  23. FIRST_FILTERED = BODY,
  24. LAST_FILTERED = TRIGGER,
  25. };
  26. ANKI_ENUM_ALLOW_NUMERIC_OPERATIONS(PhysicsObjectType)
  27. /// Base of all physics objects.
  28. class PhysicsObject : public IntrusiveListEnabled<PhysicsObject>
  29. {
  30. public:
  31. PhysicsObject(PhysicsObjectType type, PhysicsWorld* world)
  32. : m_world(world)
  33. , m_type(type)
  34. {
  35. ANKI_ASSERT(m_world);
  36. }
  37. virtual ~PhysicsObject()
  38. {
  39. }
  40. PhysicsObjectType getType() const
  41. {
  42. return m_type;
  43. }
  44. PhysicsWorld& getWorld()
  45. {
  46. return *m_world;
  47. }
  48. const PhysicsWorld& getWorld() const
  49. {
  50. return *m_world;
  51. }
  52. Atomic<I32>& getRefcount()
  53. {
  54. return m_refcount;
  55. }
  56. HeapAllocator<U8> getAllocator() const;
  57. void setUserData(void* ud)
  58. {
  59. m_userData = ud;
  60. }
  61. void* getUserData() const
  62. {
  63. return m_userData;
  64. }
  65. protected:
  66. PhysicsWorld* m_world = nullptr;
  67. private:
  68. Atomic<I32> m_refcount = {0};
  69. PhysicsObjectType m_type;
  70. void* m_userData = nullptr;
  71. };
  72. #define ANKI_PHYSICS_OBJECT \
  73. friend class PhysicsWorld; \
  74. friend class PhysicsPtrDeleter;
  75. /// This is a factor that will decide if two filtered objects will be checked for collision.
  76. /// @memberof PhysicsFilteredObject
  77. class PhysicsBroadPhaseFilterCallback
  78. {
  79. public:
  80. virtual Bool needsCollision(const PhysicsFilteredObject& a, const PhysicsFilteredObject& b) = 0;
  81. };
  82. /// A PhysicsObject that takes part into collision detection. Has functionality to filter the broad phase detection.
  83. class PhysicsFilteredObject : public PhysicsObject
  84. {
  85. public:
  86. PhysicsFilteredObject(PhysicsObjectType type, PhysicsWorld* world)
  87. : PhysicsObject(type, world)
  88. {
  89. }
  90. ~PhysicsFilteredObject()
  91. {
  92. }
  93. static Bool classof(const PhysicsObject* obj)
  94. {
  95. return obj->getType() >= PhysicsObjectType::FIRST_FILTERED
  96. && obj->getType() <= PhysicsObjectType::LAST_FILTERED;
  97. }
  98. /// Get the material(s) this object belongs.
  99. PhysicsMaterialBit getMaterialGroup() const
  100. {
  101. return m_materialGroup;
  102. }
  103. /// Set the material(s) this object belongs.
  104. void setMaterialGroup(PhysicsMaterialBit bits)
  105. {
  106. m_materialGroup = bits;
  107. }
  108. /// Get the materials this object collides.
  109. PhysicsMaterialBit getMaterialMask() const
  110. {
  111. return m_materialMask;
  112. }
  113. /// Set the materials this object collides.
  114. void setMaterialMask(PhysicsMaterialBit bit)
  115. {
  116. m_materialMask = bit;
  117. }
  118. /// Get the broadphase callback.
  119. PhysicsBroadPhaseFilterCallback* getPhysicsBroadPhaseFilterCallback() const
  120. {
  121. return m_filter;
  122. }
  123. /// Set the broadphase callback.
  124. void setPhysicsBroadPhaseFilterCallback(PhysicsBroadPhaseFilterCallback* f)
  125. {
  126. m_filter = f;
  127. }
  128. private:
  129. PhysicsMaterialBit m_materialGroup = PhysicsMaterialBit::ALL;
  130. PhysicsMaterialBit m_materialMask = PhysicsMaterialBit::ALL;
  131. PhysicsBroadPhaseFilterCallback* m_filter = nullptr;
  132. };
  133. /// @}
  134. } // end namespace anki