diff --git a/core/src/objects/part/basepart.h b/core/src/objects/part/basepart.h index 53f5f36..bec89f9 100644 --- a/core/src/objects/part/basepart.h +++ b/core/src/objects/part/basepart.h @@ -12,6 +12,8 @@ #include "enum/surface.h" #include #include +#include +#include #include #include #include "objects/annotation.h" @@ -63,6 +65,8 @@ protected: void OnAncestryChanged(std::optional> child, std::optional> newParent) override; void onUpdated(std::string); + virtual void updateCollider(rp::PhysicsCommon* common) = 0; + BasePart(const InstanceType*); BasePart(const InstanceType*, PartConstructParams params); public: diff --git a/core/src/objects/part/part.cpp b/core/src/objects/part/part.cpp index 5bb66a2..83c6e6f 100644 --- a/core/src/objects/part/part.cpp +++ b/core/src/objects/part/part.cpp @@ -1,8 +1,24 @@ #include "part.h" +#include "physics/util.h" Part::Part(): BasePart(&TYPE) { } Part::Part(PartConstructParams params): BasePart(&TYPE, params) { +} + +void Part::updateCollider(rp::PhysicsCommon* common) { + rp::BoxShape* shape = common->createBoxShape(glmToRp(size * glm::vec3(0.5f))); + + // Recreate the rigidbody if the shape changes + if (rigidBody->getNbColliders() > 0 + && dynamic_cast(rigidBody->getCollider(0)->getCollisionShape())->getHalfExtents() != shape->getHalfExtents()) { + // TODO: This causes Touched to get called twice. Fix this. + rigidBody->removeCollider(rigidBody->getCollider(0)); + rigidBody->addCollider(shape, rp::Transform()); + } + + if (rigidBody->getNbColliders() == 0) + rigidBody->addCollider(shape, rp::Transform()); } \ No newline at end of file diff --git a/core/src/objects/part/part.h b/core/src/objects/part/part.h index 1d5e8ce..851abec 100644 --- a/core/src/objects/part/part.h +++ b/core/src/objects/part/part.h @@ -6,6 +6,9 @@ class DEF_INST Part : public BasePart { AUTOGEN_PREAMBLE +protected: + void updateCollider(rp::PhysicsCommon* common) override; + public: Part(); Part(PartConstructParams params); diff --git a/core/src/objects/service/workspace.cpp b/core/src/objects/service/workspace.cpp index c729640..bf3171d 100644 --- a/core/src/objects/service/workspace.cpp +++ b/core/src/objects/service/workspace.cpp @@ -113,18 +113,7 @@ void Workspace::updatePartPhysics(std::shared_ptr part) { part->rigidBody->setTransform(transform); } - rp::BoxShape* shape = physicsCommon->createBoxShape(glmToRp(part->size * glm::vec3(0.5f))); - - // Recreate the rigidbody if the shape changes - if (part->rigidBody->getNbColliders() > 0 - && dynamic_cast(part->rigidBody->getCollider(0)->getCollisionShape())->getHalfExtents() != shape->getHalfExtents()) { - // TODO: This causes Touched to get called twice. Fix this. - part->rigidBody->removeCollider(part->rigidBody->getCollider(0)); - part->rigidBody->addCollider(shape, rp::Transform()); - } - - if (part->rigidBody->getNbColliders() == 0) - part->rigidBody->addCollider(shape, rp::Transform()); + part->updateCollider(physicsCommon); part->rigidBody->setType(part->anchored ? rp::BodyType::STATIC : rp::BodyType::DYNAMIC); part->rigidBody->getCollider(0)->setCollisionCategoryBits(0b11);