diff --git a/src/objects/base/instance.cpp b/src/objects/base/instance.cpp index 6cbd305..27b217d 100644 --- a/src/objects/base/instance.cpp +++ b/src/objects/base/instance.cpp @@ -42,10 +42,17 @@ void Instance::SetParent(std::optional> newParent) { // TODO: Add code for sending signals for parent updates // TODO: Yeahhh maybe this isn't the best way of doing this? if (hierarchyPostUpdateHandler.has_value()) hierarchyPostUpdateHandler.value()(this->shared_from_this(), lastParent, newParent); + + this->OnParentUpdated(lastParent, newParent); } std::optional> Instance::GetParent() { if (!parent.has_value()) return std::nullopt; if (parent.value().expired()) return std::nullopt; return parent.value().lock(); +} + + +void Instance::OnParentUpdated(std::optional> oldParent, std::optional> newParent) { + // Empty stub } \ No newline at end of file diff --git a/src/objects/base/instance.h b/src/objects/base/instance.h index bb7baa6..abc3725 100644 --- a/src/objects/base/instance.h +++ b/src/objects/base/instance.h @@ -2,6 +2,7 @@ #include "metadata.h" #include +#include #include // Struct describing information about an instance @@ -23,6 +24,8 @@ private: protected: Instance(InstanceType*); virtual ~Instance(); + + virtual void OnParentUpdated(std::optional> oldParent, std::optional> newParent); public: static InstanceType* TYPE; std::string name; diff --git a/src/objects/part.cpp b/src/objects/part.cpp index d91fbae..54e99c2 100644 --- a/src/objects/part.cpp +++ b/src/objects/part.cpp @@ -19,4 +19,19 @@ Part::Part(): Instance(&TYPE_) { Part::Part(PartConstructParams params): Instance(&TYPE_), position(params.position), rotation(params.rotation), scale(params.scale), material(params.material), anchored(params.anchored) { +} + +// 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() { + world->destroyRigidBody(rigidBody); + Instance::~Instance(); +} + + +void Part::OnParentUpdated(std::optional> oldParent, std::optional> newParent) { + if (this->rigidBody) + this->rigidBody->setIsActive(newParent.has_value()); + + // TODO: Sleeping bodies that touch this one also need to be updated } \ No newline at end of file diff --git a/src/objects/part.h b/src/objects/part.h index c4aed5b..480ad99 100644 --- a/src/objects/part.h +++ b/src/objects/part.h @@ -20,6 +20,8 @@ struct PartConstructParams { }; class Part : public Instance { +protected: + void OnParentUpdated(std::optional> oldParent, std::optional> newParent) override; public: static InstanceType* TYPE; @@ -34,6 +36,7 @@ public: Part(); Part(PartConstructParams params); + ~Part() override; static inline std::shared_ptr New() { return std::make_shared(); }; static inline std::shared_ptr New(PartConstructParams params) { return std::make_shared(params); }; diff --git a/src/physics/simulation.cpp b/src/physics/simulation.cpp index 1c43769..7cb3dd4 100644 --- a/src/physics/simulation.cpp +++ b/src/physics/simulation.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -19,8 +20,15 @@ namespace rp = reactphysics3d; +class PhysicsListener : public rp::EventListener { + void onContact(const CollisionCallback::CallbackData& /*callbackData*/) override { + // printf("Collision occurred!\n"); + } +}; + rp::PhysicsCommon physicsCommon; rp::PhysicsWorld* world; +PhysicsListener eventListener; void simulationInit() { world = physicsCommon.createPhysicsWorld(); @@ -47,6 +55,8 @@ void syncPartPhysics(std::shared_ptr part) { if (part->rigidBody->getNbColliders() == 0) part->rigidBody->addCollider(shape, rp::Transform()); part->rigidBody->setType(part->anchored ? rp::BodyType::STATIC : rp::BodyType::DYNAMIC); + + world->setEventListener(&eventListener); } void physicsStep(float deltaTime) {