PhysicsWorld.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Copyright (C) 2009-2015, Panagiotis Christopoulos Charitos.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #ifndef ANKI_PHYSICS_PHYSICS_WORLD_H
  6. #define ANKI_PHYSICS_PHYSICS_WORLD_H
  7. #include "anki/physics/Common.h"
  8. #include "anki/physics/PhysicsCollisionShape.h"
  9. #include "anki/physics/PhysicsBody.h"
  10. #include "anki/physics/PhysicsPlayerController.h"
  11. #include "anki/util/List.h"
  12. namespace anki {
  13. /// @addtogroup physics
  14. /// @{
  15. /// The master container for all physics related stuff.
  16. class PhysicsWorld
  17. {
  18. public:
  19. PhysicsWorld();
  20. ~PhysicsWorld();
  21. ANKI_USE_RESULT Error create(
  22. AllocAlignedCallback allocCb, void* allocCbData);
  23. template<typename T, typename... TArgs>
  24. T* newCollisionShape(TArgs&&... args)
  25. {
  26. return newObjectInternal<T>(m_collisions, std::forward<TArgs>(args)...);
  27. }
  28. template<typename T, typename... TArgs>
  29. T* newBody(TArgs&&... args)
  30. {
  31. return newObjectInternal<T>(m_bodies, std::forward<TArgs>(args)...);
  32. }
  33. PhysicsPlayerController* newPlayerController(
  34. const PhysicsPlayerController::Initializer& init)
  35. {
  36. return newObjectInternal<PhysicsPlayerController>(
  37. m_playerControllers, init);
  38. }
  39. /// Start asynchronous update.
  40. Error updateAsync(F32 dt);
  41. /// End asynchronous update.
  42. void waitUpdate();
  43. /// @privatesection
  44. /// @{
  45. NewtonWorld* _getNewtonWorld() const
  46. {
  47. ANKI_ASSERT(m_world);
  48. return m_world;
  49. }
  50. void _increaseObjectsMarkedForDeletion(PhysicsObject::Type type);
  51. const Vec4& getGravity() const
  52. {
  53. return m_gravity;
  54. }
  55. F32 getDeltaTime() const
  56. {
  57. return m_dt;
  58. }
  59. /// @}
  60. private:
  61. ChainAllocator<U8> m_alloc;
  62. List<PhysicsCollisionShape*> m_collisions;
  63. List<PhysicsBody*> m_bodies;
  64. List<PhysicsPlayerController*> m_playerControllers;
  65. Array<Atomic<U32>, static_cast<U>(PhysicsObject::Type::COUNT)>
  66. m_forDeletionCount = {{{0}, {0}, {0}}};
  67. mutable NewtonWorld* m_world = nullptr;
  68. Vec4 m_gravity = Vec4(0.0, -9.8, 0.0, 0.0);
  69. F32 m_dt = 0.0;
  70. template<typename T, typename TContainer, typename... TArgs>
  71. T* newObjectInternal(TContainer& cont, TArgs&&... args);
  72. template<typename T>
  73. void cleanupMarkedForDeletion(
  74. List<T*>& container, Atomic<U32>& count);
  75. /// Custom update
  76. static void postUpdateCallback(
  77. const NewtonWorld* const world,
  78. void* const listenerUserData, F32 dt)
  79. {
  80. static_cast<PhysicsWorld*>(listenerUserData)->postUpdate(dt);
  81. }
  82. void postUpdate(F32 dt);
  83. static void destroyCallback(
  84. const NewtonWorld* const world,
  85. void* const listenerUserData)
  86. {}
  87. };
  88. //==============================================================================
  89. template<typename T, typename TContainer, typename... TArgs>
  90. inline T* PhysicsWorld::newObjectInternal(TContainer& cont, TArgs&&... args)
  91. {
  92. Error err = ErrorCode::NONE;
  93. ChainAllocator<T> al = m_alloc;
  94. T* ptr = al.template newInstance<T>(this);
  95. if(ptr)
  96. {
  97. err = ptr->create(std::forward<TArgs>(args)...);
  98. }
  99. else
  100. {
  101. err = ErrorCode::OUT_OF_MEMORY;
  102. }
  103. if(!err)
  104. {
  105. err = cont.pushBack(m_alloc, ptr);
  106. }
  107. if(err)
  108. {
  109. ANKI_LOGE("Failed to create physics object");
  110. if(ptr)
  111. {
  112. al.deleteInstance(ptr);
  113. ptr = nullptr;
  114. }
  115. }
  116. return ptr;
  117. }
  118. /// @}
  119. } // end namespace anki
  120. #endif