feat(physics): parts fallen beyond fall height get automatically destroyed

This commit is contained in:
maelstrom 2025-05-30 02:15:58 +02:00
parent 19b6489476
commit 964c733f53
2 changed files with 11 additions and 2 deletions

View file

@ -127,17 +127,19 @@ void Workspace::PhysicsStep(float deltaTime) {
// Step the simulation a few steps // Step the simulation a few steps
physicsWorld->update(std::min(deltaTime / 2, (1/60.f))); 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 // 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++) { for (auto it = this->GetDescendantsStart(); it != this->GetDescendantsEnd(); it++) {
std::shared_ptr<Instance> obj = *it; std::shared_ptr<Instance> 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<Part>()) continue;
std::shared_ptr<Part> part = std::dynamic_pointer_cast<Part>(obj); std::shared_ptr<Part> part = std::dynamic_pointer_cast<Part>(obj);
// Sync properties
const rp::Transform& transform = part->rigidBody->getTransform(); const rp::Transform& transform = part->rigidBody->getTransform();
part->cframe = CFrame(transform); part->cframe = CFrame(transform);
part->velocity = part->rigidBody->getLinearVelocity(); part->velocity = part->rigidBody->getLinearVelocity();
// part->rigidBody->enableGravity(true); // part->rigidBody->enableGravity(true);
// RotateV/Motor joint
for (auto& joint : part->secondaryJoints) { for (auto& joint : part->secondaryJoints) {
if (joint.expired() || !joint.lock()->IsA("RotateV")) continue; if (joint.expired() || !joint.lock()->IsA("RotateV")) continue;
@ -146,6 +148,11 @@ void Workspace::PhysicsStep(float deltaTime) {
// part->rigidBody->enableGravity(false); // part->rigidBody->enableGravity(false);
part->rigidBody->setAngularVelocity(-(motor->part0.lock()->cframe * motor->c0).LookVector() * rate); part->rigidBody->setAngularVelocity(-(motor->part0.lock()->cframe * motor->c0).LookVector() * rate);
} }
// Destroy fallen parts
if (part->cframe.Position().Y() < this->fallenPartsDestroyHeight) {
part->Destroy();
}
} }
} }

View file

@ -66,6 +66,8 @@ public:
Workspace(); Workspace();
~Workspace(); ~Workspace();
DEF_PROP float fallenPartsDestroyHeight = -500;
// static inline std::shared_ptr<Workspace> New() { return std::make_shared<Workspace>(); }; // static inline std::shared_ptr<Workspace> New() { return std::make_shared<Workspace>(); };
static inline std::shared_ptr<Instance> Create() { return std::make_shared<Workspace>(); }; static inline std::shared_ptr<Instance> Create() { return std::make_shared<Workspace>(); };