Physics3D: Ensure RigidBody are destroyed on world destruction
This commit is contained in:
parent
2d236afe0f
commit
f289d13101
|
|
@ -14,6 +14,8 @@ namespace Nz
|
||||||
{
|
{
|
||||||
class NAZARA_PHYSICS3D_API RigidBody3DComponent : public RigidBody3D
|
class NAZARA_PHYSICS3D_API RigidBody3DComponent : public RigidBody3D
|
||||||
{
|
{
|
||||||
|
friend class Physics3DSystem;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
using RigidBody3D::RigidBody3D;
|
using RigidBody3D::RigidBody3D;
|
||||||
RigidBody3DComponent(const RigidBody3DComponent&) = default;
|
RigidBody3DComponent(const RigidBody3DComponent&) = default;
|
||||||
|
|
|
||||||
|
|
@ -77,6 +77,9 @@ namespace Nz
|
||||||
RigidBody3D& operator=(const RigidBody3D& object);
|
RigidBody3D& operator=(const RigidBody3D& object);
|
||||||
RigidBody3D& operator=(RigidBody3D&& object) noexcept;
|
RigidBody3D& operator=(RigidBody3D&& object) noexcept;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
void Destroy();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void UpdateBody(const Matrix4f& transformMatrix);
|
void UpdateBody(const Matrix4f& transformMatrix);
|
||||||
static void ForceAndTorqueCallback(const NewtonBody* body, float timeStep, int threadIndex);
|
static void ForceAndTorqueCallback(const NewtonBody* body, float timeStep, int threadIndex);
|
||||||
|
|
|
||||||
|
|
@ -35,6 +35,7 @@ namespace Nz
|
||||||
private:
|
private:
|
||||||
static void OnConstruct(entt::registry& registry, entt::entity entity);
|
static void OnConstruct(entt::registry& registry, entt::entity entity);
|
||||||
|
|
||||||
|
entt::registry& m_registry;
|
||||||
entt::connection m_constructConnection;
|
entt::connection m_constructConnection;
|
||||||
PhysWorld3D m_physWorld;
|
PhysWorld3D m_physWorld;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -418,6 +418,17 @@ namespace Nz
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void RigidBody3D::Destroy()
|
||||||
|
{
|
||||||
|
if (m_body)
|
||||||
|
{
|
||||||
|
NewtonDestroyBody(m_body);
|
||||||
|
m_body = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
m_geom.reset();
|
||||||
|
}
|
||||||
|
|
||||||
void RigidBody3D::UpdateBody(const Matrix4f& transformMatrix)
|
void RigidBody3D::UpdateBody(const Matrix4f& transformMatrix)
|
||||||
{
|
{
|
||||||
NewtonBodySetMatrix(m_body, &transformMatrix.m11);
|
NewtonBodySetMatrix(m_body, &transformMatrix.m11);
|
||||||
|
|
|
||||||
|
|
@ -8,13 +8,19 @@
|
||||||
|
|
||||||
namespace Nz
|
namespace Nz
|
||||||
{
|
{
|
||||||
Physics3DSystem::Physics3DSystem(entt::registry& registry)
|
Physics3DSystem::Physics3DSystem(entt::registry& registry) :
|
||||||
|
m_registry(registry)
|
||||||
{
|
{
|
||||||
m_constructConnection = registry.on_construct<RigidBody3DComponent>().connect<OnConstruct>();
|
m_constructConnection = registry.on_construct<RigidBody3DComponent>().connect<OnConstruct>();
|
||||||
}
|
}
|
||||||
|
|
||||||
Physics3DSystem::~Physics3DSystem()
|
Physics3DSystem::~Physics3DSystem()
|
||||||
{
|
{
|
||||||
|
// Ensure every NewtonBody is destroyed before world is
|
||||||
|
auto rigidBodyView = m_registry.view<RigidBody3DComponent>();
|
||||||
|
for (auto [entity, rigidBodyComponent] : rigidBodyView.each())
|
||||||
|
rigidBodyComponent.Destroy();
|
||||||
|
|
||||||
m_constructConnection.release();
|
m_constructConnection.release();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue