PhysicsWorld.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Copyright (C) 2009-2021, 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/Physics/PhysicsObject.h>
  8. #include <AnKi/Util/List.h>
  9. #include <AnKi/Util/WeakArray.h>
  10. #include <AnKi/Util/ClassWrapper.h>
  11. namespace anki {
  12. /// @addtogroup physics
  13. /// @{
  14. /// Raycast callback (interface).
  15. class PhysicsWorldRayCastCallback
  16. {
  17. public:
  18. Vec3 m_from;
  19. Vec3 m_to;
  20. PhysicsMaterialBit m_materialMask; ///< Materials to check
  21. Bool m_firstHit = true;
  22. PhysicsWorldRayCastCallback(const Vec3& from, const Vec3& to, PhysicsMaterialBit materialMask)
  23. : m_from(from)
  24. , m_to(to)
  25. , m_materialMask(materialMask)
  26. {
  27. }
  28. /// Process a raycast result.
  29. virtual void processResult(PhysicsFilteredObject& obj, const Vec3& worldNormal, const Vec3& worldPosition) = 0;
  30. };
  31. /// The master container for all physics related stuff.
  32. class PhysicsWorld
  33. {
  34. public:
  35. PhysicsWorld();
  36. ~PhysicsWorld();
  37. ANKI_USE_RESULT Error init(AllocAlignedCallback allocCb, void* allocCbData);
  38. template<typename T, typename... TArgs>
  39. PhysicsPtr<T> newInstance(TArgs&&... args)
  40. {
  41. T* obj = static_cast<T*>(m_alloc.getMemoryPool().allocate(sizeof(T), alignof(T)));
  42. ::new(obj) T(this, std::forward<TArgs>(args)...);
  43. {
  44. LockGuard<Mutex> lock(m_markedMtx);
  45. m_markedForCreation.pushBack(obj);
  46. }
  47. #if ANKI_ENABLE_ASSERTIONS
  48. const U32 count = m_objectsCreatedCount.fetchAdd(1) + 1;
  49. ANKI_ASSERT(count > 0);
  50. #endif
  51. return PhysicsPtr<T>(obj);
  52. }
  53. /// Do the update.
  54. Error update(Second dt);
  55. HeapAllocator<U8> getAllocator() const
  56. {
  57. return m_alloc;
  58. }
  59. HeapAllocator<U8>& getAllocator()
  60. {
  61. return m_alloc;
  62. }
  63. StackAllocator<U8> getTempAllocator() const
  64. {
  65. return m_tmpAlloc;
  66. }
  67. StackAllocator<U8>& getTempAllocator()
  68. {
  69. return m_tmpAlloc;
  70. }
  71. void rayCast(WeakArray<PhysicsWorldRayCastCallback*> rayCasts) const;
  72. void rayCast(PhysicsWorldRayCastCallback& raycast) const
  73. {
  74. PhysicsWorldRayCastCallback* ptr = &raycast;
  75. WeakArray<PhysicsWorldRayCastCallback*> arr(&ptr, 1);
  76. rayCast(arr);
  77. }
  78. ANKI_INTERNAL btDynamicsWorld& getBtWorld()
  79. {
  80. return *m_world;
  81. }
  82. ANKI_INTERNAL const btDynamicsWorld& getBtWorld() const
  83. {
  84. return *m_world;
  85. }
  86. ANKI_INTERNAL constexpr F32 getCollisionMargin() const
  87. {
  88. return 0.04f;
  89. }
  90. ANKI_INTERNAL void destroyObject(PhysicsObject* obj);
  91. ANKI_INTERNAL PhysicsTriggerFilteredPair*
  92. getOrCreatePhysicsTriggerFilteredPair(PhysicsTrigger* trigger, PhysicsFilteredObject* filtered, Bool& isNew);
  93. private:
  94. class MyOverlapFilterCallback;
  95. class MyRaycastCallback;
  96. HeapAllocator<U8> m_alloc;
  97. StackAllocator<U8> m_tmpAlloc;
  98. ClassWrapper<btDbvtBroadphase> m_broadphase;
  99. ClassWrapper<btGhostPairCallback> m_gpc;
  100. MyOverlapFilterCallback* m_filterCallback = nullptr;
  101. ClassWrapper<btDefaultCollisionConfiguration> m_collisionConfig;
  102. ClassWrapper<btCollisionDispatcher> m_dispatcher;
  103. ClassWrapper<btSequentialImpulseConstraintSolver> m_solver;
  104. ClassWrapper<btDiscreteDynamicsWorld> m_world;
  105. Array<IntrusiveList<PhysicsObject>, U(PhysicsObjectType::COUNT)> m_objectLists;
  106. IntrusiveList<PhysicsObject> m_markedForCreation;
  107. IntrusiveList<PhysicsObject> m_markedForDeletion;
  108. Mutex m_markedMtx; ///< Locks the above
  109. #if ANKI_ENABLE_ASSERTIONS
  110. Atomic<I32> m_objectsCreatedCount = {0};
  111. #endif
  112. void destroyMarkedForDeletion();
  113. };
  114. /// @}
  115. } // end namespace anki