From 0e3a84a06ba252bcb06d86cfc49ae4718a9524e0 Mon Sep 17 00:00:00 2001 From: Diren D Bharwani Date: Wed, 1 Mar 2023 04:34:20 +0800 Subject: [PATCH] Fixed bug where changing scenes would not clear drawn colliders --- .../System/SHPhysicsDebugDrawSystem.cpp | 20 ++++++++++++++++--- .../Physics/System/SHPhysicsDebugDrawSystem.h | 15 ++++++++++---- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/SHADE_Engine/src/Physics/System/SHPhysicsDebugDrawSystem.cpp b/SHADE_Engine/src/Physics/System/SHPhysicsDebugDrawSystem.cpp index ae25216f..ad99c363 100644 --- a/SHADE_Engine/src/Physics/System/SHPhysicsDebugDrawSystem.cpp +++ b/SHADE_Engine/src/Physics/System/SHPhysicsDebugDrawSystem.cpp @@ -38,6 +38,9 @@ namespace SHADE : flags { 0 } { collidersToDraw.clear(); + + eventFunctions[0] = { &SHPhysicsDebugDrawSystem::onColliderDraw , SH_PHYSICS_COLLIDER_DRAW_EVENT }; + eventFunctions[1] = { &SHPhysicsDebugDrawSystem::onSceneExit , SH_SCENE_EXIT_PRE }; } /*-----------------------------------------------------------------------------------*/ @@ -78,10 +81,13 @@ namespace SHADE SystemFamily::GetID(); // Register collider draw event - const std::shared_ptr EVENT_RECEIVER = std::make_shared>(this, &SHPhysicsDebugDrawSystem::onColliderDraw); - const ReceiverPtr EVENT_RECEIVER_PTR = std::dynamic_pointer_cast(EVENT_RECEIVER); + for (int i = 0; i < NUM_EVENT_FUNCTIONS; ++i) + { + const std::shared_ptr EVENT_RECEIVER = std::make_shared>(this, eventFunctions[i].first); + const ReceiverPtr EVENT_RECEIVER_PTR = std::dynamic_pointer_cast(EVENT_RECEIVER); - SHEventManager::SubscribeTo(SH_PHYSICS_COLLIDER_DRAW_EVENT, EVENT_RECEIVER_PTR); + SHEventManager::SubscribeTo(eventFunctions[i].second, EVENT_RECEIVER_PTR); + } } void SHPhysicsDebugDrawSystem::Exit() @@ -122,6 +128,14 @@ namespace SHADE return onColliderDrawEvent.get()->handle; } + SHEventHandle SHPhysicsDebugDrawSystem::onSceneExit(SHEventPtr onSceneExitEvent) + { + // Empty list of colliders to draw + collidersToDraw.clear(); + return onSceneExitEvent.get()->handle; + } + + void SHPhysicsDebugDrawSystem::drawCollider(SHDebugDrawSystem* debugDrawSystem, const SHCollider& collider) noexcept { for (const auto* SHAPE : collider.GetCollisionShapes()) diff --git a/SHADE_Engine/src/Physics/System/SHPhysicsDebugDrawSystem.h b/SHADE_Engine/src/Physics/System/SHPhysicsDebugDrawSystem.h index 336dff0c..83f96377 100644 --- a/SHADE_Engine/src/Physics/System/SHPhysicsDebugDrawSystem.h +++ b/SHADE_Engine/src/Physics/System/SHPhysicsDebugDrawSystem.h @@ -89,6 +89,8 @@ namespace SHADE /* Type Definitions */ /*---------------------------------------------------------------------------------*/ + using EventFunctionPair = std::pair; + union DebugDrawInfo { struct Contact @@ -123,20 +125,25 @@ namespace SHADE /* Data Members */ /*---------------------------------------------------------------------------------*/ - static constexpr uint8_t ACTIVE_FLAG = 0x01; + static constexpr uint8_t ACTIVE_FLAG = 0x01; + static constexpr int NUM_EVENT_FUNCTIONS = 4; - static const SHColour DEBUG_DRAW_COLOURS[static_cast(Colours::COUNT)]; + static const SHColour DEBUG_DRAW_COLOURS[static_cast(Colours::COUNT)]; + + // Event function container for cleanly registering to events + EventFunctionPair eventFunctions[NUM_EVENT_FUNCTIONS]; // 0 0 0 drawBroadphase drawRaycasts drawContacts drawAllColliders debugDrawActive - uint8_t flags; + uint8_t flags; - Colliders collidersToDraw; + Colliders collidersToDraw; /*---------------------------------------------------------------------------------*/ /* Member Functions */ /*---------------------------------------------------------------------------------*/ SHEventHandle onColliderDraw(SHEventPtr onColliderDrawEvent); + SHEventHandle onSceneExit (SHEventPtr onSceneExitEvent); static void drawCollider (SHDebugDrawSystem* debugDrawSystem, const SHCollider& collider) noexcept; static void drawRaycast (SHDebugDrawSystem* debugDrawSystem, const DebugDrawInfo::Raycast& raycastInfo) noexcept;