Merge remote-tracking branch 'origin/Physics-module'

Former-commit-id: d4e2c3b0633ac7916b646e6fe33c75ed94e68bf1
This commit is contained in:
Lynix
2013-06-01 12:06:09 +02:00
15 changed files with 965 additions and 0 deletions

View File

@@ -0,0 +1,29 @@
// Copyright (C) 2013 Jérôme Leclercq
// This file is part of the "Nazara Engine - Physics module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Physics/Config.hpp>
#if NAZARA_PHYSICS_MEMORYLEAKTRACKER || defined(NAZARA_DEBUG)
#include <Nazara/Core/Debug/MemoryLeakTracker.hpp>
#include <new>
void* operator new(std::size_t size)
{
return NzMemoryManager::Allocate(size, false);
}
void* operator new[](std::size_t size)
{
return NzMemoryManager::Allocate(size, true);
}
void operator delete(void* pointer) noexcept
{
NzMemoryManager::Free(pointer, false);
}
void operator delete[](void* pointer) noexcept
{
NzMemoryManager::Free(pointer, true);
}
#endif

240
src/Nazara/Physics/Geom.cpp Normal file
View File

@@ -0,0 +1,240 @@
// Copyright (C) 2013 Jérôme Leclercq
// This file is part of the "Nazara Engine - Physics module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Physics/Geom.hpp>
#include <Nazara/Physics/PhysWorld.hpp>
#include <Newton/Newton.h>
#include <memory>
#include <Nazara/Physics/Debug.hpp>
NzBaseGeom::NzBaseGeom(NzPhysWorld* physWorld) :
m_world(physWorld)
{
}
NzBaseGeom::~NzBaseGeom()
{
NewtonReleaseCollision(m_world->GetHandle(), m_collision);
}
NzCubef NzBaseGeom::ComputeAABB(const NzVector3f& translation, const NzQuaternionf& rotation, const NzVector3f& scale) const
{
NzVector3f min, max;
NewtonCollisionCalculateAABB(m_collision, NzMatrix4f::Transform(translation, rotation), min, max);
// Et on applique le scale à la fin
return NzCubef(scale*min, scale*max);
}
NzCubef NzBaseGeom::ComputeAABB(const NzMatrix4f& offsetMatrix) const
{
NzVector3f min, max;
NewtonCollisionCalculateAABB(m_collision, offsetMatrix, min, max);
return NzCubef(min, max);
}
void NzBaseGeom::ComputeInertialMatrix(NzVector3f* inertia, NzVector3f* center) const
{
float inertiaMatrix[3];
float origin[3];
NewtonConvexCollisionCalculateInertialMatrix(m_collision, inertiaMatrix, origin);
if (inertia)
inertia->Set(inertiaMatrix);
if (center)
center->Set(origin);
}
float NzBaseGeom::ComputeVolume() const
{
return NewtonConvexCollisionCalculateVolume(m_collision);
}
NewtonCollision* NzBaseGeom::GetHandle() const
{
return m_collision;
}
NzPhysWorld* NzBaseGeom::GetWorld() const
{
return m_world;
}
/********************************** BoxGeom **********************************/
NzBoxGeom::NzBoxGeom(NzPhysWorld* physWorld, const NzVector3f& lengths, const NzVector3f& translation, const NzQuaternionf& rotation) :
NzBaseGeom(physWorld),
m_lengths(lengths)
{
m_collision = NewtonCreateBox(physWorld->GetHandle(), lengths.x, lengths.y, lengths.z, 0, NzMatrix4f::Transform(translation, rotation));
}
NzVector3f NzBoxGeom::GetLengths() const
{
return m_lengths;
}
nzGeomType NzBoxGeom::GetType() const
{
return nzGeomType_Box;
}
/******************************** CapsuleGeom ********************************/
NzCapsuleGeom::NzCapsuleGeom(NzPhysWorld* physWorld, float length, float radius, const NzVector3f& translation, const NzQuaternionf& rotation) :
NzBaseGeom(physWorld),
m_length(length),
m_radius(radius)
{
m_collision = NewtonCreateCapsule(physWorld->GetHandle(), radius, length, 0, NzMatrix4f::Transform(translation, rotation));
}
float NzCapsuleGeom::GetLength() const
{
return m_length;
}
float NzCapsuleGeom::GetRadius() const
{
return m_radius;
}
nzGeomType NzCapsuleGeom::GetType() const
{
return nzGeomType_Capsule;
}
/******************************* CompoundGeom ********************************/
NzCompoundGeom::NzCompoundGeom(NzPhysWorld* physWorld, NzBaseGeom* geoms, unsigned int geomCount) :
NzBaseGeom(physWorld)
{
std::vector<NewtonCollision*> collisions;
collisions.reserve(geomCount);
for (unsigned int i = 0; i < geomCount; ++i)
{
if (geoms[i].GetType() == nzGeomType_Compound)
{
NewtonCollisionInfoRecord info;
NewtonCollisionGetInfo(geoms[i].GetHandle(), &info);
unsigned int count = info.m_compoundCollision.m_chidrenCount;
for (unsigned int j = 0; j < count; ++j)
collisions.push_back(info.m_compoundCollision.m_chidren[j]);
}
else
collisions.push_back(geoms[i].GetHandle());
}
m_collision = NewtonCreateCompoundCollision(physWorld->GetHandle(), collisions.size(), &collisions[0], 0);
}
nzGeomType NzCompoundGeom::GetType() const
{
return nzGeomType_Compound;
}
/********************************* ConeGeom **********************************/
NzConeGeom::NzConeGeom(NzPhysWorld* physWorld, float length, float radius, const NzVector3f& translation, const NzQuaternionf& rotation) :
NzBaseGeom(physWorld),
m_length(length),
m_radius(radius)
{
m_collision = NewtonCreateCone(physWorld->GetHandle(), radius, length, 0, NzMatrix4f::Transform(translation, rotation));
}
float NzConeGeom::GetLength() const
{
return m_length;
}
float NzConeGeom::GetRadius() const
{
return m_radius;
}
nzGeomType NzConeGeom::GetType() const
{
return nzGeomType_Cone;
}
/****************************** ConvexHullGeom *******************************/
NzConvexHullGeom::NzConvexHullGeom(NzPhysWorld* physWorld, const NzVector3f* vertices, unsigned int vertexCount, unsigned int stride, float tolerance, const NzVector3f& translation, const NzQuaternionf& rotation) :
NzBaseGeom(physWorld)
{
m_collision = NewtonCreateConvexHull(physWorld->GetHandle(), vertexCount, reinterpret_cast<const float*>(vertices), stride, tolerance, 0, NzMatrix4f::Transform(translation, rotation));
}
nzGeomType NzConvexHullGeom::GetType() const
{
return nzGeomType_Compound;
}
/******************************* CylinderGeom ********************************/
NzCylinderGeom::NzCylinderGeom(NzPhysWorld* physWorld, float length, float radius, const NzVector3f& translation, const NzQuaternionf& rotation) :
NzBaseGeom(physWorld),
m_length(length),
m_radius(radius)
{
m_collision = NewtonCreateCylinder(physWorld->GetHandle(), radius, length, 0, NzMatrix4f::Transform(translation, rotation));
}
float NzCylinderGeom::GetLength() const
{
return m_length;
}
float NzCylinderGeom::GetRadius() const
{
return m_radius;
}
nzGeomType NzCylinderGeom::GetType() const
{
return nzGeomType_Cylinder;
}
/********************************* NullGeom **********************************/
NzNullGeom::NzNullGeom(NzPhysWorld* physWorld) :
NzBaseGeom(physWorld)
{
m_collision = NewtonCreateNull(physWorld->GetHandle());
}
nzGeomType NzNullGeom::GetType() const
{
return nzGeomType_Null;
}
/******************************** SphereGeom *********************************/
NzSphereGeom::NzSphereGeom(NzPhysWorld* physWorld, const NzVector3f& radius, const NzVector3f& translation) :
NzBaseGeom(physWorld),
m_radius(radius)
{
m_collision = NewtonCreateSphere(physWorld->GetHandle(), radius.x, radius.y, radius.z, 0, NzMatrix4f::Translate(translation));
}
NzSphereGeom::NzSphereGeom(NzPhysWorld* physWorld, float radius, const NzVector3f& translation) :
NzSphereGeom(physWorld, NzVector3f(radius), translation)
{
}
NzVector3f NzSphereGeom::GetRadius() const
{
return m_radius;
}
nzGeomType NzSphereGeom::GetType() const
{
return nzGeomType_Sphere;
}

View File

@@ -0,0 +1,157 @@
// Copyright (C) 2013 Jérôme Leclercq
// This file is part of the "Nazara Engine - Physics module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Physics/PhysObject.hpp>
#include <Nazara/Physics/Config.hpp>
#include <Nazara/Physics/Geom.hpp>
#include <Nazara/Physics/PhysWorld.hpp>
#include <Newton/Newton.h>
#include <Nazara/Physics/Debug.hpp>
NzPhysObject::NzPhysObject(NzPhysWorld* world, const NzMatrix4f& mat) :
m_matrix(mat),
m_forceAccumulator(NzVector3f::Zero()),
m_world(world),
m_ownsGeom(true),
m_gravityFactor(1.f),
m_mass(0.f)
{
#if NAZARA_PHYSICS_SAFE
if (!world)
NazaraError("Invalid physics world"); ///TODO: Unexcepted
#endif
m_geom = new NzNullGeom(world);
m_body = NewtonCreateBody(world->GetHandle(), m_geom->GetHandle(), mat);
NewtonBodySetUserData(m_body, this);
}
NzPhysObject::NzPhysObject(NzPhysWorld* world, const NzBaseGeom* geom, const NzMatrix4f& mat) :
m_matrix(mat),
m_forceAccumulator(NzVector3f::Zero()),
m_geom(geom),
m_world(world),
m_ownsGeom(false),
m_gravityFactor(1.f),
m_mass(0.f)
{
#if NAZARA_PHYSICS_SAFE
if (!world)
NazaraError("Invalid physics world"); ///TODO: Unexcepted
#endif
m_body = NewtonCreateBody(world->GetHandle(), geom->GetHandle(), mat);
NewtonBodySetUserData(m_body, this);
}
NzPhysObject::~NzPhysObject()
{
NewtonDestroyBody(m_world->GetHandle(), m_body);
if (m_ownsGeom)
delete m_geom;
}
float NzPhysObject::GetGravityFactor() const
{
return m_gravityFactor;
}
float NzPhysObject::GetMass() const
{
return m_mass;
}
NzVector3f NzPhysObject::GetMassCenter() const
{
NzVector3f center;
NewtonBodyGetCentreOfMass(m_body, center);
return center;
}
NzVector3f NzPhysObject::GetPosition() const
{
return m_matrix.GetTranslation();
}
NzQuaternionf NzPhysObject::GetRotation() const
{
return m_matrix.GetRotation();
}
NzVector3f NzPhysObject::GetVelocity() const
{
NzVector3f velocity;
NewtonBodyGetVelocity(m_body, velocity);
return velocity;
}
bool NzPhysObject::IsMoveable() const
{
return m_mass > 0.f;
}
void NzPhysObject::SetMass(float mass)
{
if (m_mass > 0.f)
{
float Ix, Iy, Iz;
NewtonBodyGetMassMatrix(m_body, &m_mass, &Ix, &Iy, &Iz);
float scale = mass/m_mass;
NewtonBodySetMassMatrix(m_body, mass, Ix*scale, Iy*scale, Iz*scale);
}
else if (mass > 0.f)
{
NzVector3f inertia, origin;
m_geom->ComputeInertialMatrix(&inertia, &origin);
NewtonBodySetCentreOfMass(m_body, &origin.x);
NewtonBodySetMassMatrix(m_body, mass, inertia.x*mass, inertia.y*mass, inertia.z*mass);
NewtonBodySetForceAndTorqueCallback(m_body, &ForceAndTorqueCallback);
NewtonBodySetTransformCallback(m_body, &TransformCallback);
}
m_mass = mass;
}
void NzPhysObject::SetMassCenter(NzVector3f center)
{
if (m_mass > 0.f)
NewtonBodySetCentreOfMass(m_body, center);
}
void NzPhysObject::ForceAndTorqueCallback(const NewtonBody* body, float timeStep, int threadIndex)
{
NazaraUnused(timeStep);
NazaraUnused(threadIndex);
NzPhysObject* me = static_cast<NzPhysObject*>(NewtonBodyGetUserData(body));
if (!NzNumberEquals(me->m_gravityFactor, 0.f))
me->m_forceAccumulator += me->m_world->GetGravity() * me->m_gravityFactor * me->m_mass;
/*for (std::set<PhysObjectListener*>::iterator it = me->m_listeners.begin(); it != me->m_listeners.end(); ++it)
(*it)->PhysObjectApplyForce(me);*/
NewtonBodySetForce(body, me->m_forceAccumulator);
me->m_forceAccumulator.Set(0.f);
/*NewtonBodyAddTorque(body, &me->m_torqueAccumulator.x);
me->m_torqueAccumulator = 0.f;*/
///TODO: Implanter la force gyroscopique?
}
void NzPhysObject::TransformCallback(const NewtonBody* body, const float* matrix, int threadIndex)
{
NazaraUnused(threadIndex);
NzPhysObject* me = static_cast<NzPhysObject*>(NewtonBodyGetUserData(body));
me->m_matrix.Set(matrix);
/*for (std::set<PhysObjectListener*>::iterator it = me->m_listeners.begin(); it != me->m_listeners.end(); ++it)
(*it)->PhysObjectOnUpdate(me);*/
}

View File

@@ -0,0 +1,53 @@
// Copyright (C) 2013 Jérôme Leclercq
// This file is part of the "Nazara Engine - Physics module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Physics/PhysWorld.hpp>
#include <Newton/Newton.h>
#include <Nazara/Physics/Debug.hpp>
NzPhysWorld::NzPhysWorld()
{
m_world = NewtonCreate();
NewtonWorldSetUserData(m_world, this);
}
NzPhysWorld::~NzPhysWorld()
{
NewtonDestroy(m_world);
}
NzVector3f NzPhysWorld::GetGravity() const
{
return m_gravity;
}
NewtonWorld* NzPhysWorld::GetHandle() const
{
return m_world;
}
void NzPhysWorld::SetGravity(const NzVector3f& gravity)
{
m_gravity = gravity;
}
void NzPhysWorld::SetSize(const NzCubef& cube)
{
NewtonSetWorldSize(m_world, cube.GetPosition(), cube.GetPosition()+cube.GetSize());
}
void NzPhysWorld::SetSize(const NzVector3f& min, const NzVector3f& max)
{
NewtonSetWorldSize(m_world, min, max);
}
void NzPhysWorld::SetSolverModel(unsigned int model)
{
NewtonSetSolverModel(m_world, model);
}
void NzPhysWorld::Update(float timestep)
{
NewtonUpdate(m_world, timestep);
}

View File

@@ -0,0 +1,64 @@
// Copyright (C) 2013 Jérôme Leclercq
// This file is part of the "Nazara Engine - Physics module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Physics/Physics.hpp>
#include <Nazara/Core/Core.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Core/Log.hpp>
#include <Nazara/Physics/Config.hpp>
#include <Newton/Newton.h>
#include <Nazara/Physics/Debug.hpp>
unsigned int NzPhysics::GetMemoryUsed()
{
return NewtonGetMemoryUsed();
}
bool NzPhysics::Initialize()
{
if (s_moduleReferenceCounter++ != 0)
return true; // Déjà initialisé
// Initialisation des dépendances
if (!NzCore::Initialize())
{
NazaraError("Failed to initialize core module");
Uninitialize();
return false;
}
// Initialisation du module
NazaraNotice("Initialized: Physics module");
return true;
}
bool NzPhysics::IsInitialized()
{
return s_moduleReferenceCounter != 0;
}
void NzPhysics::Uninitialize()
{
if (s_moduleReferenceCounter != 1)
{
// Le module est soit encore utilisé, soit pas initialisé
if (s_moduleReferenceCounter > 1)
s_moduleReferenceCounter--;
return;
}
// Libération du module
s_moduleReferenceCounter = 0;
NazaraNotice("Uninitialized: Physics module");
// Libération des dépendances
NzCore::Uninitialize();
}
unsigned int NzPhysics::s_moduleReferenceCounter = 0;