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
|
||||
{
|
||||
friend class Physics3DSystem;
|
||||
|
||||
public:
|
||||
using RigidBody3D::RigidBody3D;
|
||||
RigidBody3DComponent(const RigidBody3DComponent&) = default;
|
||||
|
|
|
|||
|
|
@ -77,6 +77,9 @@ namespace Nz
|
|||
RigidBody3D& operator=(const RigidBody3D& object);
|
||||
RigidBody3D& operator=(RigidBody3D&& object) noexcept;
|
||||
|
||||
protected:
|
||||
void Destroy();
|
||||
|
||||
private:
|
||||
void UpdateBody(const Matrix4f& transformMatrix);
|
||||
static void ForceAndTorqueCallback(const NewtonBody* body, float timeStep, int threadIndex);
|
||||
|
|
|
|||
|
|
@ -35,6 +35,7 @@ namespace Nz
|
|||
private:
|
||||
static void OnConstruct(entt::registry& registry, entt::entity entity);
|
||||
|
||||
entt::registry& m_registry;
|
||||
entt::connection m_constructConnection;
|
||||
PhysWorld3D m_physWorld;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue