fix(physics): deleting bodies

This commit is contained in:
maelstrom 2025-08-29 02:11:07 +02:00
parent cbeaa4d2bb
commit 0bd6acb7fa
2 changed files with 11 additions and 2 deletions

View file

@ -26,7 +26,6 @@ BasePart::BasePart(const InstanceType* type, PartConstructParams params): PVInst
} }
BasePart::~BasePart() { BasePart::~BasePart() {
// This relies on physicsCommon still existing. Be very careful.
if (workspace() != nullptr) { if (workspace() != nullptr) {
workspace()->RemoveBody(shared<BasePart>()); workspace()->RemoveBody(shared<BasePart>());
} }

View file

@ -1,4 +1,5 @@
#include "world.h" #include "world.h"
#include "Jolt/Geometry/AABox.h"
#include "datatypes/vector.h" #include "datatypes/vector.h"
#include "enum/part.h" #include "enum/part.h"
#include "logger.h" #include "logger.h"
@ -104,7 +105,16 @@ void PhysWorld::addBody(std::shared_ptr<BasePart> part) {
} }
void PhysWorld::removeBody(std::shared_ptr<BasePart> part) { void PhysWorld::removeBody(std::shared_ptr<BasePart> 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<JPH::Vec3>(part->position() - aabbSize), convert<JPH::Vec3>(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> basePart) { JPH::Shape* makeShape(std::shared_ptr<BasePart> basePart) {