From: Mikko Rasa Date: Sun, 27 Nov 2022 21:48:39 +0000 (+0200) Subject: Remove destroyed entities from the physics simulation X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=338031de484ee117762d80449d9ca472a6918c92;p=libs%2Fgame.git Remove destroyed entities from the physics simulation --- diff --git a/examples/bassteroids/source/physics.cpp b/examples/bassteroids/source/physics.cpp index 3c43502..73d8582 100644 --- a/examples/bassteroids/source/physics.cpp +++ b/examples/bassteroids/source/physics.cpp @@ -1,5 +1,5 @@ #include "physics.h" -#include +#include #include #include "physicalentity.h" @@ -12,6 +12,7 @@ Physics::Physics(Game::Stage &s): observer(stage.get_event_bus()) { observer.observe([this](auto &e){ entity_added(e); }); + observer.observe([this](auto &e){ entity_removed(e); }); stage.synthesize_initial_events(observer); } @@ -36,6 +37,27 @@ void Physics::entity_added(const Game::Events::EntityCreated &e) } } +void Physics::entity_removed(const Game::Events::EntityDestroyed &e) +{ + if(Game::Handle physical = dynamic_handle_cast(e.entity)) + { + auto i = find_member(entities, physical, &SimulatedEntity::entity); + if(i!=entities.end()) + { + size_t index = distance(entities.begin(), i); + if(index