Physics3D: Ensure RigidBody are destroyed on world destruction

This commit is contained in:
Jérôme Leclercq
2021-11-30 18:17:53 +01:00
parent 2d236afe0f
commit f289d13101
5 changed files with 24 additions and 1 deletions

View File

@@ -418,6 +418,17 @@ namespace Nz
return *this;
}
void RigidBody3D::Destroy()
{
if (m_body)
{
NewtonDestroyBody(m_body);
m_body = nullptr;
}
m_geom.reset();
}
void RigidBody3D::UpdateBody(const Matrix4f& transformMatrix)
{
NewtonBodySetMatrix(m_body, &transformMatrix.m11);

View File

@@ -8,13 +8,19 @@
namespace Nz
{
Physics3DSystem::Physics3DSystem(entt::registry& registry)
Physics3DSystem::Physics3DSystem(entt::registry& registry) :
m_registry(registry)
{
m_constructConnection = registry.on_construct<RigidBody3DComponent>().connect<OnConstruct>();
}
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();
}