Physics/Geom: Improved Geom class
Renamed BaseGeom to PhysGeom, made it a reference-counted class Former-commit-id: 5b3a2921addb537c2744346edc9c74eafb3260b0
This commit is contained in:
parent
697e6714db
commit
1642536a0e
|
|
@ -8,8 +8,12 @@
|
|||
#define NAZARA_GEOM_HPP
|
||||
|
||||
#include <Nazara/Prerequesites.hpp>
|
||||
#include <Nazara/Core/PrimitiveList.hpp>
|
||||
#include <Nazara/Core/NonCopyable.hpp>
|
||||
#include <Nazara/Core/PrimitiveList.hpp>
|
||||
#include <Nazara/Core/ObjectLibrary.hpp>
|
||||
#include <Nazara/Core/ObjectListenerWrapper.hpp>
|
||||
#include <Nazara/Core/ObjectRef.hpp>
|
||||
#include <Nazara/Core/RefCounted.hpp>
|
||||
#include <Nazara/Math/Box.hpp>
|
||||
#include <Nazara/Math/Quaternion.hpp>
|
||||
#include <Nazara/Math/Vector3.hpp>
|
||||
|
|
@ -21,14 +25,21 @@
|
|||
///TODO: SceneGeom
|
||||
///TODO: TreeGeom
|
||||
|
||||
class NzPhysGeom;
|
||||
class NzPhysWorld;
|
||||
struct NewtonCollision;
|
||||
|
||||
class NAZARA_API NzBaseGeom : NzNonCopyable
|
||||
using NzPhysGeomConstListener = NzObjectListenerWrapper<const NzPhysGeom>;
|
||||
using NzPhysGeomConstRef = NzObjectRef<const NzPhysGeom>;
|
||||
using NzPhysGeomLibrary = NzObjectLibrary<NzPhysGeom>;
|
||||
using NzPhysGeomListener = NzObjectListenerWrapper<NzPhysGeom>;
|
||||
using NzPhysGeomRef = NzObjectRef<NzPhysGeom>;
|
||||
|
||||
class NAZARA_API NzPhysGeom : public NzRefCounted, NzNonCopyable
|
||||
{
|
||||
public:
|
||||
NzBaseGeom(NzPhysWorld* physWorld);
|
||||
virtual ~NzBaseGeom();
|
||||
NzPhysGeom(NzPhysWorld* physWorld);
|
||||
virtual ~NzPhysGeom();
|
||||
|
||||
virtual NzBoxf ComputeAABB(const NzVector3f& translation, const NzQuaternionf& rotation, const NzVector3f& scale) const;
|
||||
virtual NzBoxf ComputeAABB(const NzMatrix4f& offsetMatrix = NzMatrix4f::Identity()) const;
|
||||
|
|
@ -40,14 +51,23 @@ class NAZARA_API NzBaseGeom : NzNonCopyable
|
|||
|
||||
NzPhysWorld* GetWorld() const;
|
||||
|
||||
static NzBaseGeom* Build(NzPhysWorld* physWorld, const NzPrimitiveList& list);
|
||||
static NzPhysGeomRef Build(NzPhysWorld* physWorld, const NzPrimitiveList& list);
|
||||
|
||||
protected:
|
||||
NewtonCollision* m_collision;
|
||||
NzPhysWorld* m_world;
|
||||
|
||||
static NzPhysGeomLibrary::LibraryMap s_library;
|
||||
};
|
||||
|
||||
class NAZARA_API NzBoxGeom : public NzBaseGeom
|
||||
class NzBoxGeom;
|
||||
|
||||
using NzBoxGeomConstListener = NzObjectListenerWrapper<const NzBoxGeom>;
|
||||
using NzBoxGeomConstRef = NzObjectRef<const NzBoxGeom>;
|
||||
using NzBoxGeomListener = NzObjectListenerWrapper<NzBoxGeom>;
|
||||
using NzBoxGeomRef = NzObjectRef<NzBoxGeom>;
|
||||
|
||||
class NAZARA_API NzBoxGeom : public NzPhysGeom
|
||||
{
|
||||
public:
|
||||
NzBoxGeom(NzPhysWorld* physWorld, const NzVector3f& lengths, const NzMatrix4f& transformMatrix = NzMatrix4f::Identity());
|
||||
|
|
@ -56,11 +76,20 @@ class NAZARA_API NzBoxGeom : public NzBaseGeom
|
|||
NzVector3f GetLengths() const;
|
||||
nzGeomType GetType() const override;
|
||||
|
||||
template<typename... Args> static NzBoxGeomRef New(Args&&... args);
|
||||
|
||||
private:
|
||||
NzVector3f m_lengths;
|
||||
};
|
||||
|
||||
class NAZARA_API NzCapsuleGeom : public NzBaseGeom
|
||||
class NzCapsuleGeom;
|
||||
|
||||
using NzCapsuleGeomConstListener = NzObjectListenerWrapper<const NzCapsuleGeom>;
|
||||
using NzCapsuleGeomConstRef = NzObjectRef<const NzCapsuleGeom>;
|
||||
using NzCapsuleGeomListener = NzObjectListenerWrapper<NzCapsuleGeom>;
|
||||
using NzCapsuleGeomRef = NzObjectRef<NzCapsuleGeom>;
|
||||
|
||||
class NAZARA_API NzCapsuleGeom : public NzPhysGeom
|
||||
{
|
||||
public:
|
||||
NzCapsuleGeom(NzPhysWorld* physWorld, float length, float radius, const NzMatrix4f& transformMatrix = NzMatrix4f::Identity());
|
||||
|
|
@ -70,20 +99,38 @@ class NAZARA_API NzCapsuleGeom : public NzBaseGeom
|
|||
float GetRadius() const;
|
||||
nzGeomType GetType() const override;
|
||||
|
||||
template<typename... Args> static NzCapsuleGeomRef New(Args&&... args);
|
||||
|
||||
private:
|
||||
float m_length;
|
||||
float m_radius;
|
||||
};
|
||||
|
||||
class NAZARA_API NzCompoundGeom : public NzBaseGeom
|
||||
class NzCompoundGeom;
|
||||
|
||||
using NzCompoundGeomConstListener = NzObjectListenerWrapper<const NzCompoundGeom>;
|
||||
using NzCompoundGeomConstRef = NzObjectRef<const NzCompoundGeom>;
|
||||
using NzCompoundGeomListener = NzObjectListenerWrapper<NzCompoundGeom>;
|
||||
using NzCompoundGeomRef = NzObjectRef<NzCompoundGeom>;
|
||||
|
||||
class NAZARA_API NzCompoundGeom : public NzPhysGeom
|
||||
{
|
||||
public:
|
||||
NzCompoundGeom(NzPhysWorld* physWorld, NzBaseGeom** geoms, unsigned int geomCount);
|
||||
NzCompoundGeom(NzPhysWorld* physWorld, NzPhysGeom** geoms, unsigned int geomCount);
|
||||
|
||||
nzGeomType GetType() const override;
|
||||
|
||||
template<typename... Args> static NzCompoundGeomRef New(Args&&... args);
|
||||
};
|
||||
|
||||
class NAZARA_API NzConeGeom : public NzBaseGeom
|
||||
class NzConeGeom;
|
||||
|
||||
using NzConeGeomConstListener = NzObjectListenerWrapper<const NzConeGeom>;
|
||||
using NzConeGeomConstRef = NzObjectRef<const NzConeGeom>;
|
||||
using NzConeGeomListener = NzObjectListenerWrapper<NzConeGeom>;
|
||||
using NzConeGeomRef = NzObjectRef<NzConeGeom>;
|
||||
|
||||
class NAZARA_API NzConeGeom : public NzPhysGeom
|
||||
{
|
||||
public:
|
||||
NzConeGeom(NzPhysWorld* physWorld, float length, float radius, const NzMatrix4f& transformMatrix = NzMatrix4f::Identity());
|
||||
|
|
@ -93,21 +140,39 @@ class NAZARA_API NzConeGeom : public NzBaseGeom
|
|||
float GetRadius() const;
|
||||
nzGeomType GetType() const override;
|
||||
|
||||
template<typename... Args> static NzConeGeomRef New(Args&&... args);
|
||||
|
||||
private:
|
||||
float m_length;
|
||||
float m_radius;
|
||||
};
|
||||
|
||||
class NAZARA_API NzConvexHullGeom : public NzBaseGeom
|
||||
class NzConvexHullGeom;
|
||||
|
||||
using NzConvexHullGeomConstListener = NzObjectListenerWrapper<const NzConvexHullGeom>;
|
||||
using NzConvexHullGeomConstRef = NzObjectRef<const NzConvexHullGeom>;
|
||||
using NzConvexHullGeomListener = NzObjectListenerWrapper<NzConvexHullGeom>;
|
||||
using NzConvexHullGeomRef = NzObjectRef<NzConvexHullGeom>;
|
||||
|
||||
class NAZARA_API NzConvexHullGeom : public NzPhysGeom
|
||||
{
|
||||
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(NzPhysWorld* physWorld, const void* vertices, unsigned int vertexCount, unsigned int stride, float tolerance, const NzVector3f& translation, const NzQuaternionf& rotation = NzQuaternionf::Identity());
|
||||
|
||||
nzGeomType GetType() const override;
|
||||
|
||||
template<typename... Args> static NzConvexHullGeomRef New(Args&&... args);
|
||||
};
|
||||
|
||||
class NAZARA_API NzCylinderGeom : public NzBaseGeom
|
||||
class NzCylinderGeom;
|
||||
|
||||
using NzCylinderGeomConstListener = NzObjectListenerWrapper<const NzCylinderGeom>;
|
||||
using NzCylinderGeomConstRef = NzObjectRef<const NzCylinderGeom>;
|
||||
using NzCylinderGeomListener = NzObjectListenerWrapper<NzCylinderGeom>;
|
||||
using NzCylinderGeomRef = NzObjectRef<NzCylinderGeom>;
|
||||
|
||||
class NAZARA_API NzCylinderGeom : public NzPhysGeom
|
||||
{
|
||||
public:
|
||||
NzCylinderGeom(NzPhysWorld* physWorld, float length, float radius, const NzMatrix4f& transformMatrix = NzMatrix4f::Identity());
|
||||
|
|
@ -117,20 +182,38 @@ class NAZARA_API NzCylinderGeom : public NzBaseGeom
|
|||
float GetRadius() const;
|
||||
nzGeomType GetType() const override;
|
||||
|
||||
template<typename... Args> static NzCylinderGeomRef New(Args&&... args);
|
||||
|
||||
private:
|
||||
float m_length;
|
||||
float m_radius;
|
||||
};
|
||||
|
||||
class NAZARA_API NzNullGeom : public NzBaseGeom
|
||||
class NzNullGeom;
|
||||
|
||||
using NzNullGeomConstListener = NzObjectListenerWrapper<const NzNullGeom>;
|
||||
using NzNullGeomConstRef = NzObjectRef<const NzNullGeom>;
|
||||
using NzNullGeomListener = NzObjectListenerWrapper<NzNullGeom>;
|
||||
using NzNullGeomRef = NzObjectRef<NzNullGeom>;
|
||||
|
||||
class NAZARA_API NzNullGeom : public NzPhysGeom
|
||||
{
|
||||
public:
|
||||
NzNullGeom(NzPhysWorld* physWorld);
|
||||
|
||||
nzGeomType GetType() const override;
|
||||
|
||||
template<typename... Args> static NzNullGeomRef New(Args&&... args);
|
||||
};
|
||||
|
||||
class NAZARA_API NzSphereGeom : public NzBaseGeom
|
||||
class NzSphereGeom;
|
||||
|
||||
using NzSphereGeomConstListener = NzObjectListenerWrapper<const NzSphereGeom>;
|
||||
using NzSphereGeomConstRef = NzObjectRef<const NzSphereGeom>;
|
||||
using NzSphereGeomListener = NzObjectListenerWrapper<NzSphereGeom>;
|
||||
using NzSphereGeomRef = NzObjectRef<NzSphereGeom>;
|
||||
|
||||
class NAZARA_API NzSphereGeom : public NzPhysGeom
|
||||
{
|
||||
public:
|
||||
NzSphereGeom(NzPhysWorld* physWorld, float radius, const NzMatrix4f& transformMatrix = NzMatrix4f::Identity());
|
||||
|
|
@ -139,8 +222,12 @@ class NAZARA_API NzSphereGeom : public NzBaseGeom
|
|||
NzVector3f GetRadius() const;
|
||||
nzGeomType GetType() const override;
|
||||
|
||||
template<typename... Args> static NzSphereGeomRef New(Args&&... args);
|
||||
|
||||
private:
|
||||
NzVector3f m_radius;
|
||||
};
|
||||
|
||||
#include <Nazara/Physics/Geom.inl>
|
||||
|
||||
#endif // NAZARA_PHYSWORLD_HPP
|
||||
|
|
|
|||
|
|
@ -0,0 +1,80 @@
|
|||
// Copyright (C) 2015 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 <memory>
|
||||
#include <Nazara/Physics/Debug.hpp>
|
||||
|
||||
template<typename... Args>
|
||||
NzBoxGeomRef NzBoxGeom::New(Args&&... args)
|
||||
{
|
||||
std::unique_ptr<NzBoxGeom> object(new NzBoxGeom(std::forward<Args>(args)...));
|
||||
object->SetPersistent(false);
|
||||
|
||||
return object.release();
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
NzCapsuleGeomRef NzCapsuleGeom::New(Args&&... args)
|
||||
{
|
||||
std::unique_ptr<NzCapsuleGeom> object(new NzCapsuleGeom(std::forward<Args>(args)...));
|
||||
object->SetPersistent(false);
|
||||
|
||||
return object.release();
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
NzCompoundGeomRef NzCompoundGeom::New(Args&&... args)
|
||||
{
|
||||
std::unique_ptr<NzCompoundGeom> object(new NzCompoundGeom(std::forward<Args>(args)...));
|
||||
object->SetPersistent(false);
|
||||
|
||||
return object.release();
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
NzConeGeomRef NzConeGeom::New(Args&&... args)
|
||||
{
|
||||
std::unique_ptr<NzConeGeom> object(new NzConeGeom(std::forward<Args>(args)...));
|
||||
object->SetPersistent(false);
|
||||
|
||||
return object.release();
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
NzConvexHullGeomRef NzConvexHullGeom::New(Args&&... args)
|
||||
{
|
||||
std::unique_ptr<NzConvexHullGeom> object(new NzConvexHullGeom(std::forward<Args>(args)...));
|
||||
object->SetPersistent(false);
|
||||
|
||||
return object.release();
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
NzCylinderGeomRef NzCylinderGeom::New(Args&&... args)
|
||||
{
|
||||
std::unique_ptr<NzCylinderGeom> object(new NzCylinderGeom(std::forward<Args>(args)...));
|
||||
object->SetPersistent(false);
|
||||
|
||||
return object.release();
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
NzNullGeomRef NzNullGeom::New(Args&&... args)
|
||||
{
|
||||
std::unique_ptr<NzNullGeom> object(new NzNullGeom(std::forward<Args>(args)...));
|
||||
object->SetPersistent(false);
|
||||
|
||||
return object.release();
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
NzSphereGeomRef NzSphereGeom::New(Args&&... args)
|
||||
{
|
||||
std::unique_ptr<NzSphereGeom> object(new NzSphereGeom(std::forward<Args>(args)...));
|
||||
object->SetPersistent(false);
|
||||
|
||||
return object.release();
|
||||
}
|
||||
|
||||
#include <Nazara/Physics/DebugOff.hpp>
|
||||
|
|
@ -13,8 +13,8 @@
|
|||
#include <Nazara/Math/Matrix4.hpp>
|
||||
#include <Nazara/Math/Quaternion.hpp>
|
||||
#include <Nazara/Math/Vector3.hpp>
|
||||
#include <Nazara/Physics/Geom.hpp>
|
||||
|
||||
class NzBaseGeom;
|
||||
class NzPhysWorld;
|
||||
struct NewtonBody;
|
||||
|
||||
|
|
@ -22,7 +22,7 @@ class NAZARA_API NzPhysObject : NzNonCopyable
|
|||
{
|
||||
public:
|
||||
NzPhysObject(NzPhysWorld* world, const NzMatrix4f& mat = NzMatrix4f::Identity());
|
||||
NzPhysObject(NzPhysWorld* world, const NzBaseGeom* geom, const NzMatrix4f& mat = NzMatrix4f::Identity());
|
||||
NzPhysObject(NzPhysWorld* world, NzPhysGeomRef geom, const NzMatrix4f& mat = NzMatrix4f::Identity());
|
||||
~NzPhysObject();
|
||||
|
||||
void AddForce(const NzVector3f& force, nzCoordSys coordSys = nzCoordSys_Global);
|
||||
|
|
@ -31,7 +31,9 @@ class NAZARA_API NzPhysObject : NzNonCopyable
|
|||
|
||||
void EnableAutoSleep(bool autoSleep);
|
||||
|
||||
NzBoxf GetAABB() const;
|
||||
NzVector3f GetAngularVelocity() const;
|
||||
const NzPhysGeomRef& GetGeom() const;
|
||||
float GetGravityFactor() const;
|
||||
NewtonBody* GetHandle() const;
|
||||
float GetMass() const;
|
||||
|
|
@ -45,6 +47,7 @@ class NAZARA_API NzPhysObject : NzNonCopyable
|
|||
bool IsMoveable() const;
|
||||
bool IsSleeping() const;
|
||||
|
||||
void SetGeom(NzPhysGeomRef geom);
|
||||
void SetGravityFactor(float gravityFactor);
|
||||
void SetMass(float mass);
|
||||
void SetMassCenter(const NzVector3f& center);
|
||||
|
|
@ -60,9 +63,8 @@ class NAZARA_API NzPhysObject : NzNonCopyable
|
|||
NzVector3f m_forceAccumulator;
|
||||
NzVector3f m_torqueAccumulator;
|
||||
NewtonBody* m_body;
|
||||
const NzBaseGeom* m_geom;
|
||||
NzPhysGeomRef m_geom;
|
||||
NzPhysWorld* m_world;
|
||||
bool m_ownsGeom;
|
||||
float m_gravityFactor;
|
||||
float m_mass;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -10,22 +10,22 @@
|
|||
|
||||
namespace
|
||||
{
|
||||
NzBaseGeom* CreateGeomFromPrimitive(NzPhysWorld* physWorld, const NzPrimitive& primitive)
|
||||
NzPhysGeom* CreateGeomFromPrimitive(NzPhysWorld* physWorld, const NzPrimitive& primitive)
|
||||
{
|
||||
switch (primitive.type)
|
||||
{
|
||||
case nzPrimitiveType_Box:
|
||||
return new NzBoxGeom(physWorld, primitive.box.lengths, primitive.matrix);
|
||||
return NzBoxGeom::New(physWorld, primitive.box.lengths, primitive.matrix);
|
||||
|
||||
case nzPrimitiveType_Cone:
|
||||
return new NzConeGeom(physWorld, primitive.cone.length, primitive.cone.radius, primitive.matrix);
|
||||
return NzConeGeom::New(physWorld, primitive.cone.length, primitive.cone.radius, primitive.matrix);
|
||||
|
||||
case nzPrimitiveType_Plane:
|
||||
return new NzBoxGeom(physWorld, NzVector3f(primitive.plane.size.x, 0.01f, primitive.plane.size.y), primitive.matrix);
|
||||
return NzBoxGeom::New(physWorld, NzVector3f(primitive.plane.size.x, 0.01f, primitive.plane.size.y), primitive.matrix);
|
||||
///TODO: PlaneGeom?
|
||||
|
||||
case nzPrimitiveType_Sphere:
|
||||
return new NzSphereGeom(physWorld, primitive.sphere.size, primitive.matrix.GetTranslation());
|
||||
return NzSphereGeom::New(physWorld, primitive.sphere.size, primitive.matrix.GetTranslation());
|
||||
}
|
||||
|
||||
NazaraError("Primitive type not handled (0x" + NzString::Number(primitive.type, 16) + ')');
|
||||
|
|
@ -33,17 +33,17 @@ namespace
|
|||
}
|
||||
}
|
||||
|
||||
NzBaseGeom::NzBaseGeom(NzPhysWorld* physWorld) :
|
||||
NzPhysGeom::NzPhysGeom(NzPhysWorld* physWorld) :
|
||||
m_world(physWorld)
|
||||
{
|
||||
}
|
||||
|
||||
NzBaseGeom::~NzBaseGeom()
|
||||
NzPhysGeom::~NzPhysGeom()
|
||||
{
|
||||
NewtonDestroyCollision(m_collision);
|
||||
}
|
||||
|
||||
NzBoxf NzBaseGeom::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;
|
||||
NewtonCollisionCalculateAABB(m_collision, NzMatrix4f::Transform(translation, rotation), min, max);
|
||||
|
|
@ -52,7 +52,7 @@ NzBoxf NzBaseGeom::ComputeAABB(const NzVector3f& translation, const NzQuaternion
|
|||
return NzBoxf(scale*min, scale*max);
|
||||
}
|
||||
|
||||
NzBoxf NzBaseGeom::ComputeAABB(const NzMatrix4f& offsetMatrix) const
|
||||
NzBoxf NzPhysGeom::ComputeAABB(const NzMatrix4f& offsetMatrix) const
|
||||
{
|
||||
NzVector3f min, max;
|
||||
NewtonCollisionCalculateAABB(m_collision, offsetMatrix, min, max);
|
||||
|
|
@ -60,7 +60,7 @@ NzBoxf NzBaseGeom::ComputeAABB(const NzMatrix4f& offsetMatrix) const
|
|||
return NzBoxf(min, max);
|
||||
}
|
||||
|
||||
void NzBaseGeom::ComputeInertialMatrix(NzVector3f* inertia, NzVector3f* center) const
|
||||
void NzPhysGeom::ComputeInertialMatrix(NzVector3f* inertia, NzVector3f* center) const
|
||||
{
|
||||
float inertiaMatrix[3];
|
||||
float origin[3];
|
||||
|
|
@ -74,22 +74,22 @@ void NzBaseGeom::ComputeInertialMatrix(NzVector3f* inertia, NzVector3f* center)
|
|||
center->Set(origin);
|
||||
}
|
||||
|
||||
float NzBaseGeom::ComputeVolume() const
|
||||
float NzPhysGeom::ComputeVolume() const
|
||||
{
|
||||
return NewtonConvexCollisionCalculateVolume(m_collision);
|
||||
}
|
||||
|
||||
NewtonCollision* NzBaseGeom::GetHandle() const
|
||||
NewtonCollision* NzPhysGeom::GetHandle() const
|
||||
{
|
||||
return m_collision;
|
||||
}
|
||||
|
||||
NzPhysWorld* NzBaseGeom::GetWorld() const
|
||||
NzPhysWorld* NzPhysGeom::GetWorld() const
|
||||
{
|
||||
return m_world;
|
||||
}
|
||||
|
||||
NzBaseGeom* NzBaseGeom::Build(NzPhysWorld* physWorld, const NzPrimitiveList& list)
|
||||
NzPhysGeomRef NzPhysGeom::Build(NzPhysWorld* physWorld, const NzPrimitiveList& list)
|
||||
{
|
||||
unsigned int primitiveCount = list.GetSize();
|
||||
|
||||
|
|
@ -103,21 +103,23 @@ NzBaseGeom* NzBaseGeom::Build(NzPhysWorld* physWorld, const NzPrimitiveList& lis
|
|||
|
||||
if (primitiveCount > 1)
|
||||
{
|
||||
std::vector<NzBaseGeom*> geoms(primitiveCount);
|
||||
std::vector<NzPhysGeom*> geoms(primitiveCount);
|
||||
|
||||
for (unsigned int i = 0; i < primitiveCount; ++i)
|
||||
geoms[i] = CreateGeomFromPrimitive(physWorld, list.GetPrimitive(i));
|
||||
|
||||
return new NzCompoundGeom(physWorld, &geoms[0], primitiveCount);
|
||||
return NzCompoundGeom::New(physWorld, &geoms[0], primitiveCount);
|
||||
}
|
||||
else
|
||||
return CreateGeomFromPrimitive(physWorld, list.GetPrimitive(0));
|
||||
}
|
||||
|
||||
NzPhysGeomLibrary::LibraryMap NzPhysGeom::s_library;
|
||||
|
||||
/********************************** BoxGeom **********************************/
|
||||
|
||||
NzBoxGeom::NzBoxGeom(NzPhysWorld* physWorld, const NzVector3f& lengths, const NzMatrix4f& transformMatrix) :
|
||||
NzBaseGeom(physWorld),
|
||||
NzPhysGeom(physWorld),
|
||||
m_lengths(lengths)
|
||||
{
|
||||
m_collision = NewtonCreateBox(physWorld->GetHandle(), lengths.x, lengths.y, lengths.z, 0, transformMatrix);
|
||||
|
|
@ -141,7 +143,7 @@ nzGeomType NzBoxGeom::GetType() const
|
|||
/******************************** CapsuleGeom ********************************/
|
||||
|
||||
NzCapsuleGeom::NzCapsuleGeom(NzPhysWorld* physWorld, float length, float radius, const NzMatrix4f& transformMatrix) :
|
||||
NzBaseGeom(physWorld),
|
||||
NzPhysGeom(physWorld),
|
||||
m_length(length),
|
||||
m_radius(radius)
|
||||
{
|
||||
|
|
@ -170,8 +172,8 @@ nzGeomType NzCapsuleGeom::GetType() const
|
|||
|
||||
/******************************* CompoundGeom ********************************/
|
||||
|
||||
NzCompoundGeom::NzCompoundGeom(NzPhysWorld* physWorld, NzBaseGeom** geoms, unsigned int geomCount) :
|
||||
NzBaseGeom(physWorld)
|
||||
NzCompoundGeom::NzCompoundGeom(NzPhysWorld* physWorld, NzPhysGeom** geoms, unsigned int geomCount) :
|
||||
NzPhysGeom(physWorld)
|
||||
{
|
||||
m_collision = NewtonCreateCompoundCollision(physWorld->GetHandle(), 0);
|
||||
NewtonCompoundCollisionBeginAddRemove(m_collision);
|
||||
|
|
@ -195,7 +197,7 @@ nzGeomType NzCompoundGeom::GetType() const
|
|||
/********************************* ConeGeom **********************************/
|
||||
|
||||
NzConeGeom::NzConeGeom(NzPhysWorld* physWorld, float length, float radius, const NzMatrix4f& transformMatrix) :
|
||||
NzBaseGeom(physWorld),
|
||||
NzPhysGeom(physWorld),
|
||||
m_length(length),
|
||||
m_radius(radius)
|
||||
{
|
||||
|
|
@ -225,7 +227,7 @@ nzGeomType NzConeGeom::GetType() const
|
|||
/****************************** ConvexHullGeom *******************************/
|
||||
|
||||
NzConvexHullGeom::NzConvexHullGeom(NzPhysWorld* physWorld, const void* vertices, unsigned int vertexCount, unsigned int stride, float tolerance, const NzMatrix4f& transformMatrix) :
|
||||
NzBaseGeom(physWorld)
|
||||
NzPhysGeom(physWorld)
|
||||
{
|
||||
m_collision = NewtonCreateConvexHull(physWorld->GetHandle(), vertexCount, reinterpret_cast<const float*>(vertices), stride, tolerance, 0, transformMatrix);
|
||||
}
|
||||
|
|
@ -243,7 +245,7 @@ nzGeomType NzConvexHullGeom::GetType() const
|
|||
/******************************* CylinderGeom ********************************/
|
||||
|
||||
NzCylinderGeom::NzCylinderGeom(NzPhysWorld* physWorld, float length, float radius, const NzMatrix4f& transformMatrix) :
|
||||
NzBaseGeom(physWorld),
|
||||
NzPhysGeom(physWorld),
|
||||
m_length(length),
|
||||
m_radius(radius)
|
||||
{
|
||||
|
|
@ -273,7 +275,7 @@ nzGeomType NzCylinderGeom::GetType() const
|
|||
/********************************* NullGeom **********************************/
|
||||
|
||||
NzNullGeom::NzNullGeom(NzPhysWorld* physWorld) :
|
||||
NzBaseGeom(physWorld)
|
||||
NzPhysGeom(physWorld)
|
||||
{
|
||||
m_collision = NewtonCreateNull(physWorld->GetHandle());
|
||||
}
|
||||
|
|
@ -286,7 +288,7 @@ nzGeomType NzNullGeom::GetType() const
|
|||
/******************************** SphereGeom *********************************/
|
||||
|
||||
NzSphereGeom::NzSphereGeom(NzPhysWorld* physWorld, float radius, const NzMatrix4f& transformMatrix) :
|
||||
NzBaseGeom(physWorld),
|
||||
NzPhysGeom(physWorld),
|
||||
m_radius(radius)
|
||||
{
|
||||
m_collision = NewtonCreateSphere(physWorld->GetHandle(), radius, 0, transformMatrix);
|
||||
|
|
|
|||
|
|
@ -4,55 +4,35 @@
|
|||
|
||||
#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 <algorithm>
|
||||
#include <Nazara/Physics/Debug.hpp>
|
||||
|
||||
NzPhysObject::NzPhysObject(NzPhysWorld* world, const NzMatrix4f& mat) :
|
||||
m_matrix(mat),
|
||||
m_forceAccumulator(NzVector3f::Zero()),
|
||||
m_torqueAccumulator(NzVector3f::Zero()),
|
||||
m_world(world),
|
||||
m_ownsGeom(true),
|
||||
m_gravityFactor(1.f),
|
||||
m_mass(0.f)
|
||||
NzPhysObject(world, NzNullGeom::New(world), mat)
|
||||
{
|
||||
#if NAZARA_PHYSICS_SAFE
|
||||
if (!world)
|
||||
NazaraError("Invalid physics world"); ///TODO: Unexcepted
|
||||
#endif
|
||||
|
||||
m_geom = new NzNullGeom(world);
|
||||
m_body = NewtonCreateDynamicBody(world->GetHandle(), m_geom->GetHandle(), mat);
|
||||
NewtonBodySetUserData(m_body, this);
|
||||
}
|
||||
|
||||
NzPhysObject::NzPhysObject(NzPhysWorld* world, const NzBaseGeom* geom, const NzMatrix4f& mat) :
|
||||
NzPhysObject::NzPhysObject(NzPhysWorld* world, NzPhysGeomRef geom, const NzMatrix4f& mat) :
|
||||
m_matrix(mat),
|
||||
m_forceAccumulator(NzVector3f::Zero()),
|
||||
m_torqueAccumulator(NzVector3f::Zero()),
|
||||
m_geom(geom),
|
||||
m_geom(std::move(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
|
||||
NazaraAssert(m_world, "Invalid world");
|
||||
NazaraAssert(m_geom, "Invalid geometry");
|
||||
|
||||
m_body = NewtonCreateDynamicBody(world->GetHandle(), geom->GetHandle(), mat);
|
||||
m_body = NewtonCreateDynamicBody(world->GetHandle(), m_geom->GetHandle(), mat);
|
||||
NewtonBodySetUserData(m_body, this);
|
||||
}
|
||||
|
||||
NzPhysObject::~NzPhysObject()
|
||||
{
|
||||
NewtonDestroyBody(m_world->GetHandle(), m_body);
|
||||
|
||||
if (m_ownsGeom)
|
||||
delete m_geom;
|
||||
}
|
||||
|
||||
void NzPhysObject::AddForce(const NzVector3f& force, nzCoordSys coordSys)
|
||||
|
|
@ -112,6 +92,14 @@ void NzPhysObject::EnableAutoSleep(bool autoSleep)
|
|||
NewtonBodySetAutoSleep(m_body, autoSleep);
|
||||
}
|
||||
|
||||
NzBoxf NzPhysObject::GetAABB() const
|
||||
{
|
||||
NzVector3f min, max;
|
||||
NewtonBodyGetAABB(m_body, min, max);
|
||||
|
||||
return NzBoxf(min, max);
|
||||
}
|
||||
|
||||
NzVector3f NzPhysObject::GetAngularVelocity() const
|
||||
{
|
||||
NzVector3f angularVelocity;
|
||||
|
|
@ -120,6 +108,11 @@ NzVector3f NzPhysObject::GetAngularVelocity() const
|
|||
return angularVelocity;
|
||||
}
|
||||
|
||||
const NzPhysGeomRef& NzPhysObject::GetGeom() const
|
||||
{
|
||||
return m_geom;
|
||||
}
|
||||
|
||||
float NzPhysObject::GetGravityFactor() const
|
||||
{
|
||||
return m_gravityFactor;
|
||||
|
|
@ -191,6 +184,14 @@ bool NzPhysObject::IsSleeping() const
|
|||
return NewtonBodyGetSleepState(m_body) != 0;
|
||||
}
|
||||
|
||||
void NzPhysObject::SetGeom(NzPhysGeomRef geom)
|
||||
{
|
||||
if (geom)
|
||||
m_geom = geom;
|
||||
else
|
||||
m_geom = NzNullGeom::New(m_world);
|
||||
}
|
||||
|
||||
void NzPhysObject::SetGravityFactor(float gravityFactor)
|
||||
{
|
||||
m_gravityFactor = gravityFactor;
|
||||
|
|
@ -240,6 +241,7 @@ void NzPhysObject::SetRotation(const NzQuaternionf& rotation)
|
|||
void NzPhysObject::UpdateBody()
|
||||
{
|
||||
NewtonBodySetMatrix(m_body, m_matrix);
|
||||
|
||||
/*for (std::set<PhysObjectListener*>::iterator it = m_listeners.begin(); it != m_listeners.end(); ++it)
|
||||
(*it)->PhysObjectOnUpdate(this);*/
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue