From 964c733f53812a46304767a2f3ffbab6167c1169 Mon Sep 17 00:00:00 2001 From: maelstrom Date: Fri, 30 May 2025 02:15:58 +0200 Subject: [PATCH] feat(physics): parts fallen beyond fall height get automatically destroyed --- core/src/objects/workspace.cpp | 11 +++++++++-- core/src/objects/workspace.h | 2 ++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/core/src/objects/workspace.cpp b/core/src/objects/workspace.cpp index 694ec66..4b830fe 100644 --- a/core/src/objects/workspace.cpp +++ b/core/src/objects/workspace.cpp @@ -127,17 +127,19 @@ void Workspace::PhysicsStep(float deltaTime) { // Step the simulation a few steps physicsWorld->update(std::min(deltaTime / 2, (1/60.f))); - // Naive implementation. Parts are only considered so if they are just under Workspace // TODO: Add list of tracked parts in workspace based on their ancestry using inWorkspace property of Instance for (auto it = this->GetDescendantsStart(); it != this->GetDescendantsEnd(); it++) { std::shared_ptr obj = *it; - if (obj->GetClass()->className != "Part") continue; // TODO: Replace this with a .IsA call instead of comparing the class name directly + if (!obj->IsA()) continue; std::shared_ptr part = std::dynamic_pointer_cast(obj); + + // Sync properties const rp::Transform& transform = part->rigidBody->getTransform(); part->cframe = CFrame(transform); part->velocity = part->rigidBody->getLinearVelocity(); // part->rigidBody->enableGravity(true); + // RotateV/Motor joint for (auto& joint : part->secondaryJoints) { if (joint.expired() || !joint.lock()->IsA("RotateV")) continue; @@ -146,6 +148,11 @@ void Workspace::PhysicsStep(float deltaTime) { // part->rigidBody->enableGravity(false); part->rigidBody->setAngularVelocity(-(motor->part0.lock()->cframe * motor->c0).LookVector() * rate); } + + // Destroy fallen parts + if (part->cframe.Position().Y() < this->fallenPartsDestroyHeight) { + part->Destroy(); + } } } diff --git a/core/src/objects/workspace.h b/core/src/objects/workspace.h index 934b326..cf5c34b 100644 --- a/core/src/objects/workspace.h +++ b/core/src/objects/workspace.h @@ -66,6 +66,8 @@ public: Workspace(); ~Workspace(); + DEF_PROP float fallenPartsDestroyHeight = -500; + // static inline std::shared_ptr New() { return std::make_shared(); }; static inline std::shared_ptr Create() { return std::make_shared(); };