Ndk/Physics: Merged StaticCollisionSystem with PhysicsSystem
Former-commit-id: de2127eb967fb29f285e9ebae7c743c07ea39f8a
This commit is contained in:
parent
8b5deffe35
commit
da36f95b7c
|
|
@ -27,6 +27,10 @@ namespace Ndk
|
||||||
static SystemIndex systemIndex;
|
static SystemIndex systemIndex;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
void OnEntityValidation(Entity* entity, bool justAdded) override;
|
||||||
|
|
||||||
|
std::vector<EntityHandle> m_dynamicObjects;
|
||||||
|
std::vector<EntityHandle> m_staticObjects;
|
||||||
NzPhysWorld m_world;
|
NzPhysWorld m_world;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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 <NDK/System.hpp>
|
|
||||||
|
|
||||||
namespace Ndk
|
|
||||||
{
|
|
||||||
class NDK_API StaticCollisionSystem : public System<StaticCollisionSystem>
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
StaticCollisionSystem();
|
|
||||||
StaticCollisionSystem(const StaticCollisionSystem& system) = default;
|
|
||||||
~StaticCollisionSystem() = default;
|
|
||||||
|
|
||||||
void Update(float elapsedTime);
|
|
||||||
|
|
||||||
static SystemIndex systemIndex;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#include <NDK/Systems/StaticCollisionSystem.inl>
|
|
||||||
|
|
||||||
#endif // NDK_SYSTEMS_STATICCOLLISIONSYSTEM_HPP
|
|
||||||
|
|
@ -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
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
@ -20,7 +20,6 @@
|
||||||
#include <NDK/Components/VelocityComponent.hpp>
|
#include <NDK/Components/VelocityComponent.hpp>
|
||||||
#include <NDK/Systems/ListenerSystem.hpp>
|
#include <NDK/Systems/ListenerSystem.hpp>
|
||||||
#include <NDK/Systems/PhysicsSystem.hpp>
|
#include <NDK/Systems/PhysicsSystem.hpp>
|
||||||
#include <NDK/Systems/StaticCollisionSystem.hpp>
|
|
||||||
#include <NDK/Systems/VelocitySystem.hpp>
|
#include <NDK/Systems/VelocitySystem.hpp>
|
||||||
|
|
||||||
namespace Ndk
|
namespace Ndk
|
||||||
|
|
@ -62,7 +61,6 @@ namespace Ndk
|
||||||
// Systèmes
|
// Systèmes
|
||||||
InitializeSystem<ListenerSystem>();
|
InitializeSystem<ListenerSystem>();
|
||||||
InitializeSystem<PhysicsSystem>();
|
InitializeSystem<PhysicsSystem>();
|
||||||
InitializeSystem<StaticCollisionSystem>();
|
|
||||||
InitializeSystem<VelocitySystem>();
|
InitializeSystem<VelocitySystem>();
|
||||||
|
|
||||||
NazaraNotice("Initialized: SDK");
|
NazaraNotice("Initialized: SDK");
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,8 @@ namespace Ndk
|
||||||
{
|
{
|
||||||
PhysicsSystem::PhysicsSystem()
|
PhysicsSystem::PhysicsSystem()
|
||||||
{
|
{
|
||||||
Requires<NodeComponent, PhysicsComponent>();
|
Requires<NodeComponent>();
|
||||||
|
RequiresAny<CollisionComponent, PhysicsComponent>();
|
||||||
}
|
}
|
||||||
|
|
||||||
PhysicsSystem::PhysicsSystem(const PhysicsSystem& system) :
|
PhysicsSystem::PhysicsSystem(const PhysicsSystem& system) :
|
||||||
|
|
@ -25,7 +26,7 @@ namespace Ndk
|
||||||
{
|
{
|
||||||
m_world.Step(elapsedTime);
|
m_world.Step(elapsedTime);
|
||||||
|
|
||||||
for (const Ndk::EntityHandle& entity : GetEntities())
|
for (const Ndk::EntityHandle& entity : m_dynamicObjects)
|
||||||
{
|
{
|
||||||
NodeComponent& node = entity->GetComponent<NodeComponent>();
|
NodeComponent& node = entity->GetComponent<NodeComponent>();
|
||||||
PhysicsComponent& phys = entity->GetComponent<PhysicsComponent>();
|
PhysicsComponent& phys = entity->GetComponent<PhysicsComponent>();
|
||||||
|
|
@ -34,6 +35,70 @@ namespace Ndk
|
||||||
node.SetRotation(physObj.GetRotation(), nzCoordSys_Global);
|
node.SetRotation(physObj.GetRotation(), nzCoordSys_Global);
|
||||||
node.SetPosition(physObj.GetPosition(), nzCoordSys_Global);
|
node.SetPosition(physObj.GetPosition(), nzCoordSys_Global);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
float invElapsedTime = 1.f / elapsedTime;
|
||||||
|
for (const Ndk::EntityHandle& entity : m_staticObjects)
|
||||||
|
{
|
||||||
|
CollisionComponent& collision = entity->GetComponent<CollisionComponent>();
|
||||||
|
NodeComponent& node = entity->GetComponent<NodeComponent>();
|
||||||
|
|
||||||
|
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<PhysicsComponent>()) ? 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<PhysicsComponent>())
|
||||||
|
m_dynamicObjects.emplace_back(entity);
|
||||||
|
else
|
||||||
|
{
|
||||||
|
NazaraAssert(entity->HasComponent<CollisionComponent>(), "Validated entity should have component CollisionComponent");
|
||||||
|
m_staticObjects.emplace_back(entity);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
SystemIndex PhysicsSystem::systemIndex;
|
SystemIndex PhysicsSystem::systemIndex;
|
||||||
|
|
|
||||||
|
|
@ -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 <NDK/Systems/StaticCollisionSystem.hpp>
|
|
||||||
#include <Nazara/Physics/PhysObject.hpp>
|
|
||||||
#include <NDK/Components/CollisionComponent.hpp>
|
|
||||||
#include <NDK/Components/NodeComponent.hpp>
|
|
||||||
|
|
||||||
namespace Ndk
|
|
||||||
{
|
|
||||||
StaticCollisionSystem::StaticCollisionSystem()
|
|
||||||
{
|
|
||||||
Requires<CollisionComponent, NodeComponent>();
|
|
||||||
Excludes<PhysicsComponent>();
|
|
||||||
}
|
|
||||||
|
|
||||||
void StaticCollisionSystem::Update(float elapsedTime)
|
|
||||||
{
|
|
||||||
float invElapsedTime = 1.f / elapsedTime;
|
|
||||||
for (const Ndk::EntityHandle& entity : GetEntities())
|
|
||||||
{
|
|
||||||
CollisionComponent& collision = entity->GetComponent<CollisionComponent>();
|
|
||||||
NodeComponent& node = entity->GetComponent<NodeComponent>();
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
@ -6,7 +6,6 @@
|
||||||
#include <Nazara/Core/Error.hpp>
|
#include <Nazara/Core/Error.hpp>
|
||||||
#include <NDK/Systems/ListenerSystem.hpp>
|
#include <NDK/Systems/ListenerSystem.hpp>
|
||||||
#include <NDK/Systems/PhysicsSystem.hpp>
|
#include <NDK/Systems/PhysicsSystem.hpp>
|
||||||
#include <NDK/Systems/StaticCollisionSystem.hpp>
|
|
||||||
#include <NDK/Systems/VelocitySystem.hpp>
|
#include <NDK/Systems/VelocitySystem.hpp>
|
||||||
|
|
||||||
namespace Ndk
|
namespace Ndk
|
||||||
|
|
@ -21,7 +20,6 @@ namespace Ndk
|
||||||
{
|
{
|
||||||
AddSystem<ListenerSystem>();
|
AddSystem<ListenerSystem>();
|
||||||
AddSystem<PhysicsSystem>();
|
AddSystem<PhysicsSystem>();
|
||||||
AddSystem<StaticCollisionSystem>();
|
|
||||||
AddSystem<VelocitySystem>();
|
AddSystem<VelocitySystem>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue