refactor(part): moved collider builder to subclass
This commit is contained in:
parent
d23206b1fc
commit
2538673fb2
4 changed files with 24 additions and 12 deletions
|
@ -12,6 +12,8 @@
|
||||||
#include "enum/surface.h"
|
#include "enum/surface.h"
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
|
#include <reactphysics3d/body/RigidBody.h>
|
||||||
|
#include <reactphysics3d/engine/PhysicsCommon.h>
|
||||||
#include <reactphysics3d/reactphysics3d.h>
|
#include <reactphysics3d/reactphysics3d.h>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include "objects/annotation.h"
|
#include "objects/annotation.h"
|
||||||
|
@ -63,6 +65,8 @@ protected:
|
||||||
void OnAncestryChanged(std::optional<std::shared_ptr<Instance>> child, std::optional<std::shared_ptr<Instance>> newParent) override;
|
void OnAncestryChanged(std::optional<std::shared_ptr<Instance>> child, std::optional<std::shared_ptr<Instance>> newParent) override;
|
||||||
void onUpdated(std::string);
|
void onUpdated(std::string);
|
||||||
|
|
||||||
|
virtual void updateCollider(rp::PhysicsCommon* common) = 0;
|
||||||
|
|
||||||
BasePart(const InstanceType*);
|
BasePart(const InstanceType*);
|
||||||
BasePart(const InstanceType*, PartConstructParams params);
|
BasePart(const InstanceType*, PartConstructParams params);
|
||||||
public:
|
public:
|
||||||
|
|
|
@ -1,8 +1,24 @@
|
||||||
#include "part.h"
|
#include "part.h"
|
||||||
|
#include "physics/util.h"
|
||||||
|
|
||||||
Part::Part(): BasePart(&TYPE) {
|
Part::Part(): BasePart(&TYPE) {
|
||||||
}
|
}
|
||||||
|
|
||||||
Part::Part(PartConstructParams params): BasePart(&TYPE, params) {
|
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<rp::BoxShape*>(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());
|
||||||
}
|
}
|
|
@ -6,6 +6,9 @@
|
||||||
class DEF_INST Part : public BasePart {
|
class DEF_INST Part : public BasePart {
|
||||||
AUTOGEN_PREAMBLE
|
AUTOGEN_PREAMBLE
|
||||||
|
|
||||||
|
protected:
|
||||||
|
void updateCollider(rp::PhysicsCommon* common) override;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Part();
|
Part();
|
||||||
Part(PartConstructParams params);
|
Part(PartConstructParams params);
|
||||||
|
|
|
@ -113,18 +113,7 @@ void Workspace::updatePartPhysics(std::shared_ptr<BasePart> part) {
|
||||||
part->rigidBody->setTransform(transform);
|
part->rigidBody->setTransform(transform);
|
||||||
}
|
}
|
||||||
|
|
||||||
rp::BoxShape* shape = physicsCommon->createBoxShape(glmToRp(part->size * glm::vec3(0.5f)));
|
part->updateCollider(physicsCommon);
|
||||||
|
|
||||||
// Recreate the rigidbody if the shape changes
|
|
||||||
if (part->rigidBody->getNbColliders() > 0
|
|
||||||
&& dynamic_cast<rp::BoxShape*>(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->rigidBody->setType(part->anchored ? rp::BodyType::STATIC : rp::BodyType::DYNAMIC);
|
part->rigidBody->setType(part->anchored ? rp::BodyType::STATIC : rp::BodyType::DYNAMIC);
|
||||||
part->rigidBody->getCollider(0)->setCollisionCategoryBits(0b11);
|
part->rigidBody->getCollider(0)->setCollisionCategoryBits(0b11);
|
||||||
|
|
Loading…
Add table
Reference in a new issue