Skip to content
Snippets Groups Projects
Commit 7834586a authored by Jonathan Kunstwald's avatar Jonathan Kunstwald
Browse files

Replace infinitely growing weak ptr vector with destruction event in physics engine

parent d34b716d
No related branches found
No related tags found
No related merge requests found
...@@ -60,6 +60,8 @@ void FlockApp::startupGameCore() ...@@ -60,6 +60,8 @@ void FlockApp::startupGameCore()
mParticleSystem = std::make_unique<ParticleSystem>(); mParticleSystem = std::make_unique<ParticleSystem>();
mParticleSystem->init(); mParticleSystem->init();
mPhysicsEngine.init(mRegistry);
} }
void FlockApp::loadLevel() void FlockApp::loadLevel()
......
...@@ -3,22 +3,17 @@ ...@@ -3,22 +3,17 @@
#include "PointQuery.hh" #include "PointQuery.hh"
#include "Raycast.hh" #include "Raycast.hh"
void flock::PhysicsEngine::onSensorDestruction(entt::registry<uint32_t> &ecs, uint32_t entity)
{
SensorC const &sensor = ecs.get<SensorC>(entity);
mScene.RemoveBody(sensor.sensor->body);
}
flock::PhysicsEngine::PhysicsEngine(float timestep) : mScene(q3Scene{timestep}) {} flock::PhysicsEngine::PhysicsEngine(float timestep) : mScene(q3Scene{timestep}) {}
flock::PhysicsEngine::~PhysicsEngine() void flock::PhysicsEngine::init(entt::registry<uint32_t> &reg)
{
/**
* Sensors have to get shutdown manually on destruction of the physics engine in order to disable their regular destructor
* TODO: Do the entire body destruction based on EnTT component destruction events, this vector will grow indefinitely
*/
for (auto &weakSensor : mPhysicsSensorBackup)
{ {
if (!weakSensor.expired()) reg.destruction<SensorC>().connect<&PhysicsEngine::onSensorDestruction>(this);
{
auto lockedSensor = weakSensor.lock();
lockedSensor->killOnSceneShutdown();
}
}
} }
void flock::PhysicsEngine::step() void flock::PhysicsEngine::step()
...@@ -68,6 +63,5 @@ void flock::PhysicsEngine::syncSensors(entt::registry<uint32_t> &reg) ...@@ -68,6 +63,5 @@ void flock::PhysicsEngine::syncSensors(entt::registry<uint32_t> &reg)
flock::SensorC flock::PhysicsEngine::createSensor(uint32_t entity, const glm::vec3 &position, const glm::vec3 &size) flock::SensorC flock::PhysicsEngine::createSensor(uint32_t entity, const glm::vec3 &position, const glm::vec3 &size)
{ {
auto sharedSensor = std::make_shared<PhysicsSensor>(entity, position, size, &mScene); auto sharedSensor = std::make_shared<PhysicsSensor>(entity, position, size, &mScene);
mPhysicsSensorBackup.push_back(sharedSensor);
return SensorC{sharedSensor}; return SensorC{sharedSensor};
} }
...@@ -20,12 +20,14 @@ class PhysicsEngine ...@@ -20,12 +20,14 @@ class PhysicsEngine
{ {
private: private:
q3Scene mScene; q3Scene mScene;
std::vector<std::weak_ptr<PhysicsSensor>> mPhysicsSensorBackup;
private:
void onSensorDestruction(entt::registry<uint32_t>& ecs, uint32_t entity);
public: public:
PhysicsEngine(float timestep); PhysicsEngine(float timestep);
~PhysicsEngine(); void init(entt::registry<uint32_t>& reg);
void step(); void step();
......
...@@ -13,31 +13,19 @@ class PhysicsEngine; ...@@ -13,31 +13,19 @@ class PhysicsEngine;
struct PhysicsSensor struct PhysicsSensor
{ {
private: private:
q3Scene* const mSceneBackref;
uint32_t const mEntity; uint32_t const mEntity;
bool mSceneAlive = true;
void killOnSceneShutdown()
{
mSceneBackref->RemoveBody(body);
mSceneAlive = false;
}
friend PhysicsEngine;
public: public:
q3Body* body; q3Body* body;
PhysicsSensor(uint32_t ent, glm::vec3 const& position, glm::vec3 const& size, q3Scene* scene) PhysicsSensor(uint32_t ent, glm::vec3 const& position, glm::vec3 const& size, q3Scene* scene) : mEntity(ent)
: mSceneBackref(scene), mEntity(ent)
{ {
q3BodyDef bodyDef; q3BodyDef bodyDef;
bodyDef.position = q3Vec3(position.x, position.y, position.z); bodyDef.position = q3Vec3(position.x, position.y, position.z);
bodyDef.bodyType = eKinematicBody; bodyDef.bodyType = eKinematicBody;
bodyDef.userData = this; bodyDef.userData = this;
body = mSceneBackref->CreateBody(bodyDef); body = scene->CreateBody(bodyDef);
q3BoxDef boxDef; q3BoxDef boxDef;
q3Transform localSpace; // Identity Transform (0,0,0) q3Transform localSpace; // Identity Transform (0,0,0)
...@@ -48,12 +36,6 @@ public: ...@@ -48,12 +36,6 @@ public:
body->AddBox(boxDef); body->AddBox(boxDef);
} }
~PhysicsSensor()
{
if (mSceneAlive)
mSceneBackref->RemoveBody(body);
}
void setVelocity(glm::vec3 const& vel) { body->SetLinearVelocity(q3Vec3(vel.x, vel.y, vel.z)); } void setVelocity(glm::vec3 const& vel) { body->SetLinearVelocity(q3Vec3(vel.x, vel.y, vel.z)); }
constexpr uint32_t getEntity() const noexcept { return mEntity; } constexpr uint32_t getEntity() const noexcept { return mEntity; }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment