Physics/Geom: Geoms no longer need a pointer to PhysWorld

Former-commit-id: 7f9372e3ec13525208b49bc3fd07787657f64acb
This commit is contained in:
Lynix 2015-04-20 01:34:17 +02:00
parent 1c7c1de3e7
commit 06b2651264
2 changed files with 252 additions and 113 deletions

View File

@ -1,4 +1,4 @@
// Copyright (C) 2015 Jérôme Leclercq // Copyright (C) 2015 Jérôme Leclercq
// This file is part of the "Nazara Engine - Physics module" // This file is part of the "Nazara Engine - Physics module"
// For conditions of distribution and use, see copyright notice in Config.hpp // For conditions of distribution and use, see copyright notice in Config.hpp
@ -18,6 +18,7 @@
#include <Nazara/Math/Quaternion.hpp> #include <Nazara/Math/Quaternion.hpp>
#include <Nazara/Math/Vector3.hpp> #include <Nazara/Math/Vector3.hpp>
#include <Nazara/Physics/Enums.hpp> #include <Nazara/Physics/Enums.hpp>
#include <unordered_map>
///TODO: CollisionModifier ///TODO: CollisionModifier
///TODO: HeightfieldGeom ///TODO: HeightfieldGeom
@ -38,24 +39,23 @@ using NzPhysGeomRef = NzObjectRef<NzPhysGeom>;
class NAZARA_API NzPhysGeom : public NzRefCounted, NzNonCopyable class NAZARA_API NzPhysGeom : public NzRefCounted, NzNonCopyable
{ {
public: public:
NzPhysGeom(NzPhysWorld* physWorld); NzPhysGeom() = default;
virtual ~NzPhysGeom(); virtual ~NzPhysGeom();
virtual NzBoxf ComputeAABB(const NzVector3f& translation, const NzQuaternionf& rotation, const NzVector3f& scale) const; NzBoxf ComputeAABB(const NzVector3f& translation, const NzQuaternionf& rotation, const NzVector3f& scale) const;
virtual NzBoxf ComputeAABB(const NzMatrix4f& offsetMatrix = NzMatrix4f::Identity()) const; virtual NzBoxf ComputeAABB(const NzMatrix4f& offsetMatrix = NzMatrix4f::Identity(), const NzVector3f& scale = NzVector3f::Unit()) const;
virtual void ComputeInertialMatrix(NzVector3f* inertia, NzVector3f* center) const; virtual void ComputeInertialMatrix(NzVector3f* inertia, NzVector3f* center) const;
virtual float ComputeVolume() const; virtual float ComputeVolume() const;
NewtonCollision* GetHandle() const; NewtonCollision* GetHandle(NzPhysWorld* world) const;
virtual nzGeomType GetType() const = 0; virtual nzGeomType GetType() const = 0;
NzPhysWorld* GetWorld() const; static NzPhysGeomRef Build(const NzPrimitiveList& list);
static NzPhysGeomRef Build(NzPhysWorld* physWorld, const NzPrimitiveList& list);
protected: protected:
NewtonCollision* m_collision; virtual NewtonCollision* CreateHandle(NzPhysWorld* world) const = 0;
NzPhysWorld* m_world;
mutable std::unordered_map<NzPhysWorld*, NewtonCollision*> m_handles;
static NzPhysGeomLibrary::LibraryMap s_library; static NzPhysGeomLibrary::LibraryMap s_library;
}; };
@ -70,8 +70,11 @@ using NzBoxGeomRef = NzObjectRef<NzBoxGeom>;
class NAZARA_API NzBoxGeom : public NzPhysGeom class NAZARA_API NzBoxGeom : public NzPhysGeom
{ {
public: public:
NzBoxGeom(NzPhysWorld* physWorld, const NzVector3f& lengths, const NzMatrix4f& transformMatrix = NzMatrix4f::Identity()); NzBoxGeom(const NzVector3f& lengths, const NzMatrix4f& transformMatrix = NzMatrix4f::Identity());
NzBoxGeom(NzPhysWorld* physWorld, const NzVector3f& lengths, const NzVector3f& translation, const NzQuaternionf& rotation = NzQuaternionf::Identity()); NzBoxGeom(const NzVector3f& lengths, const NzVector3f& translation, const NzQuaternionf& rotation = NzQuaternionf::Identity());
NzBoxf ComputeAABB(const NzMatrix4f& offsetMatrix = NzMatrix4f::Identity(), const NzVector3f& scale = NzVector3f::Unit()) const override;
float ComputeVolume() const override;
NzVector3f GetLengths() const; NzVector3f GetLengths() const;
nzGeomType GetType() const override; nzGeomType GetType() const override;
@ -79,6 +82,9 @@ class NAZARA_API NzBoxGeom : public NzPhysGeom
template<typename... Args> static NzBoxGeomRef New(Args&&... args); template<typename... Args> static NzBoxGeomRef New(Args&&... args);
private: private:
NewtonCollision* CreateHandle(NzPhysWorld* world) const override;
NzMatrix4f m_matrix;
NzVector3f m_lengths; NzVector3f m_lengths;
}; };
@ -92,8 +98,8 @@ using NzCapsuleGeomRef = NzObjectRef<NzCapsuleGeom>;
class NAZARA_API NzCapsuleGeom : public NzPhysGeom class NAZARA_API NzCapsuleGeom : public NzPhysGeom
{ {
public: public:
NzCapsuleGeom(NzPhysWorld* physWorld, float length, float radius, const NzMatrix4f& transformMatrix = NzMatrix4f::Identity()); NzCapsuleGeom(float length, float radius, const NzMatrix4f& transformMatrix = NzMatrix4f::Identity());
NzCapsuleGeom(NzPhysWorld* physWorld, float length, float radius, const NzVector3f& translation, const NzQuaternionf& rotation = NzQuaternionf::Identity()); NzCapsuleGeom(float length, float radius, const NzVector3f& translation, const NzQuaternionf& rotation = NzQuaternionf::Identity());
float GetLength() const; float GetLength() const;
float GetRadius() const; float GetRadius() const;
@ -102,6 +108,9 @@ class NAZARA_API NzCapsuleGeom : public NzPhysGeom
template<typename... Args> static NzCapsuleGeomRef New(Args&&... args); template<typename... Args> static NzCapsuleGeomRef New(Args&&... args);
private: private:
NewtonCollision* CreateHandle(NzPhysWorld* world) const override;
NzMatrix4f m_matrix;
float m_length; float m_length;
float m_radius; float m_radius;
}; };
@ -116,7 +125,7 @@ using NzCompoundGeomRef = NzObjectRef<NzCompoundGeom>;
class NAZARA_API NzCompoundGeom : public NzPhysGeom class NAZARA_API NzCompoundGeom : public NzPhysGeom
{ {
public: public:
NzCompoundGeom(NzPhysWorld* physWorld, NzPhysGeom** geoms, unsigned int geomCount); NzCompoundGeom(NzPhysGeom** geoms, unsigned int geomCount);
const std::vector<NzPhysGeomRef>& GetGeoms() const; const std::vector<NzPhysGeomRef>& GetGeoms() const;
nzGeomType GetType() const override; nzGeomType GetType() const override;
@ -124,6 +133,8 @@ class NAZARA_API NzCompoundGeom : public NzPhysGeom
template<typename... Args> static NzCompoundGeomRef New(Args&&... args); template<typename... Args> static NzCompoundGeomRef New(Args&&... args);
private: private:
NewtonCollision* CreateHandle(NzPhysWorld* world) const override;
std::vector<NzPhysGeomRef> m_geoms; std::vector<NzPhysGeomRef> m_geoms;
}; };
@ -137,8 +148,8 @@ using NzConeGeomRef = NzObjectRef<NzConeGeom>;
class NAZARA_API NzConeGeom : public NzPhysGeom class NAZARA_API NzConeGeom : public NzPhysGeom
{ {
public: public:
NzConeGeom(NzPhysWorld* physWorld, float length, float radius, const NzMatrix4f& transformMatrix = NzMatrix4f::Identity()); NzConeGeom(float length, float radius, const NzMatrix4f& transformMatrix = NzMatrix4f::Identity());
NzConeGeom(NzPhysWorld* physWorld, float length, float radius, const NzVector3f& translation, const NzQuaternionf& rotation = NzQuaternionf::Identity()); NzConeGeom(float length, float radius, const NzVector3f& translation, const NzQuaternionf& rotation = NzQuaternionf::Identity());
float GetLength() const; float GetLength() const;
float GetRadius() const; float GetRadius() const;
@ -147,6 +158,9 @@ class NAZARA_API NzConeGeom : public NzPhysGeom
template<typename... Args> static NzConeGeomRef New(Args&&... args); template<typename... Args> static NzConeGeomRef New(Args&&... args);
private: private:
NewtonCollision* CreateHandle(NzPhysWorld* world) const override;
NzMatrix4f m_matrix;
float m_length; float m_length;
float m_radius; float m_radius;
}; };
@ -161,12 +175,20 @@ using NzConvexHullGeomRef = NzObjectRef<NzConvexHullGeom>;
class NAZARA_API NzConvexHullGeom : public NzPhysGeom class NAZARA_API NzConvexHullGeom : public NzPhysGeom
{ {
public: public:
NzConvexHullGeom(NzPhysWorld* physWorld, const void* vertices, unsigned int vertexCount, unsigned int stride = sizeof(NzVector3f), float tolerance = 0.002f, const NzMatrix4f& transformMatrix = NzMatrix4f::Identity()); NzConvexHullGeom(const void* vertices, unsigned int vertexCount, unsigned int stride = sizeof(NzVector3f), float tolerance = 0.002f, const NzMatrix4f& transformMatrix = NzMatrix4f::Identity());
NzConvexHullGeom(NzPhysWorld* physWorld, const void* vertices, unsigned int vertexCount, unsigned int stride, float tolerance, const NzVector3f& translation, const NzQuaternionf& rotation = NzQuaternionf::Identity()); NzConvexHullGeom(const void* vertices, unsigned int vertexCount, unsigned int stride, float tolerance, const NzVector3f& translation, const NzQuaternionf& rotation = NzQuaternionf::Identity());
nzGeomType GetType() const override; nzGeomType GetType() const override;
template<typename... Args> static NzConvexHullGeomRef New(Args&&... args); template<typename... Args> static NzConvexHullGeomRef New(Args&&... args);
private:
NewtonCollision* CreateHandle(NzPhysWorld* world) const override;
std::vector<NzVector3f> m_vertices;
NzMatrix4f m_matrix;
float m_tolerance;
unsigned int m_vertexStride;
}; };
class NzCylinderGeom; class NzCylinderGeom;
@ -179,8 +201,8 @@ using NzCylinderGeomRef = NzObjectRef<NzCylinderGeom>;
class NAZARA_API NzCylinderGeom : public NzPhysGeom class NAZARA_API NzCylinderGeom : public NzPhysGeom
{ {
public: public:
NzCylinderGeom(NzPhysWorld* physWorld, float length, float radius, const NzMatrix4f& transformMatrix = NzMatrix4f::Identity()); NzCylinderGeom(float length, float radius, const NzMatrix4f& transformMatrix = NzMatrix4f::Identity());
NzCylinderGeom(NzPhysWorld* physWorld, float length, float radius, const NzVector3f& translation, const NzQuaternionf& rotation = NzQuaternionf::Identity()); NzCylinderGeom(float length, float radius, const NzVector3f& translation, const NzQuaternionf& rotation = NzQuaternionf::Identity());
float GetLength() const; float GetLength() const;
float GetRadius() const; float GetRadius() const;
@ -189,6 +211,9 @@ class NAZARA_API NzCylinderGeom : public NzPhysGeom
template<typename... Args> static NzCylinderGeomRef New(Args&&... args); template<typename... Args> static NzCylinderGeomRef New(Args&&... args);
private: private:
NewtonCollision* CreateHandle(NzPhysWorld* world) const override;
NzMatrix4f m_matrix;
float m_length; float m_length;
float m_radius; float m_radius;
}; };
@ -203,11 +228,14 @@ using NzNullGeomRef = NzObjectRef<NzNullGeom>;
class NAZARA_API NzNullGeom : public NzPhysGeom class NAZARA_API NzNullGeom : public NzPhysGeom
{ {
public: public:
NzNullGeom(NzPhysWorld* physWorld); NzNullGeom();
nzGeomType GetType() const override; nzGeomType GetType() const override;
template<typename... Args> static NzNullGeomRef New(Args&&... args); template<typename... Args> static NzNullGeomRef New(Args&&... args);
private:
NewtonCollision* CreateHandle(NzPhysWorld* world) const override;
}; };
class NzSphereGeom; class NzSphereGeom;
@ -220,16 +248,22 @@ using NzSphereGeomRef = NzObjectRef<NzSphereGeom>;
class NAZARA_API NzSphereGeom : public NzPhysGeom class NAZARA_API NzSphereGeom : public NzPhysGeom
{ {
public: public:
NzSphereGeom(NzPhysWorld* physWorld, float radius, const NzMatrix4f& transformMatrix = NzMatrix4f::Identity()); NzSphereGeom(float radius, const NzMatrix4f& transformMatrix = NzMatrix4f::Identity());
NzSphereGeom(NzPhysWorld* physWorld, float radius, const NzVector3f& translation, const NzQuaternionf& rotation = NzQuaternionf::Identity()); NzSphereGeom(float radius, const NzVector3f& translation, const NzQuaternionf& rotation = NzQuaternionf::Identity());
NzVector3f GetRadius() const; NzBoxf ComputeAABB(const NzMatrix4f& offsetMatrix = NzMatrix4f::Identity(), const NzVector3f& scale = NzVector3f::Unit()) const override;
float ComputeVolume() const override;
float GetRadius() const;
nzGeomType GetType() const override; nzGeomType GetType() const override;
template<typename... Args> static NzSphereGeomRef New(Args&&... args); template<typename... Args> static NzSphereGeomRef New(Args&&... args);
private: private:
NzVector3f m_radius; NewtonCollision* CreateHandle(NzPhysWorld* world) const override;
NzVector3f m_position;
float m_radius;
}; };
#include <Nazara/Physics/Geom.inl> #include <Nazara/Physics/Geom.inl>

View File

@ -10,54 +10,59 @@
namespace namespace
{ {
NzPhysGeom* CreateGeomFromPrimitive(NzPhysWorld* physWorld, const NzPrimitive& primitive) NzPhysGeomRef CreateGeomFromPrimitive(const NzPrimitive& primitive)
{ {
switch (primitive.type) switch (primitive.type)
{ {
case nzPrimitiveType_Box: case nzPrimitiveType_Box:
return NzBoxGeom::New(physWorld, primitive.box.lengths, primitive.matrix); return NzBoxGeom::New(primitive.box.lengths, primitive.matrix);
case nzPrimitiveType_Cone: case nzPrimitiveType_Cone:
return NzConeGeom::New(physWorld, primitive.cone.length, primitive.cone.radius, primitive.matrix); return NzConeGeom::New(primitive.cone.length, primitive.cone.radius, primitive.matrix);
case nzPrimitiveType_Plane: case nzPrimitiveType_Plane:
return NzBoxGeom::New(physWorld, NzVector3f(primitive.plane.size.x, 0.01f, primitive.plane.size.y), primitive.matrix); return NzBoxGeom::New(NzVector3f(primitive.plane.size.x, 0.01f, primitive.plane.size.y), primitive.matrix);
///TODO: PlaneGeom? ///TODO: PlaneGeom?
case nzPrimitiveType_Sphere: case nzPrimitiveType_Sphere:
return NzSphereGeom::New(physWorld, primitive.sphere.size, primitive.matrix.GetTranslation()); return NzSphereGeom::New(primitive.sphere.size, primitive.matrix.GetTranslation());
} }
NazaraError("Primitive type not handled (0x" + NzString::Number(primitive.type, 16) + ')'); NazaraError("Primitive type not handled (0x" + NzString::Number(primitive.type, 16) + ')');
return nullptr; return NzPhysGeomRef();
} }
} }
NzPhysGeom::NzPhysGeom(NzPhysWorld* physWorld) :
m_world(physWorld)
{
}
NzPhysGeom::~NzPhysGeom() NzPhysGeom::~NzPhysGeom()
{ {
NewtonDestroyCollision(m_collision); for (auto& pair : m_handles)
NewtonDestroyCollision(pair.second);
} }
NzBoxf NzPhysGeom::ComputeAABB(const NzVector3f& translation, const NzQuaternionf& rotation, const NzVector3f& scale) const NzBoxf NzPhysGeom::ComputeAABB(const NzVector3f& translation, const NzQuaternionf& rotation, const NzVector3f& scale) const
{ {
NzVector3f min, max; return ComputeAABB(NzMatrix4f::Transform(translation, rotation), scale);
NewtonCollisionCalculateAABB(m_collision, NzMatrix4f::Transform(translation, rotation), min, max);
// Et on applique le scale à la fin
return NzBoxf(scale*min, scale*max);
} }
NzBoxf NzPhysGeom::ComputeAABB(const NzMatrix4f& offsetMatrix) const NzBoxf NzPhysGeom::ComputeAABB(const NzMatrix4f& offsetMatrix, const NzVector3f& scale) const
{ {
NzVector3f min, max; NzVector3f min, max;
NewtonCollisionCalculateAABB(m_collision, offsetMatrix, min, max);
return NzBoxf(min, max); // Si nous n'avons aucune instance, nous en créons une temporaire
if (m_handles.empty())
{
NzPhysWorld world;
NewtonCollision* collision = CreateHandle(&world);
{
NewtonCollisionCalculateAABB(collision, offsetMatrix, min, max);
}
NewtonDestroyCollision(collision);
}
else // Sinon on utilise une instance au hasard (elles sont toutes identiques de toute façon)
NewtonCollisionCalculateAABB(m_handles.begin()->second, offsetMatrix, min, max);
return NzBoxf(scale * min, scale * max);
} }
void NzPhysGeom::ComputeInertialMatrix(NzVector3f* inertia, NzVector3f* center) const void NzPhysGeom::ComputeInertialMatrix(NzVector3f* inertia, NzVector3f* center) const
@ -65,7 +70,19 @@ void NzPhysGeom::ComputeInertialMatrix(NzVector3f* inertia, NzVector3f* center)
float inertiaMatrix[3]; float inertiaMatrix[3];
float origin[3]; float origin[3];
NewtonConvexCollisionCalculateInertialMatrix(m_collision, inertiaMatrix, origin); // Si nous n'avons aucune instance, nous en créons une temporaire
if (m_handles.empty())
{
NzPhysWorld world;
NewtonCollision* collision = CreateHandle(&world);
{
NewtonConvexCollisionCalculateInertialMatrix(collision, inertiaMatrix, origin);
}
NewtonDestroyCollision(collision);
}
else // Sinon on utilise une instance au hasard (elles sont toutes identiques de toute façon)
NewtonConvexCollisionCalculateInertialMatrix(m_handles.begin()->second, inertiaMatrix, origin);
if (inertia) if (inertia)
inertia->Set(inertiaMatrix); inertia->Set(inertiaMatrix);
@ -76,20 +93,35 @@ void NzPhysGeom::ComputeInertialMatrix(NzVector3f* inertia, NzVector3f* center)
float NzPhysGeom::ComputeVolume() const float NzPhysGeom::ComputeVolume() const
{ {
return NewtonConvexCollisionCalculateVolume(m_collision); float volume;
}
NewtonCollision* NzPhysGeom::GetHandle() const // Si nous n'avons aucune instance, nous en créons une temporaire
if (m_handles.empty())
{ {
return m_collision; NzPhysWorld world;
}
NzPhysWorld* NzPhysGeom::GetWorld() const NewtonCollision* collision = CreateHandle(&world);
{ {
return m_world; volume = NewtonConvexCollisionCalculateVolume(collision);
}
NewtonDestroyCollision(collision);
}
else // Sinon on utilise une instance au hasard (elles sont toutes identiques de toute façon)
volume = NewtonConvexCollisionCalculateVolume(m_handles.begin()->second);
return volume;
} }
NzPhysGeomRef NzPhysGeom::Build(NzPhysWorld* physWorld, const NzPrimitiveList& list) NewtonCollision* NzPhysGeom::GetHandle(NzPhysWorld* world) const
{
auto it = m_handles.find(world);
if (it == m_handles.end())
it = m_handles.insert(std::make_pair(world, CreateHandle(world))).first;
return it->second;
}
NzPhysGeomRef NzPhysGeom::Build(const NzPrimitiveList& list)
{ {
unsigned int primitiveCount = list.GetSize(); unsigned int primitiveCount = list.GetSize();
@ -106,30 +138,45 @@ NzPhysGeomRef NzPhysGeom::Build(NzPhysWorld* physWorld, const NzPrimitiveList& l
std::vector<NzPhysGeom*> geoms(primitiveCount); std::vector<NzPhysGeom*> geoms(primitiveCount);
for (unsigned int i = 0; i < primitiveCount; ++i) for (unsigned int i = 0; i < primitiveCount; ++i)
geoms[i] = CreateGeomFromPrimitive(physWorld, list.GetPrimitive(i)); geoms[i] = CreateGeomFromPrimitive(list.GetPrimitive(i));
return NzCompoundGeom::New(physWorld, &geoms[0], primitiveCount); return NzCompoundGeom::New(&geoms[0], primitiveCount);
} }
else else
return CreateGeomFromPrimitive(physWorld, list.GetPrimitive(0)); return CreateGeomFromPrimitive(list.GetPrimitive(0));
} }
NzPhysGeomLibrary::LibraryMap NzPhysGeom::s_library; NzPhysGeomLibrary::LibraryMap NzPhysGeom::s_library;
/********************************** BoxGeom **********************************/ /********************************** BoxGeom **********************************/
NzBoxGeom::NzBoxGeom(NzPhysWorld* physWorld, const NzVector3f& lengths, const NzMatrix4f& transformMatrix) : NzBoxGeom::NzBoxGeom(const NzVector3f& lengths, const NzMatrix4f& transformMatrix) :
NzPhysGeom(physWorld), m_matrix(transformMatrix),
m_lengths(lengths) m_lengths(lengths)
{ {
m_collision = NewtonCreateBox(physWorld->GetHandle(), lengths.x, lengths.y, lengths.z, 0, transformMatrix);
} }
NzBoxGeom::NzBoxGeom(NzPhysWorld* physWorld, const NzVector3f& lengths, const NzVector3f& translation, const NzQuaternionf& rotation) : NzBoxGeom::NzBoxGeom(const NzVector3f& lengths, const NzVector3f& translation, const NzQuaternionf& rotation) :
NzBoxGeom(physWorld, lengths, NzMatrix4f::Transform(translation, rotation)) NzBoxGeom(lengths, NzMatrix4f::Transform(translation, rotation))
{ {
} }
NzBoxf NzBoxGeom::ComputeAABB(const NzMatrix4f& offsetMatrix, const NzVector3f& scale) const
{
NzVector3f halfLengths(m_lengths * 0.5f);
NzBoxf aabb(-halfLengths.x, -halfLengths.y, -halfLengths.z, m_lengths.x, m_lengths.y, m_lengths.z);
aabb.Transform(offsetMatrix, true);
aabb *= scale;
return aabb;
}
float NzBoxGeom::ComputeVolume() const
{
return m_lengths.x * m_lengths.y * m_lengths.z;
}
NzVector3f NzBoxGeom::GetLengths() const NzVector3f NzBoxGeom::GetLengths() const
{ {
return m_lengths; return m_lengths;
@ -140,18 +187,22 @@ nzGeomType NzBoxGeom::GetType() const
return nzGeomType_Box; return nzGeomType_Box;
} }
NewtonCollision* NzBoxGeom::CreateHandle(NzPhysWorld* world) const
{
return NewtonCreateBox(world->GetHandle(), m_lengths.x, m_lengths.y, m_lengths.z, 0, m_matrix);
}
/******************************** CapsuleGeom ********************************/ /******************************** CapsuleGeom ********************************/
NzCapsuleGeom::NzCapsuleGeom(NzPhysWorld* physWorld, float length, float radius, const NzMatrix4f& transformMatrix) : NzCapsuleGeom::NzCapsuleGeom(float length, float radius, const NzMatrix4f& transformMatrix) :
NzPhysGeom(physWorld), m_matrix(transformMatrix),
m_length(length), m_length(length),
m_radius(radius) m_radius(radius)
{ {
m_collision = NewtonCreateCapsule(physWorld->GetHandle(), radius, length, 0, transformMatrix);
} }
NzCapsuleGeom::NzCapsuleGeom(NzPhysWorld* physWorld, float length, float radius, const NzVector3f& translation, const NzQuaternionf& rotation) : NzCapsuleGeom::NzCapsuleGeom(float length, float radius, const NzVector3f& translation, const NzQuaternionf& rotation) :
NzCapsuleGeom(physWorld, length, radius, NzMatrix4f::Transform(translation, rotation)) NzCapsuleGeom(length, radius, NzMatrix4f::Transform(translation, rotation))
{ {
} }
@ -170,32 +221,20 @@ nzGeomType NzCapsuleGeom::GetType() const
return nzGeomType_Capsule; return nzGeomType_Capsule;
} }
NewtonCollision* NzCapsuleGeom::CreateHandle(NzPhysWorld* world) const
{
return NewtonCreateCapsule(world->GetHandle(), m_radius, m_length, 0, m_matrix);
}
/******************************* CompoundGeom ********************************/ /******************************* CompoundGeom ********************************/
NzCompoundGeom::NzCompoundGeom(NzPhysWorld* physWorld, NzPhysGeom** geoms, unsigned int geomCount) : NzCompoundGeom::NzCompoundGeom(NzPhysGeom** geoms, unsigned int geomCount)
NzPhysGeom(physWorld)
{ {
m_collision = NewtonCreateCompoundCollision(physWorld->GetHandle(), 0);
NewtonCompoundCollisionBeginAddRemove(m_collision);
m_geoms.reserve(geomCount); m_geoms.reserve(geomCount);
for (unsigned int i = 0; i < geomCount; ++i) for (unsigned int i = 0; i < geomCount; ++i)
{
if (geoms[i]->GetType() == nzGeomType_Compound)
{
NzCompoundGeom* compoundGeom = static_cast<NzCompoundGeom*>(geoms[i]);
for (const NzPhysGeomRef& geom : compoundGeom->GetGeoms())
NewtonCompoundCollisionAddSubCollision(m_collision, geom->GetHandle());
}
else
NewtonCompoundCollisionAddSubCollision(m_collision, geoms[i]->GetHandle());
m_geoms.emplace_back(geoms[i]); m_geoms.emplace_back(geoms[i]);
} }
NewtonCompoundCollisionEndAddRemove(m_collision);
}
const std::vector<NzPhysGeomRef>& NzCompoundGeom::GetGeoms() const const std::vector<NzPhysGeomRef>& NzCompoundGeom::GetGeoms() const
{ {
return m_geoms; return m_geoms;
@ -206,18 +245,38 @@ nzGeomType NzCompoundGeom::GetType() const
return nzGeomType_Compound; return nzGeomType_Compound;
} }
NewtonCollision* NzCompoundGeom::CreateHandle(NzPhysWorld* world) const
{
NewtonCollision* compoundCollision = NewtonCreateCompoundCollision(world->GetHandle(), 0);
NewtonCompoundCollisionBeginAddRemove(compoundCollision);
for (const NzPhysGeomRef& geom : m_geoms)
{
if (geom->GetType() == nzGeomType_Compound)
{
NzCompoundGeom* compoundGeom = static_cast<NzCompoundGeom*>(geom.Get());
for (const NzPhysGeomRef& piece : compoundGeom->GetGeoms())
NewtonCompoundCollisionAddSubCollision(compoundCollision, piece->GetHandle(world));
}
else
NewtonCompoundCollisionAddSubCollision(compoundCollision, geom->GetHandle(world));
}
NewtonCompoundCollisionEndAddRemove(compoundCollision);
return compoundCollision;
}
/********************************* ConeGeom **********************************/ /********************************* ConeGeom **********************************/
NzConeGeom::NzConeGeom(NzPhysWorld* physWorld, float length, float radius, const NzMatrix4f& transformMatrix) : NzConeGeom::NzConeGeom(float length, float radius, const NzMatrix4f& transformMatrix) :
NzPhysGeom(physWorld), m_matrix(transformMatrix),
m_length(length), m_length(length),
m_radius(radius) m_radius(radius)
{ {
m_collision = NewtonCreateCone(physWorld->GetHandle(), radius, length, 0, transformMatrix);
} }
NzConeGeom::NzConeGeom(NzPhysWorld* physWorld, float length, float radius, const NzVector3f& translation, const NzQuaternionf& rotation) : NzConeGeom::NzConeGeom(float length, float radius, const NzVector3f& translation, const NzQuaternionf& rotation) :
NzConeGeom(physWorld, length, radius, NzMatrix4f::Transform(translation, rotation)) NzConeGeom(length, radius, NzMatrix4f::Transform(translation, rotation))
{ {
} }
@ -236,16 +295,32 @@ nzGeomType NzConeGeom::GetType() const
return nzGeomType_Cone; return nzGeomType_Cone;
} }
/****************************** ConvexHullGeom *******************************/ NewtonCollision* NzConeGeom::CreateHandle(NzPhysWorld* world) const
NzConvexHullGeom::NzConvexHullGeom(NzPhysWorld* physWorld, const void* vertices, unsigned int vertexCount, unsigned int stride, float tolerance, const NzMatrix4f& transformMatrix) :
NzPhysGeom(physWorld)
{ {
m_collision = NewtonCreateConvexHull(physWorld->GetHandle(), vertexCount, reinterpret_cast<const float*>(vertices), stride, tolerance, 0, transformMatrix); return NewtonCreateCone(world->GetHandle(), m_radius, m_length, 0, m_matrix);
} }
NzConvexHullGeom::NzConvexHullGeom(NzPhysWorld* physWorld, const void* vertices, unsigned int vertexCount, unsigned int stride, float tolerance, const NzVector3f& translation, const NzQuaternionf& rotation) : /****************************** ConvexHullGeom *******************************/
NzConvexHullGeom(physWorld, vertices, vertexCount, stride, tolerance, NzMatrix4f::Transform(translation, rotation))
NzConvexHullGeom::NzConvexHullGeom(const void* vertices, unsigned int vertexCount, unsigned int stride, float tolerance, const NzMatrix4f& transformMatrix) :
m_matrix(transformMatrix),
m_tolerance(tolerance),
m_vertexStride(stride)
{
const nzUInt8* ptr = static_cast<const nzUInt8*>(vertices);
m_vertices.resize(vertexCount);
if (stride != sizeof(NzVector3f))
{
for (unsigned int i = 0; i < vertexCount; ++i)
m_vertices[i] = *reinterpret_cast<const NzVector3f*>(ptr + stride*i);
}
else // Fast path
std::memcpy(m_vertices.data(), vertices, vertexCount*sizeof(NzVector3f));
}
NzConvexHullGeom::NzConvexHullGeom(const void* vertices, unsigned int vertexCount, unsigned int stride, float tolerance, const NzVector3f& translation, const NzQuaternionf& rotation) :
NzConvexHullGeom(vertices, vertexCount, stride, tolerance, NzMatrix4f::Transform(translation, rotation))
{ {
} }
@ -254,18 +329,22 @@ nzGeomType NzConvexHullGeom::GetType() const
return nzGeomType_Compound; return nzGeomType_Compound;
} }
NewtonCollision* NzConvexHullGeom::CreateHandle(NzPhysWorld* world) const
{
return NewtonCreateConvexHull(world->GetHandle(), m_vertices.size(), reinterpret_cast<const float*>(m_vertices.data()), sizeof(NzVector3f), m_tolerance, 0, m_matrix);
}
/******************************* CylinderGeom ********************************/ /******************************* CylinderGeom ********************************/
NzCylinderGeom::NzCylinderGeom(NzPhysWorld* physWorld, float length, float radius, const NzMatrix4f& transformMatrix) : NzCylinderGeom::NzCylinderGeom(float length, float radius, const NzMatrix4f& transformMatrix) :
NzPhysGeom(physWorld), m_matrix(transformMatrix),
m_length(length), m_length(length),
m_radius(radius) m_radius(radius)
{ {
m_collision = NewtonCreateCylinder(physWorld->GetHandle(), radius, length, 0, transformMatrix);
} }
NzCylinderGeom::NzCylinderGeom(NzPhysWorld* physWorld, float length, float radius, const NzVector3f& translation, const NzQuaternionf& rotation) : NzCylinderGeom::NzCylinderGeom(float length, float radius, const NzVector3f& translation, const NzQuaternionf& rotation) :
NzCylinderGeom(physWorld, length, radius, NzMatrix4f::Transform(translation, rotation)) NzCylinderGeom(length, radius, NzMatrix4f::Transform(translation, rotation))
{ {
} }
@ -284,12 +363,15 @@ nzGeomType NzCylinderGeom::GetType() const
return nzGeomType_Cylinder; return nzGeomType_Cylinder;
} }
NewtonCollision* NzCylinderGeom::CreateHandle(NzPhysWorld* world) const
{
return NewtonCreateCylinder(world->GetHandle(), m_radius, m_length, 0, m_matrix);
}
/********************************* NullGeom **********************************/ /********************************* NullGeom **********************************/
NzNullGeom::NzNullGeom(NzPhysWorld* physWorld) : NzNullGeom::NzNullGeom()
NzPhysGeom(physWorld)
{ {
m_collision = NewtonCreateNull(physWorld->GetHandle());
} }
nzGeomType NzNullGeom::GetType() const nzGeomType NzNullGeom::GetType() const
@ -297,21 +379,39 @@ nzGeomType NzNullGeom::GetType() const
return nzGeomType_Null; return nzGeomType_Null;
} }
NewtonCollision* NzNullGeom::CreateHandle(NzPhysWorld* world) const
{
return NewtonCreateNull(world->GetHandle());
}
/******************************** SphereGeom *********************************/ /******************************** SphereGeom *********************************/
NzSphereGeom::NzSphereGeom(NzPhysWorld* physWorld, float radius, const NzMatrix4f& transformMatrix) : NzSphereGeom::NzSphereGeom(float radius, const NzMatrix4f& transformMatrix) :
NzPhysGeom(physWorld), NzSphereGeom(radius, transformMatrix.GetTranslation())
{
}
NzSphereGeom::NzSphereGeom(float radius, const NzVector3f& translation, const NzQuaternionf& rotation) :
m_position(translation),
m_radius(radius) m_radius(radius)
{ {
m_collision = NewtonCreateSphere(physWorld->GetHandle(), radius, 0, transformMatrix); NazaraUnused(rotation);
} }
NzSphereGeom::NzSphereGeom(NzPhysWorld* physWorld, float radius, const NzVector3f& translation, const NzQuaternionf& rotation) : NzBoxf NzSphereGeom::ComputeAABB(const NzMatrix4f& offsetMatrix, const NzVector3f& scale) const
NzSphereGeom(physWorld, radius, NzMatrix4f::Transform(translation, rotation))
{ {
NzVector3f size(m_radius * scale);
NzVector3f position(offsetMatrix.GetTranslation());
return NzBoxf(position - size, position + size);
} }
NzVector3f NzSphereGeom::GetRadius() const float NzSphereGeom::ComputeVolume() const
{
return M_PI * m_radius * m_radius * m_radius / 3.f;
}
float NzSphereGeom::GetRadius() const
{ {
return m_radius; return m_radius;
} }
@ -320,3 +420,8 @@ nzGeomType NzSphereGeom::GetType() const
{ {
return nzGeomType_Sphere; return nzGeomType_Sphere;
} }
NewtonCollision* NzSphereGeom::CreateHandle(NzPhysWorld* world) const
{
return NewtonCreateSphere(world->GetHandle(), m_radius, 0, NzMatrix4f::Translate(m_position));
}