diff --git a/core/src/objects/part/basepart.cpp b/core/src/objects/part/basepart.cpp index ff2e426..84a3bb7 100644 --- a/core/src/objects/part/basepart.cpp +++ b/core/src/objects/part/basepart.cpp @@ -26,7 +26,6 @@ BasePart::BasePart(const InstanceType* type, PartConstructParams params): PVInst } BasePart::~BasePart() { - // This relies on physicsCommon still existing. Be very careful. if (workspace() != nullptr) { workspace()->RemoveBody(shared()); } diff --git a/core/src/physics/world.cpp b/core/src/physics/world.cpp index 494e8ef..a657820 100644 --- a/core/src/physics/world.cpp +++ b/core/src/physics/world.cpp @@ -1,4 +1,5 @@ #include "world.h" +#include "Jolt/Geometry/AABox.h" #include "datatypes/vector.h" #include "enum/part.h" #include "logger.h" @@ -104,7 +105,16 @@ void PhysWorld::addBody(std::shared_ptr part) { } void PhysWorld::removeBody(std::shared_ptr part) { - // TODO: + JPH::BodyInterface& interface = worldImpl.GetBodyInterface(); + + // https://jrouwe.github.io/JoltPhysics/index.html#sleeping-bodies + // Wake sleeping bodies in its area before removing it + Vector3 aabbSize = part->GetAABB(); + interface.ActivateBodiesInAABox(JPH::AABox(convert(part->position() - aabbSize), convert(part->position() + aabbSize)), {}, {}); + + interface.RemoveBody(part->rigidBody.bodyImpl->GetID()); + interface.DestroyBody(part->rigidBody.bodyImpl->GetID()); + part->rigidBody.bodyImpl = nullptr; } JPH::Shape* makeShape(std::shared_ptr basePart) {