diff --git a/SDK/include/NDK/Systems/PhysicsSystem.hpp b/SDK/include/NDK/Systems/PhysicsSystem.hpp index a5b0b9fcc..5c24c2b8a 100644 --- a/SDK/include/NDK/Systems/PhysicsSystem.hpp +++ b/SDK/include/NDK/Systems/PhysicsSystem.hpp @@ -10,6 +10,7 @@ #include #include #include +#include namespace Ndk { @@ -31,7 +32,7 @@ namespace Ndk EntityList m_dynamicObjects; EntityList m_staticObjects; - Nz::PhysWorld m_world; + std::unique_ptr m_world; ///TODO: std::optional (Should I make a Nz::Optional class?) }; } diff --git a/SDK/include/NDK/Systems/PhysicsSystem.inl b/SDK/include/NDK/Systems/PhysicsSystem.inl index bfe886cb1..443b82060 100644 --- a/SDK/include/NDK/Systems/PhysicsSystem.inl +++ b/SDK/include/NDK/Systems/PhysicsSystem.inl @@ -11,7 +11,7 @@ namespace Ndk inline Nz::PhysWorld& PhysicsSystem::GetWorld() { - return m_world; + return *m_world; } /*! @@ -21,6 +21,6 @@ namespace Ndk inline const Nz::PhysWorld& PhysicsSystem::GetWorld() const { - return m_world; + return *m_world; } } diff --git a/SDK/src/NDK/Systems/PhysicsSystem.cpp b/SDK/src/NDK/Systems/PhysicsSystem.cpp index 442784c22..793b0f1dc 100644 --- a/SDK/src/NDK/Systems/PhysicsSystem.cpp +++ b/SDK/src/NDK/Systems/PhysicsSystem.cpp @@ -50,16 +50,19 @@ namespace Ndk void PhysicsSystem::OnEntityValidation(Entity* entity, bool justAdded) { - // If entity has not been just added to the system, it is possible that it does not own to the right array + // It's possible our entity got revalidated because of the addition/removal of a PhysicsComponent if (!justAdded) { - // We take the inverted array from which the entity should belong to + // We take the opposite array from which the entity should belong to auto& entities = (entity->HasComponent()) ? m_staticObjects : m_dynamicObjects; entities.Remove(entity); } auto& entities = (entity->HasComponent()) ? m_dynamicObjects : m_staticObjects; entities.Insert(entity); + + if (!m_world) + m_world = std::make_unique(); } /*! @@ -70,7 +73,10 @@ namespace Ndk void PhysicsSystem::OnUpdate(float elapsedTime) { - m_world.Step(elapsedTime); + if (!m_world) + return; + + m_world->Step(elapsedTime); for (const Ndk::EntityHandle& entity : m_dynamicObjects) {