Upgrade Physics2D and Physics3D
This commit is contained in:
@@ -3,6 +3,8 @@
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/Physics2D/Collider2D.hpp>
|
||||
#include <Nazara/Core/CallOnExit.hpp>
|
||||
#include <Nazara/Core/StackArray.hpp>
|
||||
#include <Nazara/Math/Quaternion.hpp>
|
||||
#include <Nazara/Physics2D/RigidBody2D.hpp>
|
||||
#include <Nazara/Physics2D/PhysWorld2D.hpp>
|
||||
@@ -13,6 +15,16 @@
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
namespace
|
||||
{
|
||||
constexpr cpSpaceDebugColor white = { 1.f, 1.f, 1.f, 1.f };
|
||||
|
||||
Vector2f FromChipmunk(const cpVect& v)
|
||||
{
|
||||
return Vector2f(float(v.x), float(v.y));
|
||||
}
|
||||
}
|
||||
|
||||
Collider2D::~Collider2D() = default;
|
||||
|
||||
void Collider2D::ForEachPolygon(const std::function<void(const Vector2f* vertices, std::size_t vertexCount)>& callback) const
|
||||
@@ -20,65 +32,108 @@ namespace Nz
|
||||
// Currently, the only way to get only the polygons of a shape is to create a temporary cpSpace containing only this shape
|
||||
// A better way to do this would be to reimplement this function in every subclass type in the very same way chipmunk does
|
||||
|
||||
PhysWorld2D physWorld;
|
||||
RigidBody2D rigidBody(&physWorld, 0.f);
|
||||
cpSpace* space = cpSpaceNew();
|
||||
if (!space)
|
||||
throw std::runtime_error("failed to create chipmunk space");
|
||||
|
||||
std::vector<cpShape*> shapeVector;
|
||||
rigidBody.SetGeom(const_cast<Collider2D*>(this), false, false); //< Won't be used for writing, but still ugly
|
||||
CallOnExit spaceRRID([&] { cpSpaceFree(space); });
|
||||
|
||||
PhysWorld2D::DebugDrawOptions drawCallbacks;
|
||||
drawCallbacks.circleCallback = [&](const Vector2f& origin, const RadianAnglef& /*rotation*/, float radius, Nz::Color /*outlineColor*/, Nz::Color /*fillColor*/, void* /*userData*/)
|
||||
cpBody* body = cpSpaceGetStaticBody(space);
|
||||
|
||||
std::vector<cpShape*> shapes;
|
||||
CreateShapes(body, &shapes);
|
||||
CallOnExit shapeRRID([&]
|
||||
{
|
||||
for (cpShape* shape : shapes)
|
||||
cpShapeDestroy(shape);
|
||||
});
|
||||
|
||||
for (cpShape* shape : shapes)
|
||||
cpSpaceAddShape(space, shape);
|
||||
|
||||
using CallbackType = std::decay_t<decltype(callback)>;
|
||||
|
||||
cpSpaceDebugDrawOptions drawOptions;
|
||||
drawOptions.collisionPointColor = white;
|
||||
drawOptions.constraintColor = white;
|
||||
drawOptions.shapeOutlineColor = white;
|
||||
drawOptions.data = const_cast<void*>(static_cast<const void*>(&callback));
|
||||
drawOptions.flags = CP_SPACE_DEBUG_DRAW_SHAPES;
|
||||
|
||||
// Callback trampoline
|
||||
drawOptions.colorForShape = [](cpShape* /*shape*/, cpDataPointer /*userdata*/) { return white; };
|
||||
drawOptions.drawCircle = [](cpVect pos, cpFloat /*angle*/, cpFloat radius, cpSpaceDebugColor /*outlineColor*/, cpSpaceDebugColor /*fillColor*/, cpDataPointer userdata)
|
||||
{
|
||||
const auto& callback = *static_cast<const CallbackType*>(userdata);
|
||||
|
||||
constexpr std::size_t circleVerticesCount = 20;
|
||||
|
||||
std::array<Vector2f, circleVerticesCount> vertices;
|
||||
|
||||
Vector2f origin = FromChipmunk(pos);
|
||||
float r = static_cast<float>(radius);
|
||||
|
||||
RadianAnglef angleBetweenVertices = 2.f * float(M_PI) / vertices.size();
|
||||
for (std::size_t i = 0; i < vertices.size(); ++i)
|
||||
{
|
||||
RadianAnglef angle = float(i) * angleBetweenVertices;
|
||||
std::pair<float, float> sincos = angle.GetSinCos();
|
||||
|
||||
vertices[i] = origin + Vector2f(radius * sincos.first, radius * sincos.second);
|
||||
vertices[i] = origin + Vector2f(r * sincos.first, r * sincos.second);
|
||||
}
|
||||
|
||||
callback(vertices.data(), vertices.size());
|
||||
};
|
||||
|
||||
drawCallbacks.polygonCallback = [&](const Vector2f* vertices, std::size_t vertexCount, float radius, Nz::Color /*outlineColor*/, Nz::Color /*fillColor*/, void* /*userData*/)
|
||||
{
|
||||
//TODO: Handle radius
|
||||
callback(vertices, vertexCount);
|
||||
};
|
||||
drawOptions.drawDot = [](cpFloat /*size*/, cpVect /*pos*/, cpSpaceDebugColor /*color*/, cpDataPointer /*userdata*/) {}; //< Dummy
|
||||
|
||||
drawCallbacks.segmentCallback = [&](const Vector2f& first, const Vector2f& second, Nz::Color /*color*/, void* /*userData*/)
|
||||
drawOptions.drawFatSegment = [](cpVect a, cpVect b, cpFloat radius, cpSpaceDebugColor /*outlineColor*/, cpSpaceDebugColor /*fillColor*/, cpDataPointer userdata)
|
||||
{
|
||||
std::array<Vector2f, 2> vertices = { first, second };
|
||||
const auto& callback = *static_cast<const CallbackType*>(userdata);
|
||||
|
||||
callback(vertices.data(), vertices.size());
|
||||
};
|
||||
|
||||
drawCallbacks.thickSegmentCallback = [&](const Vector2f& first, const Vector2f& second, float thickness, Nz::Color /*outlineColor*/, Nz::Color /*fillColor*/, void* /*userData*/)
|
||||
{
|
||||
static std::pair<float, float> sincos = Nz::DegreeAnglef(90.f).GetSinCos();
|
||||
|
||||
Vector2f normal = Vector2f::Normalize(second - first);
|
||||
Vector2f from = FromChipmunk(a);
|
||||
Vector2f to = FromChipmunk(b);
|
||||
|
||||
Vector2f normal = Vector2f::Normalize(to - from);
|
||||
Vector2f thicknessNormal(sincos.second * normal.x - sincos.first * normal.y,
|
||||
sincos.first * normal.x + sincos.second * normal.y);
|
||||
|
||||
float thickness = static_cast<float>(radius);
|
||||
|
||||
std::array<Vector2f, 4> vertices;
|
||||
vertices[0] = first + thickness * thicknessNormal;
|
||||
vertices[1] = first - thickness * thicknessNormal;
|
||||
vertices[2] = second - thickness * thicknessNormal;
|
||||
vertices[3] = second + thickness * thicknessNormal;
|
||||
vertices[0] = from + thickness * thicknessNormal;
|
||||
vertices[1] = from - thickness * thicknessNormal;
|
||||
vertices[2] = to - thickness * thicknessNormal;
|
||||
vertices[3] = to + thickness * thicknessNormal;
|
||||
|
||||
callback(vertices.data(), vertices.size());
|
||||
};
|
||||
|
||||
physWorld.DebugDraw(drawCallbacks, true, false, false);
|
||||
drawOptions.drawPolygon = [](int vertexCount, const cpVect* vertices, cpFloat /*radius*/, cpSpaceDebugColor /*outlineColor*/, cpSpaceDebugColor /*fillColor*/, cpDataPointer userdata)
|
||||
{
|
||||
const auto& callback = *static_cast<const CallbackType*>(userdata);
|
||||
|
||||
StackArray<Vector2f> nVertices = NazaraStackArray(Vector2f, vertexCount);
|
||||
for (int i = 0; i < vertexCount; ++i)
|
||||
nVertices[i].Set(float(vertices[i].x), float(vertices[i].y));
|
||||
|
||||
callback(nVertices.data(), nVertices.size());
|
||||
};
|
||||
|
||||
drawOptions.drawSegment = [](cpVect a, cpVect b, cpSpaceDebugColor /*fillColor*/, cpDataPointer userdata)
|
||||
{
|
||||
const auto& callback = *static_cast<const CallbackType*>(userdata);
|
||||
|
||||
std::array<Vector2f, 2> vertices = { FromChipmunk(a), FromChipmunk(b) };
|
||||
callback(vertices.data(), vertices.size());
|
||||
};
|
||||
|
||||
cpSpaceDebugDraw(space, &drawOptions);
|
||||
}
|
||||
|
||||
std::size_t Collider2D::GenerateShapes(RigidBody2D* body, std::vector<cpShape*>* shapes) const
|
||||
std::size_t Collider2D::GenerateShapes(cpBody* body, std::vector<cpShape*>* shapes) const
|
||||
{
|
||||
std::size_t shapeCount = CreateShapes(body, shapes);
|
||||
|
||||
@@ -123,12 +178,12 @@ namespace Nz
|
||||
|
||||
ColliderType2D BoxCollider2D::GetType() const
|
||||
{
|
||||
return ColliderType2D_Box;
|
||||
return ColliderType2D::Box;
|
||||
}
|
||||
|
||||
std::size_t BoxCollider2D::CreateShapes(RigidBody2D* body, std::vector<cpShape*>* shapes) const
|
||||
std::size_t BoxCollider2D::CreateShapes(cpBody* body, std::vector<cpShape*>* shapes) const
|
||||
{
|
||||
shapes->push_back(cpBoxShapeNew2(body->GetHandle(), cpBBNew(m_rect.x, m_rect.y, m_rect.x + m_rect.width, m_rect.y + m_rect.height), m_radius));
|
||||
shapes->push_back(cpBoxShapeNew2(body, cpBBNew(m_rect.x, m_rect.y, m_rect.x + m_rect.width, m_rect.y + m_rect.height), m_radius));
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -152,18 +207,18 @@ namespace Nz
|
||||
|
||||
ColliderType2D CircleCollider2D::GetType() const
|
||||
{
|
||||
return ColliderType2D_Circle;
|
||||
return ColliderType2D::Circle;
|
||||
}
|
||||
|
||||
std::size_t CircleCollider2D::CreateShapes(RigidBody2D* body, std::vector<cpShape*>* shapes) const
|
||||
std::size_t CircleCollider2D::CreateShapes(cpBody* body, std::vector<cpShape*>* shapes) const
|
||||
{
|
||||
shapes->push_back(cpCircleShapeNew(body->GetHandle(), m_radius, cpv(m_offset.x, m_offset.y)));
|
||||
shapes->push_back(cpCircleShapeNew(body, m_radius, cpv(m_offset.x, m_offset.y)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
/******************************** CompoundCollider2D *********************************/
|
||||
|
||||
CompoundCollider2D::CompoundCollider2D(std::vector<Collider2DRef> geoms) :
|
||||
CompoundCollider2D::CompoundCollider2D(std::vector<std::shared_ptr<Collider2D>> geoms) :
|
||||
m_geoms(std::move(geoms)),
|
||||
m_doesOverrideCollisionProperties(true)
|
||||
{
|
||||
@@ -191,10 +246,10 @@ namespace Nz
|
||||
|
||||
ColliderType2D CompoundCollider2D::GetType() const
|
||||
{
|
||||
return ColliderType2D_Compound;
|
||||
return ColliderType2D::Compound;
|
||||
}
|
||||
|
||||
std::size_t CompoundCollider2D::CreateShapes(RigidBody2D* body, std::vector<cpShape*>* shapes) const
|
||||
std::size_t CompoundCollider2D::CreateShapes(cpBody* body, std::vector<cpShape*>* shapes) const
|
||||
{
|
||||
// Since C++ does not allow protected call from other objects, we have to be a friend of Collider2D, yay
|
||||
|
||||
@@ -205,7 +260,7 @@ namespace Nz
|
||||
return shapeCount;
|
||||
}
|
||||
|
||||
std::size_t CompoundCollider2D::GenerateShapes(RigidBody2D* body, std::vector<cpShape*>* shapes) const
|
||||
std::size_t CompoundCollider2D::GenerateShapes(cpBody* body, std::vector<cpShape*>* shapes) const
|
||||
{
|
||||
// This is our parent's default behavior
|
||||
if (m_doesOverrideCollisionProperties)
|
||||
@@ -248,12 +303,12 @@ namespace Nz
|
||||
|
||||
ColliderType2D ConvexCollider2D::GetType() const
|
||||
{
|
||||
return ColliderType2D_Convex;
|
||||
return ColliderType2D::Convex;
|
||||
}
|
||||
|
||||
std::size_t ConvexCollider2D::CreateShapes(RigidBody2D* body, std::vector<cpShape*>* shapes) const
|
||||
std::size_t ConvexCollider2D::CreateShapes(cpBody* body, std::vector<cpShape*>* shapes) const
|
||||
{
|
||||
shapes->push_back(cpPolyShapeNew(body->GetHandle(), int(m_vertices.size()), reinterpret_cast<const cpVect*>(m_vertices.data()), cpTransformIdentity, m_radius));
|
||||
shapes->push_back(cpPolyShapeNew(body, int(m_vertices.size()), reinterpret_cast<const cpVect*>(m_vertices.data()), cpTransformIdentity, m_radius));
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -261,7 +316,7 @@ namespace Nz
|
||||
|
||||
ColliderType2D NullCollider2D::GetType() const
|
||||
{
|
||||
return ColliderType2D_Null;
|
||||
return ColliderType2D::Null;
|
||||
}
|
||||
|
||||
Nz::Vector2f NullCollider2D::ComputeCenterOfMass() const
|
||||
@@ -274,7 +329,7 @@ namespace Nz
|
||||
return (mass > 0.f) ? 1.f : 0.f; //< Null inertia is only possible for static/kinematic objects
|
||||
}
|
||||
|
||||
std::size_t NullCollider2D::CreateShapes(RigidBody2D* /*body*/, std::vector<cpShape*>* /*shapes*/) const
|
||||
std::size_t NullCollider2D::CreateShapes(cpBody* /*body*/, std::vector<cpShape*>* /*shapes*/) const
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
@@ -293,12 +348,12 @@ namespace Nz
|
||||
|
||||
ColliderType2D SegmentCollider2D::GetType() const
|
||||
{
|
||||
return ColliderType2D_Segment;
|
||||
return ColliderType2D::Segment;
|
||||
}
|
||||
|
||||
std::size_t SegmentCollider2D::CreateShapes(RigidBody2D* body, std::vector<cpShape*>* shapes) const
|
||||
std::size_t SegmentCollider2D::CreateShapes(cpBody* body, std::vector<cpShape*>* shapes) const
|
||||
{
|
||||
cpShape* segment = cpSegmentShapeNew(body->GetHandle(), cpv(m_first.x, m_first.y), cpv(m_second.x, m_second.y), m_thickness);
|
||||
cpShape* segment = cpSegmentShapeNew(body, cpv(m_first.x, m_first.y), cpv(m_second.x, m_second.y), m_thickness);
|
||||
cpSegmentShapeSetNeighbors(segment, cpv(m_firstNeighbor.x, m_firstNeighbor.y), cpv(m_secondNeighbor.x, m_secondNeighbor.y));
|
||||
|
||||
shapes->push_back(segment);
|
||||
|
||||
@@ -18,7 +18,7 @@ namespace Nz
|
||||
{
|
||||
}
|
||||
|
||||
RigidBody2D::RigidBody2D(PhysWorld2D* world, float mass, Collider2DRef geom) :
|
||||
RigidBody2D::RigidBody2D(PhysWorld2D* world, float mass, std::shared_ptr<Collider2D> geom) :
|
||||
m_positionOffset(Vector2f::Zero()),
|
||||
m_geom(),
|
||||
m_userData(nullptr),
|
||||
@@ -233,7 +233,7 @@ namespace Nz
|
||||
return float(cpShapeGetFriction(m_shapes[shapeIndex]));
|
||||
}
|
||||
|
||||
const Collider2DRef& RigidBody2D::GetGeom() const
|
||||
const std::shared_ptr<Collider2D>& RigidBody2D::GetGeom() const
|
||||
{
|
||||
return m_geom;
|
||||
}
|
||||
@@ -374,7 +374,7 @@ namespace Nz
|
||||
cpShapeSetFriction(m_shapes[shapeIndex], cpFloat(friction));
|
||||
}
|
||||
|
||||
void RigidBody2D::SetGeom(Collider2DRef geom, bool recomputeMoment, bool recomputeMassCenter)
|
||||
void RigidBody2D::SetGeom(std::shared_ptr<Collider2D> geom, bool recomputeMoment, bool recomputeMassCenter)
|
||||
{
|
||||
// We have no public way of getting rid of an existing geom without removing the whole body
|
||||
// So let's save some attributes of the body, destroy it and rebuild it
|
||||
@@ -393,11 +393,11 @@ namespace Nz
|
||||
}
|
||||
|
||||
if (geom)
|
||||
m_geom = geom;
|
||||
m_geom = std::move(geom);
|
||||
else
|
||||
m_geom = NullCollider2D::New();
|
||||
m_geom = std::make_shared<NullCollider2D>();
|
||||
|
||||
m_geom->GenerateShapes(this, &m_shapes);
|
||||
m_geom->GenerateShapes(m_handle, &m_shapes);
|
||||
|
||||
for (cpShape* shape : m_shapes)
|
||||
cpShapeSetUserData(shape, this);
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user