Ndk/Physics: Added first physics components/systems

Former-commit-id: 654b7a2a4645487d139474dcbd02c0882d7c8f02
This commit is contained in:
Lynix
2015-05-02 10:00:07 +02:00
parent 55519b5e31
commit d558b04aa7
15 changed files with 670 additions and 0 deletions

View File

@@ -0,0 +1,67 @@
// 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/Components/CollisionComponent.hpp>
#include <Nazara/Physics/PhysObject.hpp>
#include <NDK/World.hpp>
#include <NDK/Components/PhysicsComponent.hpp>
#include <NDK/Systems/PhysicsSystem.hpp>
namespace Ndk
{
void CollisionComponent::SetGeom(NzPhysGeomRef geom)
{
m_geom = std::move(geom);
if (m_entity->HasComponent<PhysicsComponent>())
{
// On met à jour la géométrie du PhysObject associé au PhysicsComponent
PhysicsComponent& physComponent = m_entity->GetComponent<PhysicsComponent>();
physComponent.GetPhysObject().SetGeom(m_geom);
}
else
{
NazaraAssert(m_staticBody, "An entity without physics component should have a static body");
m_staticBody->SetGeom(m_geom);
}
}
void CollisionComponent::InitializeStaticBody()
{
NazaraAssert(m_entity, "Invalid entity");
World* entityWorld = m_entity->GetWorld();
NazaraAssert(entityWorld, "Entity must have world");
NazaraAssert(entityWorld->HasSystem<PhysicsSystem>(), "World must have a physics system");
NzPhysWorld& physWorld = entityWorld->GetSystem<PhysicsSystem>().GetWorld();
m_staticBody.reset(new NzPhysObject(&physWorld, m_geom));
m_staticBody->EnableAutoSleep(false);
}
void CollisionComponent::OnAttached()
{
if (!m_entity->HasComponent<PhysicsComponent>())
InitializeStaticBody();
}
void CollisionComponent::OnComponentAttached(BaseComponent& component)
{
if (component.GetIndex() == GetComponentIndex<PhysicsComponent>())
m_staticBody.reset();
}
void CollisionComponent::OnComponentDetached(BaseComponent& component)
{
if (component.GetIndex() == GetComponentIndex<PhysicsComponent>())
InitializeStaticBody();
}
void CollisionComponent::OnDetached()
{
m_staticBody.reset();
}
ComponentIndex CollisionComponent::componentIndex;
}

View File

@@ -0,0 +1,59 @@
// 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/Components/PhysicsComponent.hpp>
#include <Nazara/Physics/PhysObject.hpp>
#include <NDK/World.hpp>
#include <NDK/Components/CollisionComponent.hpp>
#include <NDK/Components/NodeComponent.hpp>
#include <NDK/Systems/PhysicsSystem.hpp>
namespace Ndk
{
void PhysicsComponent::OnAttached()
{
World* entityWorld = m_entity->GetWorld();
NazaraAssert(entityWorld->HasSystem<PhysicsSystem>(), "World must have a physics system");
NzPhysWorld& world = entityWorld->GetSystem<PhysicsSystem>().GetWorld();
NzPhysGeomRef geom;
if (m_entity->HasComponent<CollisionComponent>())
geom = m_entity->GetComponent<CollisionComponent>().GetGeom();
NzMatrix4f matrix;
if (m_entity->HasComponent<NodeComponent>())
matrix = m_entity->GetComponent<NodeComponent>().GetTransformMatrix();
else
matrix.MakeIdentity();
m_object.reset(new NzPhysObject(&world, geom, matrix));
m_object->SetMass(1.f);
}
void PhysicsComponent::OnComponentAttached(BaseComponent& component)
{
if (component.GetIndex() == GetComponentIndex<CollisionComponent>())
{
NazaraAssert(m_object, "Invalid object");
m_object->SetGeom(static_cast<CollisionComponent&>(component).GetGeom());
}
}
void PhysicsComponent::OnComponentDetached(BaseComponent& component)
{
if (component.GetIndex() == GetComponentIndex<CollisionComponent>())
{
NazaraAssert(m_object, "Invalid object");
m_object->SetGeom(NzNullGeom::New());
}
}
void PhysicsComponent::OnDetached()
{
m_object.reset();
}
ComponentIndex PhysicsComponent::componentIndex;
}

View File

@@ -13,10 +13,14 @@
#include <Nazara/Utility/Utility.hpp>
#include <NDK/Algorithm.hpp>
#include <NDK/BaseSystem.hpp>
#include <NDK/Components/CollisionComponent.hpp>
#include <NDK/Components/ListenerComponent.hpp>
#include <NDK/Components/NodeComponent.hpp>
#include <NDK/Components/PhysicsComponent.hpp>
#include <NDK/Components/VelocityComponent.hpp>
#include <NDK/Systems/ListenerSystem.hpp>
#include <NDK/Systems/PhysicsSystem.hpp>
#include <NDK/Systems/StaticCollisionSystem.hpp>
#include <NDK/Systems/VelocitySystem.hpp>
namespace Ndk
@@ -49,12 +53,16 @@ namespace Ndk
BaseSystem::Initialize();
// Composants
InitializeComponent<CollisionComponent>("NdkColli");
InitializeComponent<ListenerComponent>("NdkList");
InitializeComponent<NodeComponent>("NdkNode");
InitializeComponent<PhysicsComponent>("NdkPhys");
InitializeComponent<VelocityComponent>("NdkVeloc");
// Systèmes
InitializeSystem<ListenerSystem>();
InitializeSystem<PhysicsSystem>();
InitializeSystem<StaticCollisionSystem>();
InitializeSystem<VelocitySystem>();
NazaraNotice("Initialized: SDK");

View File

@@ -0,0 +1,40 @@
// 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/PhysicsSystem.hpp>
#include <Nazara/Physics/PhysObject.hpp>
#include <NDK/Components/CollisionComponent.hpp>
#include <NDK/Components/NodeComponent.hpp>
#include <NDK/Components/PhysicsComponent.hpp>
namespace Ndk
{
PhysicsSystem::PhysicsSystem()
{
Requires<NodeComponent, PhysicsComponent>();
}
PhysicsSystem::PhysicsSystem(const PhysicsSystem& system) :
System(system),
m_world()
{
}
void PhysicsSystem::Update(float elapsedTime)
{
m_world.Step(elapsedTime);
for (const Ndk::EntityHandle& entity : GetEntities())
{
NodeComponent& node = entity->GetComponent<NodeComponent>();
PhysicsComponent& phys = entity->GetComponent<PhysicsComponent>();
NzPhysObject& physObj = phys.GetPhysObject();
node.SetRotation(physObj.GetRotation(), nzCoordSys_Global);
node.SetPosition(physObj.GetPosition(), nzCoordSys_Global);
}
}
SystemIndex PhysicsSystem::systemIndex;
}

View File

@@ -0,0 +1,60 @@
// 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;
}

View File

@@ -4,6 +4,7 @@
#include <NDK/Systems/VelocitySystem.hpp>
#include <NDK/Components/NodeComponent.hpp>
#include <NDK/Components/PhysicsComponent.hpp>
#include <NDK/Components/VelocityComponent.hpp>
namespace Ndk
@@ -11,6 +12,7 @@ namespace Ndk
VelocitySystem::VelocitySystem()
{
Requires<NodeComponent, VelocityComponent>();
Excludes<PhysicsComponent>();
}
void VelocitySystem::Update(float elapsedTime)

View File

@@ -5,6 +5,8 @@
#include <NDK/World.hpp>
#include <Nazara/Core/Error.hpp>
#include <NDK/Systems/ListenerSystem.hpp>
#include <NDK/Systems/PhysicsSystem.hpp>
#include <NDK/Systems/StaticCollisionSystem.hpp>
#include <NDK/Systems/VelocitySystem.hpp>
namespace Ndk
@@ -18,6 +20,8 @@ namespace Ndk
void World::AddDefaultSystems()
{
AddSystem<ListenerSystem>();
AddSystem<PhysicsSystem>();
AddSystem<StaticCollisionSystem>();
AddSystem<VelocitySystem>();
}