Upgrade Physics2D and Physics3D

This commit is contained in:
Jérôme Leclercq
2021-05-24 19:12:21 +02:00
parent 4bcb63d776
commit 8b0b5295f7
25 changed files with 239 additions and 448 deletions

View File

@@ -12,26 +12,26 @@ namespace Nz
{
namespace
{
Collider3DRef CreateGeomFromPrimitive(const Primitive& primitive)
std::shared_ptr<Collider3D> CreateGeomFromPrimitive(const Primitive& primitive)
{
switch (primitive.type)
{
case PrimitiveType_Box:
return BoxCollider3D::New(primitive.box.lengths, primitive.matrix);
return std::make_shared<BoxCollider3D>(primitive.box.lengths, primitive.matrix);
case PrimitiveType_Cone:
return ConeCollider3D::New(primitive.cone.length, primitive.cone.radius, primitive.matrix);
return std::make_shared<ConeCollider3D>(primitive.cone.length, primitive.cone.radius, primitive.matrix);
case PrimitiveType_Plane:
return BoxCollider3D::New(Vector3f(primitive.plane.size.x, 0.01f, primitive.plane.size.y), primitive.matrix);
return std::make_shared<BoxCollider3D>(Vector3f(primitive.plane.size.x, 0.01f, primitive.plane.size.y), primitive.matrix);
///TODO: PlaneGeom?
case PrimitiveType_Sphere:
return SphereCollider3D::New(primitive.sphere.size, primitive.matrix.GetTranslation());
return std::make_shared<SphereCollider3D>(primitive.sphere.size, primitive.matrix.GetTranslation());
}
NazaraError("Primitive type not handled (0x" + NumberToString(primitive.type, 16) + ')');
return Collider3DRef();
return std::shared_ptr<Collider3D>();
}
}
@@ -148,42 +148,24 @@ namespace Nz
return it->second;
}
Collider3DRef Collider3D::Build(const PrimitiveList& list)
std::shared_ptr<Collider3D> Collider3D::Build(const PrimitiveList& list)
{
std::size_t primitiveCount = list.GetSize();
if (primitiveCount > 1)
{
std::vector<Collider3DRef> geoms(primitiveCount);
std::vector<std::shared_ptr<Collider3D>> geoms(primitiveCount);
for (unsigned int i = 0; i < primitiveCount; ++i)
geoms[i] = CreateGeomFromPrimitive(list.GetPrimitive(i));
return CompoundCollider3D::New(std::move(geoms));
return std::make_shared<CompoundCollider3D>(std::move(geoms));
}
else if (primitiveCount > 0)
return CreateGeomFromPrimitive(list.GetPrimitive(0));
else
return NullCollider3D::New();
return std::make_shared<NullCollider3D>();
}
bool Collider3D::Initialize()
{
if (!Collider3DLibrary::Initialize())
{
NazaraError("Failed to initialise library");
return false;
}
return true;
}
void Collider3D::Uninitialize()
{
Collider3DLibrary::Uninitialize();
}
Collider3DLibrary::LibraryMap Collider3D::s_library;
/********************************** BoxCollider3D **********************************/
BoxCollider3D::BoxCollider3D(const Vector3f& lengths, const Matrix4f& transformMatrix) :
@@ -220,7 +202,7 @@ namespace Nz
ColliderType3D BoxCollider3D::GetType() const
{
return ColliderType3D_Box;
return ColliderType3D::Box;
}
NewtonCollision* BoxCollider3D::CreateHandle(PhysWorld3D* world) const
@@ -254,7 +236,7 @@ namespace Nz
ColliderType3D CapsuleCollider3D::GetType() const
{
return ColliderType3D_Capsule;
return ColliderType3D::Capsule;
}
NewtonCollision* CapsuleCollider3D::CreateHandle(PhysWorld3D* world) const
@@ -264,19 +246,19 @@ namespace Nz
/******************************* CompoundCollider3D ********************************/
CompoundCollider3D::CompoundCollider3D(std::vector<Collider3DRef> geoms) :
CompoundCollider3D::CompoundCollider3D(std::vector<std::shared_ptr<Collider3D>> geoms) :
m_geoms(std::move(geoms))
{
}
const std::vector<Collider3DRef>& CompoundCollider3D::GetGeoms() const
const std::vector<std::shared_ptr<Collider3D>>& CompoundCollider3D::GetGeoms() const
{
return m_geoms;
}
ColliderType3D CompoundCollider3D::GetType() const
{
return ColliderType3D_Compound;
return ColliderType3D::Compound;
}
NewtonCollision* CompoundCollider3D::CreateHandle(PhysWorld3D* world) const
@@ -284,12 +266,12 @@ namespace Nz
NewtonCollision* compoundCollision = NewtonCreateCompoundCollision(world->GetHandle(), 0);
NewtonCompoundCollisionBeginAddRemove(compoundCollision);
for (const Collider3DRef& geom : m_geoms)
for (const std::shared_ptr<Collider3D>& geom : m_geoms)
{
if (geom->GetType() == ColliderType3D_Compound)
if (geom->GetType() == ColliderType3D::Compound)
{
CompoundCollider3D* compoundGeom = static_cast<CompoundCollider3D*>(geom.Get());
for (const Collider3DRef& piece : compoundGeom->GetGeoms())
CompoundCollider3D& compoundGeom = static_cast<CompoundCollider3D&>(*geom);
for (const std::shared_ptr<Collider3D>& piece : compoundGeom.GetGeoms())
NewtonCompoundCollisionAddSubCollision(compoundCollision, piece->GetHandle(world));
}
else
@@ -326,7 +308,7 @@ namespace Nz
ColliderType3D ConeCollider3D::GetType() const
{
return ColliderType3D_Cone;
return ColliderType3D::Cone;
}
NewtonCollision* ConeCollider3D::CreateHandle(PhysWorld3D* world) const
@@ -357,7 +339,7 @@ namespace Nz
ColliderType3D ConvexCollider3D::GetType() const
{
return ColliderType3D_ConvexHull;
return ColliderType3D::ConvexHull;
}
NewtonCollision* ConvexCollider3D::CreateHandle(PhysWorld3D* world) const
@@ -391,7 +373,7 @@ namespace Nz
ColliderType3D CylinderCollider3D::GetType() const
{
return ColliderType3D_Cylinder;
return ColliderType3D::Cylinder;
}
NewtonCollision* CylinderCollider3D::CreateHandle(PhysWorld3D* world) const
@@ -407,7 +389,7 @@ namespace Nz
ColliderType3D NullCollider3D::GetType() const
{
return ColliderType3D_Null;
return ColliderType3D::Null;
}
void NullCollider3D::ComputeInertialMatrix(Vector3f* inertia, Vector3f* center) const
@@ -458,7 +440,7 @@ namespace Nz
ColliderType3D SphereCollider3D::GetType() const
{
return ColliderType3D_Sphere;
return ColliderType3D::Sphere;
}
NewtonCollision* SphereCollider3D::CreateHandle(PhysWorld3D* world) const

View File

@@ -16,13 +16,6 @@ namespace Nz
Physics3D::Physics3D(Config /*config*/) :
ModuleBase("Physics3D", this)
{
if (!Collider3D::Initialize())
throw std::runtime_error("failed to initialize colliders");
}
Physics3D::~Physics3D()
{
Collider3D::Uninitialize();
}
unsigned int Physics3D::GetMemoryUsed()

View File

@@ -12,11 +12,11 @@
namespace Nz
{
RigidBody3D::RigidBody3D(PhysWorld3D* world, const Matrix4f& mat) :
RigidBody3D(world, NullCollider3D::New(), mat)
RigidBody3D(world, std::make_shared<NullCollider3D>(), mat)
{
}
RigidBody3D::RigidBody3D(PhysWorld3D* world, Collider3DRef geom, const Matrix4f& mat) :
RigidBody3D::RigidBody3D(PhysWorld3D* world, std::shared_ptr<Collider3D> geom, const Matrix4f& mat) :
m_geom(std::move(geom)),
m_matrix(mat),
m_forceAccumulator(Vector3f::Zero()),
@@ -28,7 +28,7 @@ namespace Nz
NazaraAssert(m_world, "Invalid world");
if (!m_geom)
m_geom = NullCollider3D::New();
m_geom = std::make_shared<NullCollider3D>();
m_body = NewtonCreateDynamicBody(m_world->GetHandle(), m_geom->GetHandle(m_world), m_matrix);
NewtonBodySetUserData(m_body, this);
@@ -165,7 +165,7 @@ namespace Nz
return angularVelocity;
}
const Collider3DRef& RigidBody3D::GetGeom() const
const std::shared_ptr<Collider3D>& RigidBody3D::GetGeom() const
{
return m_geom;
}
@@ -276,14 +276,14 @@ namespace Nz
NewtonBodySetOmega(m_body, &angularVelocity.x);
}
void RigidBody3D::SetGeom(Collider3DRef geom)
void RigidBody3D::SetGeom(std::shared_ptr<Collider3D> geom)
{
if (m_geom.Get() != geom)
if (m_geom != geom)
{
if (geom)
m_geom = geom;
m_geom = std::move(geom);
else
m_geom = NullCollider3D::New();
m_geom = std::make_shared<NullCollider3D>();
NewtonBodySetCollision(m_body, m_geom->GetHandle(m_world));
}