From f316b26a835306a4d2a50897d36f00e9f31464e5 Mon Sep 17 00:00:00 2001 From: maelstrom Date: Thu, 23 Jan 2025 22:58:01 +0100 Subject: [PATCH] fix: rigidbodies getting destroyed before parts can even get to them --- src/objects/part.cpp | 1 + src/physics/simulation.cpp | 8 +++++--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/objects/part.cpp b/src/objects/part.cpp index 92af98a..b365f42 100644 --- a/src/objects/part.cpp +++ b/src/objects/part.cpp @@ -24,6 +24,7 @@ Part::Part(PartConstructParams params): Instance(&TYPE_), position(params.positi // This feels wrong. Get access to PhysicsWorld somehow else? Part will need access to this often though, most likely... extern rp::PhysicsWorld* world; Part::~Part() { + // This relies on physicsCommon still existing. Be very careful. if (this->rigidBody) world->destroyRigidBody(rigidBody); } diff --git a/src/physics/simulation.cpp b/src/physics/simulation.cpp index 7cb3dd4..fbbcdd7 100644 --- a/src/physics/simulation.cpp +++ b/src/physics/simulation.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -26,12 +27,13 @@ class PhysicsListener : public rp::EventListener { } }; -rp::PhysicsCommon physicsCommon; +rp::PhysicsCommon* physicsCommon; rp::PhysicsWorld* world; PhysicsListener eventListener; void simulationInit() { - world = physicsCommon.createPhysicsWorld(); + physicsCommon = new rp::PhysicsCommon; // I allocate this on the heap to ensure it exists while Parts are getting destructed. This is probably not great + world = physicsCommon->createPhysicsWorld(); world->setGravity(rp::Vector3(0, -196.2, 0)); } @@ -46,7 +48,7 @@ void syncPartPhysics(std::shared_ptr part) { part->rigidBody->setTransform(transform); } - rp::BoxShape* shape = physicsCommon.createBoxShape(glmToRp(part->scale * glm::vec3(0.5f))); + rp::BoxShape* shape = physicsCommon->createBoxShape(glmToRp(part->scale * glm::vec3(0.5f))); if (part->rigidBody->getNbColliders() > 0) { part->rigidBody->removeCollider(part->rigidBody->getCollider(0));