fix: rigid bodies not being cleaned up
This commit is contained in:
parent
e0eb0ac8bb
commit
b10abd3800
5 changed files with 38 additions and 0 deletions
|
@ -42,10 +42,17 @@ void Instance::SetParent(std::optional<std::shared_ptr<Instance>> newParent) {
|
||||||
// TODO: Add code for sending signals for parent updates
|
// TODO: Add code for sending signals for parent updates
|
||||||
// TODO: Yeahhh maybe this isn't the best way of doing this?
|
// TODO: Yeahhh maybe this isn't the best way of doing this?
|
||||||
if (hierarchyPostUpdateHandler.has_value()) hierarchyPostUpdateHandler.value()(this->shared_from_this(), lastParent, newParent);
|
if (hierarchyPostUpdateHandler.has_value()) hierarchyPostUpdateHandler.value()(this->shared_from_this(), lastParent, newParent);
|
||||||
|
|
||||||
|
this->OnParentUpdated(lastParent, newParent);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::optional<std::shared_ptr<Instance>> Instance::GetParent() {
|
std::optional<std::shared_ptr<Instance>> Instance::GetParent() {
|
||||||
if (!parent.has_value()) return std::nullopt;
|
if (!parent.has_value()) return std::nullopt;
|
||||||
if (parent.value().expired()) return std::nullopt;
|
if (parent.value().expired()) return std::nullopt;
|
||||||
return parent.value().lock();
|
return parent.value().lock();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void Instance::OnParentUpdated(std::optional<std::shared_ptr<Instance>> oldParent, std::optional<std::shared_ptr<Instance>> newParent) {
|
||||||
|
// Empty stub
|
||||||
}
|
}
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
#include "metadata.h"
|
#include "metadata.h"
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <optional>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
// Struct describing information about an instance
|
// Struct describing information about an instance
|
||||||
|
@ -23,6 +24,8 @@ private:
|
||||||
protected:
|
protected:
|
||||||
Instance(InstanceType*);
|
Instance(InstanceType*);
|
||||||
virtual ~Instance();
|
virtual ~Instance();
|
||||||
|
|
||||||
|
virtual void OnParentUpdated(std::optional<std::shared_ptr<Instance>> oldParent, std::optional<std::shared_ptr<Instance>> newParent);
|
||||||
public:
|
public:
|
||||||
static InstanceType* TYPE;
|
static InstanceType* TYPE;
|
||||||
std::string name;
|
std::string name;
|
||||||
|
|
|
@ -19,4 +19,19 @@ Part::Part(): Instance(&TYPE_) {
|
||||||
|
|
||||||
Part::Part(PartConstructParams params): Instance(&TYPE_), position(params.position), rotation(params.rotation),
|
Part::Part(PartConstructParams params): Instance(&TYPE_), position(params.position), rotation(params.rotation),
|
||||||
scale(params.scale), material(params.material), anchored(params.anchored) {
|
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<std::shared_ptr<Instance>> oldParent, std::optional<std::shared_ptr<Instance>> newParent) {
|
||||||
|
if (this->rigidBody)
|
||||||
|
this->rigidBody->setIsActive(newParent.has_value());
|
||||||
|
|
||||||
|
// TODO: Sleeping bodies that touch this one also need to be updated
|
||||||
}
|
}
|
|
@ -20,6 +20,8 @@ struct PartConstructParams {
|
||||||
};
|
};
|
||||||
|
|
||||||
class Part : public Instance {
|
class Part : public Instance {
|
||||||
|
protected:
|
||||||
|
void OnParentUpdated(std::optional<std::shared_ptr<Instance>> oldParent, std::optional<std::shared_ptr<Instance>> newParent) override;
|
||||||
public:
|
public:
|
||||||
static InstanceType* TYPE;
|
static InstanceType* TYPE;
|
||||||
|
|
||||||
|
@ -34,6 +36,7 @@ public:
|
||||||
|
|
||||||
Part();
|
Part();
|
||||||
Part(PartConstructParams params);
|
Part(PartConstructParams params);
|
||||||
|
~Part() override;
|
||||||
|
|
||||||
static inline std::shared_ptr<Part> New() { return std::make_shared<Part>(); };
|
static inline std::shared_ptr<Part> New() { return std::make_shared<Part>(); };
|
||||||
static inline std::shared_ptr<Part> New(PartConstructParams params) { return std::make_shared<Part>(params); };
|
static inline std::shared_ptr<Part> New(PartConstructParams params) { return std::make_shared<Part>(params); };
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
#include <reactphysics3d/collision/shapes/BoxShape.h>
|
#include <reactphysics3d/collision/shapes/BoxShape.h>
|
||||||
#include <reactphysics3d/collision/shapes/CollisionShape.h>
|
#include <reactphysics3d/collision/shapes/CollisionShape.h>
|
||||||
#include <reactphysics3d/components/RigidBodyComponents.h>
|
#include <reactphysics3d/components/RigidBodyComponents.h>
|
||||||
|
#include <reactphysics3d/engine/EventListener.h>
|
||||||
#include <reactphysics3d/mathematics/Quaternion.h>
|
#include <reactphysics3d/mathematics/Quaternion.h>
|
||||||
#include <reactphysics3d/mathematics/Transform.h>
|
#include <reactphysics3d/mathematics/Transform.h>
|
||||||
#include <reactphysics3d/mathematics/Vector3.h>
|
#include <reactphysics3d/mathematics/Vector3.h>
|
||||||
|
@ -19,8 +20,15 @@
|
||||||
|
|
||||||
namespace rp = reactphysics3d;
|
namespace rp = reactphysics3d;
|
||||||
|
|
||||||
|
class PhysicsListener : public rp::EventListener {
|
||||||
|
void onContact(const CollisionCallback::CallbackData& /*callbackData*/) override {
|
||||||
|
// printf("Collision occurred!\n");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
rp::PhysicsCommon physicsCommon;
|
rp::PhysicsCommon physicsCommon;
|
||||||
rp::PhysicsWorld* world;
|
rp::PhysicsWorld* world;
|
||||||
|
PhysicsListener eventListener;
|
||||||
|
|
||||||
void simulationInit() {
|
void simulationInit() {
|
||||||
world = physicsCommon.createPhysicsWorld();
|
world = physicsCommon.createPhysicsWorld();
|
||||||
|
@ -47,6 +55,8 @@ void syncPartPhysics(std::shared_ptr<Part> part) {
|
||||||
if (part->rigidBody->getNbColliders() == 0)
|
if (part->rigidBody->getNbColliders() == 0)
|
||||||
part->rigidBody->addCollider(shape, rp::Transform());
|
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);
|
||||||
|
|
||||||
|
world->setEventListener(&eventListener);
|
||||||
}
|
}
|
||||||
|
|
||||||
void physicsStep(float deltaTime) {
|
void physicsStep(float deltaTime) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue