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;
}