diff --git a/include/Nazara/Physics/CollisionShape.hpp b/include/Nazara/Physics/CollisionShape.hpp new file mode 100644 index 000000000..4770decd8 --- /dev/null +++ b/include/Nazara/Physics/CollisionShape.hpp @@ -0,0 +1,36 @@ +// Copyright (C) 2012 Jérôme Leclercq / Rémi Bèges +// This file is part of the "Nazara Engine". +// For conditions of distribution and use, see copyright notice in Config.hpp + +#pragma once + +#ifndef COLLISIONSHAPE_HPP +#define COLLISIONSHAPE_HPP + +#include +#include "PhysicsWorld.hpp" +//#include + +class NzCollisionShape +{ + public: + NzCollisionShape(NzPhysicsWorld* world); + ~NzCollisionShape(); + + void Create(const NzVector3f& cubeSize); + //void Create(const NzSpheref& sphere); + //void Create(const NzConef& cone); + //void Create(const NzMesh& customMesh); + void Release(); + + bool IsValid(); + + NewtonCollision* newtonCollisionShape; + + protected: + private: + NzPhysicsWorld* m_world; + bool m_isCreated; +}; + +#endif // COLLISIONSHAPE_HPP diff --git a/include/Nazara/Physics/Debug.hpp b/include/Nazara/Physics/Debug.hpp index 933d5db67..a0144954a 100644 --- a/include/Nazara/Physics/Debug.hpp +++ b/include/Nazara/Physics/Debug.hpp @@ -2,7 +2,7 @@ // This file is part of the "Nazara Engine". // For conditions of distribution and use, see copyright notice in Config.hpp -#include +#include #if NAZARA_PHYSICS_MEMORYLEAKTRACKER || defined(NAZARA_DEBUG) #include diff --git a/include/Nazara/Physics/PhysicsEntity.hpp b/include/Nazara/Physics/PhysicsEntity.hpp new file mode 100644 index 000000000..3f9e9a40b --- /dev/null +++ b/include/Nazara/Physics/PhysicsEntity.hpp @@ -0,0 +1,33 @@ +// Copyright (C) 2012 Jérôme Leclercq / Rémi Bèges +// This file is part of the "Nazara Engine". +// For conditions of distribution and use, see copyright notice in Config.hpp + +#pragma once + +#ifndef PHYSICSENTITY_HPP +#define PHYSICSENTITY_HPP + +#include +#include +#include +//#include +#include "CollisionShape.hpp" + +class NzPhysicsWorld; + +class NzPhysicsEntity +{ + public: + NzPhysicsEntity(NzPhysicsWorld* world, const NzCollisionShape& shape, const NzVector3f& position, float mass); + virtual void Init(); + virtual ~NzPhysicsEntity(); + protected: + NzPhysicsWorld* m_world; + NewtonBody* m_body; + float m_mass; + NzMatrix4f m_entityMatrix; + private: + +}; + +#endif // PHYSICSENTITY_HPP diff --git a/include/Nazara/Physics/PhysicsWorld.hpp b/include/Nazara/Physics/PhysicsWorld.hpp index b0eda5f26..e52536ce5 100644 --- a/include/Nazara/Physics/PhysicsWorld.hpp +++ b/include/Nazara/Physics/PhysicsWorld.hpp @@ -9,34 +9,43 @@ #include #include +#include +#include +#include "PhysicsSolver.hpp" +//#include -enum nzFrictionModel -{ - nzExact, - nzAdaptative -}; - -class NzPhysicsSolver; +//TODO : ajouter Axis Aligned Bounding Box class NzPhysicsWorld { + enum nzFrictionModel + { + nzExact, + nzAdaptative + }; + public: NzPhysicsWorld(); ~NzPhysicsWorld(); + void SetSize(const NzCubef& size); + const NzCubef& GetSize() const; + void SetPhysicsSolver(const NzPhysicsSolver& solver); const NzPhysicsSolver& GetPhysicsSolver() const; void SetFrictionModel(nzFrictionModel model); - const nzFrictionModel& GetFrictionModel(); + const nzFrictionModel& GetFrictionModel() const; - void UpdatePhysics(nzUint64 timestep); + void UpdatePhysics(float timestep); + + NewtonWorld* newtonWorld; protected: private: - NewtonWorld* m_world; NzPhysicsSolver m_solver; nzFrictionModel m_frictionModel; + NzCubef m_size; }; #endif // PHYSICSWORLD_HPP diff --git a/include/Nazara/Physics/StaticBody.hpp b/include/Nazara/Physics/StaticBody.hpp new file mode 100644 index 000000000..222c15b05 --- /dev/null +++ b/include/Nazara/Physics/StaticBody.hpp @@ -0,0 +1,23 @@ +// Copyright (C) 2012 Jérôme Leclercq / Rémi Bèges +// This file is part of the "Nazara Engine". +// For conditions of distribution and use, see copyright notice in Config.hpp + +#pragma once + +#ifndef STATICBODY_HPP +#define STATICBODY_HPP + +#include +//#include +#include "PhysicsEntity.hpp" + +class NzStaticBody : public NzPhysicsEntity +{ + public: + NzStaticBody(NzPhysicsWorld* world, const NzCollisionShape& shape, const NzVector3f& position, float mass); + ~NzStaticBody(); + protected: + private: +}; + +#endif // STATICBODY_HPP diff --git a/src/Nazara/Physics/CollisionShape.cpp b/src/Nazara/Physics/CollisionShape.cpp new file mode 100644 index 000000000..839c312a1 --- /dev/null +++ b/src/Nazara/Physics/CollisionShape.cpp @@ -0,0 +1,33 @@ +// Copyright (C) 2012 Jérôme Leclercq / Rémi Bèges +// This file is part of the "Nazara Engine". +// For conditions of distribution and use, see copyright notice in Config.hpp + +#include "CollisionShape.hpp" +//#include +#include + +NzCollisionShape::NzCollisionShape(NzPhysicsWorld* world) +{ + m_world = world; + m_isCreated = false; + newtonCollisionShape = nullptr; +} + +NzCollisionShape::~NzCollisionShape() +{ + if(m_isCreated) + NewtonReleaseCollision(m_world->newtonWorld, newtonCollisionShape); +} + +void NzCollisionShape::Create(const NzVector3f& cubeSize) +{ + newtonCollisionShape = NewtonCreateBox(m_world->newtonWorld, static_cast(cubeSize.x), + static_cast(cubeSize.y), + static_cast(cubeSize.z), 0, NULL); +} + +void NzCollisionShape::Release() +{ + if(m_isCreated) + NewtonReleaseCollision(m_world->newtonWorld, newtonCollisionShape); +} diff --git a/src/Nazara/Physics/PhysicsEntity.cpp b/src/Nazara/Physics/PhysicsEntity.cpp new file mode 100644 index 000000000..51d3e965a --- /dev/null +++ b/src/Nazara/Physics/PhysicsEntity.cpp @@ -0,0 +1,44 @@ +// Copyright (C) 2012 Jérôme Leclercq / Rémi Bèges +// This file is part of the "Nazara Engine". +// For conditions of distribution and use, see copyright notice in Config.hpp + +#include "PhysicsWorld.hpp" +//#include +#include "PhysicsEntity.hpp" +//#include +#include + +NzPhysicsEntity::NzPhysicsEntity(NzPhysicsWorld* world, const NzCollisionShape& shape, const NzVector3f& position, float mass) : m_world(world), m_mass(mass) +{ + m_entityMatrix.SetIdentity(); + m_entityMatrix.SetTranslation(position); + + m_body = NewtonCreateBody(world->newtonWorld, shape.newtonCollisionShape,NULL); + + //NewtonBodySetMatrix(m_pBody, /*&m_entityMatrix.matrice [0][0]*/);//Passage dgMatrix a NzMatrix4 ?? + +//Pour rigid bodies uniquement + /* + // On calcul l'inertie du corps, en passant par une petite formule + CVector inertie; + + inertie.x = 0.7f * m_masse * (m_taille.y * m_taille.y + m_taille.z * m_taille.z) / 12; + inertie.y = 0.7f * m_masse * (m_taille.x * m_taille.x + m_taille.z * m_taille.z) / 12; + inertie.z = 0.7f * m_masse * (m_taille.x * m_taille.x + m_taille.y * m_taille.y) / 12; + + // On définit ensuite la masse et l'inertie pour ce corps + NewtonBodySetMassMatrix (m_pBody, m_masse, inertie.x, inertie.y, inertie.z); + + // On règle enfin le Callback, qui sera nécessaire pour que le corps bouge + NewtonBodySetForceAndTorqueCallback (m_pBody, ApplyForceAndTorqueCallback);*/ +} + +void NzPhysicsEntity::Init() +{ + +} + +NzPhysicsEntity::~NzPhysicsEntity() +{ + NewtonDestroyBody(m_world->newtonWorld,m_body); +} diff --git a/src/Nazara/Physics/PhysicsSolver.cpp b/src/Nazara/Physics/PhysicsSolver.cpp index 9799a29f4..e062dc4ed 100644 --- a/src/Nazara/Physics/PhysicsSolver.cpp +++ b/src/Nazara/Physics/PhysicsSolver.cpp @@ -27,15 +27,15 @@ void NzPhysicsSolver::Set(NzPhysicsWorld* world) switch(m_mode) { case nzExact: - NewtonSetSolverModel(world, 0); + NewtonSetSolverModel(world->newtonWorld, 0); break; case nzAdaptative: - NewtonSetSolverModel(world, 1); + NewtonSetSolverModel(world->newtonWorld, 1); break; case nzLinear: - NewtonSetSolverModel(world, m_numberOfPassesLinearMode); + NewtonSetSolverModel(world->newtonWorld, m_numberOfPassesLinearMode); } } diff --git a/src/Nazara/Physics/PhysicsWorld.cpp b/src/Nazara/Physics/PhysicsWorld.cpp index e174b5ecb..dabbfd91b 100644 --- a/src/Nazara/Physics/PhysicsWorld.cpp +++ b/src/Nazara/Physics/PhysicsWorld.cpp @@ -4,14 +4,12 @@ #include "PhysicsWorld.hpp" //#include -#include "PhysicsSolver.hpp" -//#include #include NzPhysicsWorld::NzPhysicsWorld() { - m_world = NewtonCreate(); + newtonWorld = NewtonCreate(); m_solver.Configure(nzLinear,10); m_solver.Set(this); SetFrictionModel(nzAdaptative); @@ -19,16 +17,37 @@ NzPhysicsWorld::NzPhysicsWorld() NzPhysicsWorld::~NzPhysicsWorld() { - NewtonDestroy(m_world); + NewtonDestroy(newtonWorld); } -void NzPhysicsWorld::SetPhysicsSolverSolver(const NzPhysicsSolver& solver) +void NzPhysicsWorld::SetSize(const NzCubef& size) +{ + m_size = size; + float bottom[3]; + bottom[0] = m_size.x; + bottom[1] = m_size.y; + bottom[2] = m_size.z; + float top[3]; + top[0] = m_size.x + m_size.width; + top[1] = m_size.y + m_size.height; + top[2] = m_size.z + m_size.depth; + + NewtonSetWorldSize(newtonWorld, static_cast(bottom), + static_cast(top)); +} + +const NzCubef& NzPhysicsWorld::GetSize() const +{ + return m_size; +} + +void NzPhysicsWorld::SetPhysicsSolver(const NzPhysicsSolver& solver) { m_solver = solver; m_solver.Set(this); } -const nzSolverMode& NzPhysicsWorld::GetPhysicsSolver() +const NzPhysicsSolver& NzPhysicsWorld::GetPhysicsSolver() const { return m_solver; } @@ -38,23 +57,23 @@ void NzPhysicsWorld::SetFrictionModel(nzFrictionModel model) switch(model) { case nzExact: - NewtonSetFrictionModel(this,0); + NewtonSetFrictionModel(newtonWorld,0); break; case nzAdaptative: - NewtonSetFrictionModel(this,1); + NewtonSetFrictionModel(newtonWorld,1); break; } m_frictionModel = model; } -const nzFrictionModel& NzPhysicsWorld::GetFrictionModel() +const NzPhysicsWorld::nzFrictionModel& NzPhysicsWorld::GetFrictionModel() const { return m_frictionModel; } -void NzPhysicsWorld::UpdatePhysics(nzUint64 timestep) +void NzPhysicsWorld::UpdatePhysics(float timestep) { - NewtonUpdate(m_world,static_cast(timestep));//FLOAT WTF ? + NewtonUpdate(newtonWorld,timestep);//FLOAT WTF ? } diff --git a/src/Nazara/Physics/StaticBody.cpp b/src/Nazara/Physics/StaticBody.cpp new file mode 100644 index 000000000..118d4b627 --- /dev/null +++ b/src/Nazara/Physics/StaticBody.cpp @@ -0,0 +1,18 @@ +// Copyright (C) 2012 Jérôme Leclercq / Rémi Bèges +// This file is part of the "Nazara Engine". +// For conditions of distribution and use, see copyright notice in Config.hpp + +#include "StaticBody.hpp" +//#include + +#include + +NzStaticBody::NzStaticBody(NzPhysicsWorld* world, const NzCollisionShape& shape, const NzVector3f& position, float mass) : NzPhysicsEntity(world,shape,position,mass) +{ + //ctor +} + +NzStaticBody::~NzStaticBody() +{ + //dtor +}