diff --git a/SDK/include/NDK/Systems/PhysicsSystem.hpp b/SDK/include/NDK/Systems/PhysicsSystem.hpp index d7a3f0ba1..dbe3bf42b 100644 --- a/SDK/include/NDK/Systems/PhysicsSystem.hpp +++ b/SDK/include/NDK/Systems/PhysicsSystem.hpp @@ -27,6 +27,10 @@ namespace Ndk static SystemIndex systemIndex; private: + void OnEntityValidation(Entity* entity, bool justAdded) override; + + std::vector m_dynamicObjects; + std::vector m_staticObjects; NzPhysWorld m_world; }; } diff --git a/SDK/include/NDK/Systems/StaticCollisionSystem.hpp b/SDK/include/NDK/Systems/StaticCollisionSystem.hpp deleted file mode 100644 index edaa6b0e2..000000000 --- a/SDK/include/NDK/Systems/StaticCollisionSystem.hpp +++ /dev/null @@ -1,29 +0,0 @@ -// Copyright (C) 2015 Jérôme Leclercq -// This file is part of the "Nazara Development Kit" -// For conditions of distribution and use, see copyright notice in Prerequesites.hpp - -#pragma once - -#ifndef NDK_SYSTEMS_STATICCOLLISIONSYSTEM_HPP -#define NDK_SYSTEMS_STATICCOLLISIONSYSTEM_HPP - -#include - -namespace Ndk -{ - class NDK_API StaticCollisionSystem : public System - { - public: - StaticCollisionSystem(); - StaticCollisionSystem(const StaticCollisionSystem& system) = default; - ~StaticCollisionSystem() = default; - - void Update(float elapsedTime); - - static SystemIndex systemIndex; - }; -} - -#include - -#endif // NDK_SYSTEMS_STATICCOLLISIONSYSTEM_HPP diff --git a/SDK/include/NDK/Systems/StaticCollisionSystem.inl b/SDK/include/NDK/Systems/StaticCollisionSystem.inl deleted file mode 100644 index 668f8b9e2..000000000 --- a/SDK/include/NDK/Systems/StaticCollisionSystem.inl +++ /dev/null @@ -1,7 +0,0 @@ -// Copyright (C) 2015 Jérôme Leclercq -// This file is part of the "Nazara Development Kit" -// For conditions of distribution and use, see copyright notice in Prerequesites.hpp - -namespace Ndk -{ -} diff --git a/SDK/src/NDK/Sdk.cpp b/SDK/src/NDK/Sdk.cpp index 6537dffe5..456332a88 100644 --- a/SDK/src/NDK/Sdk.cpp +++ b/SDK/src/NDK/Sdk.cpp @@ -20,7 +20,6 @@ #include #include #include -#include #include namespace Ndk @@ -62,7 +61,6 @@ namespace Ndk // Systèmes InitializeSystem(); InitializeSystem(); - InitializeSystem(); InitializeSystem(); NazaraNotice("Initialized: SDK"); diff --git a/SDK/src/NDK/Systems/PhysicsSystem.cpp b/SDK/src/NDK/Systems/PhysicsSystem.cpp index d171e1697..ec7d10d5f 100644 --- a/SDK/src/NDK/Systems/PhysicsSystem.cpp +++ b/SDK/src/NDK/Systems/PhysicsSystem.cpp @@ -12,7 +12,8 @@ namespace Ndk { PhysicsSystem::PhysicsSystem() { - Requires(); + Requires(); + RequiresAny(); } PhysicsSystem::PhysicsSystem(const PhysicsSystem& system) : @@ -25,7 +26,7 @@ namespace Ndk { m_world.Step(elapsedTime); - for (const Ndk::EntityHandle& entity : GetEntities()) + for (const Ndk::EntityHandle& entity : m_dynamicObjects) { NodeComponent& node = entity->GetComponent(); PhysicsComponent& phys = entity->GetComponent(); @@ -34,6 +35,70 @@ namespace Ndk node.SetRotation(physObj.GetRotation(), nzCoordSys_Global); node.SetPosition(physObj.GetPosition(), nzCoordSys_Global); } + + float invElapsedTime = 1.f / elapsedTime; + for (const Ndk::EntityHandle& entity : m_staticObjects) + { + CollisionComponent& collision = entity->GetComponent(); + NodeComponent& node = entity->GetComponent(); + + NzPhysObject* physObj = collision.GetStaticBody(); + + NzQuaternionf oldRotation = physObj->GetRotation(); + NzVector3f oldPosition = physObj->GetPosition(); + NzQuaternionf newRotation = node.GetRotation(nzCoordSys_Global); + NzVector3f newPosition = node.GetPosition(nzCoordSys_Global); + + // Pour déplacer des objets statiques et assurer les collisions, il faut leur définir une vitesse + // (note importante: le moteur physique n'applique pas la vitesse sur les objets statiques) + if (newPosition != oldPosition) + { + physObj->SetPosition(newPosition); + physObj->SetVelocity((newPosition - oldPosition) * invElapsedTime); + } + else + physObj->SetVelocity(NzVector3f::Zero()); + + if (newRotation != oldRotation) + { + NzQuaternionf transition = newRotation * oldRotation.GetConjugate(); + NzEulerAnglesf angles = transition.ToEulerAngles(); + NzVector3f angularVelocity(NzToRadians(angles.pitch * invElapsedTime), + NzToRadians(angles.yaw * invElapsedTime), + NzToRadians(angles.roll * invElapsedTime)); + + physObj->SetRotation(oldRotation); + physObj->SetAngularVelocity(angularVelocity); + } + else + physObj->SetAngularVelocity(NzVector3f::Zero()); + } + } + + void PhysicsSystem::OnEntityValidation(Entity* entity, bool justAdded) + { + // Si l'entité ne vient pas d'être ajoutée au système, il est possible qu'elle fasse partie du mauvais tableau + if (!justAdded) + { + // On prend le tableau inverse de celui dont l'entité devrait faire partie + auto& entities = (entity->HasComponent()) ? m_staticObjects : m_dynamicObjects; + + auto it = std::find(entities.begin(), entities.end(), *entity); + if (it != entities.end()) + { + // Pour éviter de déplacer beaucoup de handles, on swap le dernier avec celui à supprimer + std::swap(*it, entities.back()); + entities.pop_back(); // On le sort du vector + } + } + + if (entity->HasComponent()) + m_dynamicObjects.emplace_back(entity); + else + { + NazaraAssert(entity->HasComponent(), "Validated entity should have component CollisionComponent"); + m_staticObjects.emplace_back(entity); + } } SystemIndex PhysicsSystem::systemIndex; diff --git a/SDK/src/NDK/Systems/StaticCollisionSystem.cpp b/SDK/src/NDK/Systems/StaticCollisionSystem.cpp deleted file mode 100644 index 4ac3deb24..000000000 --- a/SDK/src/NDK/Systems/StaticCollisionSystem.cpp +++ /dev/null @@ -1,60 +0,0 @@ -// Copyright (C) 2015 Jérôme Leclercq -// This file is part of the "Nazara Development Kit" -// For conditions of distribution and use, see copyright notice in Prerequesites.hpp - -#include -#include -#include -#include - -namespace Ndk -{ - StaticCollisionSystem::StaticCollisionSystem() - { - Requires(); - Excludes(); - } - - void StaticCollisionSystem::Update(float elapsedTime) - { - float invElapsedTime = 1.f / elapsedTime; - for (const Ndk::EntityHandle& entity : GetEntities()) - { - CollisionComponent& collision = entity->GetComponent(); - NodeComponent& node = entity->GetComponent(); - - NzPhysObject* physObj = collision.GetStaticBody(); - - NzQuaternionf oldRotation = physObj->GetRotation(); - NzVector3f oldPosition = physObj->GetPosition(); - NzQuaternionf newRotation = node.GetRotation(nzCoordSys_Global); - NzVector3f newPosition = node.GetPosition(nzCoordSys_Global); - - // Pour déplacer des objets statiques et assurer les collisions, il faut leur définir une vitesse - // (note importante: le moteur physique n'applique pas la vitesse sur les objets statiques) - if (newPosition != oldPosition) - { - physObj->SetPosition(newPosition); - physObj->SetVelocity((newPosition - oldPosition) * invElapsedTime); - } - else - physObj->SetVelocity(NzVector3f::Zero()); - - if (newRotation != oldRotation) - { - NzQuaternionf transition = newRotation * oldRotation.GetConjugate(); - NzEulerAnglesf angles = transition.ToEulerAngles(); - NzVector3f angularVelocity(NzToRadians(angles.pitch * invElapsedTime), - NzToRadians(angles.yaw * invElapsedTime), - NzToRadians(angles.roll * invElapsedTime)); - - physObj->SetRotation(oldRotation); - physObj->SetAngularVelocity(angularVelocity); - } - else - physObj->SetAngularVelocity(NzVector3f::Zero()); - } - } - - SystemIndex StaticCollisionSystem::systemIndex; -} diff --git a/SDK/src/NDK/World.cpp b/SDK/src/NDK/World.cpp index ef8eb01fb..98d75e6f3 100644 --- a/SDK/src/NDK/World.cpp +++ b/SDK/src/NDK/World.cpp @@ -6,7 +6,6 @@ #include #include #include -#include #include namespace Ndk @@ -21,7 +20,6 @@ namespace Ndk { AddSystem(); AddSystem(); - AddSystem(); AddSystem(); }