PhysicsWorld.h 3.4 KB

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