diff --git a/SDK/include/NDK/Systems/PhysicsSystem.hpp b/SDK/include/NDK/Systems/PhysicsSystem.hpp index 5c24c2b8a..a89b16b33 100644 --- a/SDK/include/NDK/Systems/PhysicsSystem.hpp +++ b/SDK/include/NDK/Systems/PhysicsSystem.hpp @@ -27,12 +27,13 @@ namespace Ndk static SystemIndex systemIndex; private: + void CreatePhysWorld() const; void OnEntityValidation(Entity* entity, bool justAdded) override; void OnUpdate(float elapsedTime) override; EntityList m_dynamicObjects; EntityList m_staticObjects; - std::unique_ptr m_world; ///TODO: std::optional (Should I make a Nz::Optional class?) + mutable 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 443b82060..6c8527b47 100644 --- a/SDK/include/NDK/Systems/PhysicsSystem.inl +++ b/SDK/include/NDK/Systems/PhysicsSystem.inl @@ -11,6 +11,9 @@ namespace Ndk inline Nz::PhysWorld& PhysicsSystem::GetWorld() { + if (!m_world) + CreatePhysWorld(); + return *m_world; } @@ -21,6 +24,9 @@ namespace Ndk inline const Nz::PhysWorld& PhysicsSystem::GetWorld() const { + if (!m_world) + CreatePhysWorld(); + return *m_world; } } diff --git a/SDK/src/NDK/Systems/PhysicsSystem.cpp b/SDK/src/NDK/Systems/PhysicsSystem.cpp index 793b0f1dc..782b989db 100644 --- a/SDK/src/NDK/Systems/PhysicsSystem.cpp +++ b/SDK/src/NDK/Systems/PhysicsSystem.cpp @@ -41,6 +41,13 @@ namespace Ndk { } + void PhysicsSystem::CreatePhysWorld() const + { + NazaraAssert(!m_world, "Physics world should not be created twice"); + + m_world = std::make_unique(); + } + /*! * \brief Operation to perform when entity is validated for the system * @@ -62,7 +69,7 @@ namespace Ndk entities.Insert(entity); if (!m_world) - m_world = std::make_unique(); + CreatePhysWorld(); } /*!