From 89f1f600640c9d7420a674b4192ae8ad950f8c58 Mon Sep 17 00:00:00 2001 From: Diren D Bharwani Date: Sat, 24 Dec 2022 02:19:53 +0800 Subject: [PATCH] Added physics settings menu for easily toggling debug draw states --- Assets/Scenes/PhysicsSandbox.shade | 16 +++++++------- .../src/Application/SBApplication.cpp | 6 ------ .../EditorWindow/MenuBar/SHEditorMenuBar.cpp | 21 +++++++++++++++++++ .../src/Physics/Dynamics/SHPhysicsWorld.cpp | 5 +++++ .../src/Physics/Dynamics/SHPhysicsWorld.h | 1 + .../Routines/SHPhysicsDebugDrawRoutine.cpp | 11 ++++++++-- .../System/SHPhysicsDebugDrawSystem.cpp | 16 +++++++++++--- .../Physics/System/SHPhysicsDebugDrawSystem.h | 20 ++++++++++-------- .../src/Physics/System/SHPhysicsSystem.h | 10 ++++----- 9 files changed, 73 insertions(+), 33 deletions(-) diff --git a/Assets/Scenes/PhysicsSandbox.shade b/Assets/Scenes/PhysicsSandbox.shade index 1438b558..60a97885 100644 --- a/Assets/Scenes/PhysicsSandbox.shade +++ b/Assets/Scenes/PhysicsSandbox.shade @@ -4,7 +4,7 @@ NumberOfChildren: 0 Components: Transform Component: - Translate: {x: 0, y: 3, z: 0} + Translate: {x: 0.0700113177, y: 2.5, z: 0} Rotate: {x: -0, y: 0, z: -0} Scale: {x: 1, y: 1, z: 1} IsActive: true @@ -19,14 +19,14 @@ Interpolate: true Sleeping Enabled: true Freeze Position X: false - Freeze Position Y: true + Freeze Position Y: false Freeze Position Z: false Freeze Rotation X: false Freeze Rotation Y: false Freeze Rotation Z: false IsActive: true Collider Component: - DrawColliders: true + DrawColliders: false Colliders: - Is Trigger: false Type: Sphere @@ -65,8 +65,8 @@ NumberOfChildren: 0 Components: Transform Component: - Translate: {x: 0, y: -2.5, z: 0} - Rotate: {x: 0, y: 0, z: 0} + Translate: {x: 0, y: 0, z: 0} + Rotate: {x: -0, y: 0, z: -0} Scale: {x: 1, y: 1, z: 1} IsActive: true RigidBody Component: @@ -87,11 +87,11 @@ Freeze Rotation Z: false IsActive: true Collider Component: - DrawColliders: true + DrawColliders: false Colliders: - Is Trigger: false Type: Sphere - Radius: 5 + Radius: 2.5 Friction: 0.400000006 Bounciness: 0 Density: 1 @@ -127,7 +127,7 @@ Freeze Rotation Z: false IsActive: true Collider Component: - DrawColliders: true + DrawColliders: false Colliders: - Is Trigger: false Type: Sphere diff --git a/SHADE_Application/src/Application/SBApplication.cpp b/SHADE_Application/src/Application/SBApplication.cpp index 8607702d..dfeb781b 100644 --- a/SHADE_Application/src/Application/SBApplication.cpp +++ b/SHADE_Application/src/Application/SBApplication.cpp @@ -185,12 +185,6 @@ namespace Sandbox #endif SHSceneManager::SceneUpdate(0.016f); #ifdef SHEDITOR - static bool drawBP = false; - if (SHInputManager::GetKeyDown(SHInputManager::SH_KEYCODE::RIGHT_CTRL)) - { - drawBP = !drawBP; - SHSystemManager::GetSystem()->SetFlagState(SHPhysicsDebugDrawSystem::DebugDrawFlags::BROADPHASE, drawBP); - } SHSystemManager::RunRoutines(editor->editorState != SHEditor::State::PLAY, SHFrameRateController::GetRawDeltaTime()); editor->PollPicking(); diff --git a/SHADE_Engine/src/Editor/EditorWindow/MenuBar/SHEditorMenuBar.cpp b/SHADE_Engine/src/Editor/EditorWindow/MenuBar/SHEditorMenuBar.cpp index a1335e19..a08f0f75 100644 --- a/SHADE_Engine/src/Editor/EditorWindow/MenuBar/SHEditorMenuBar.cpp +++ b/SHADE_Engine/src/Editor/EditorWindow/MenuBar/SHEditorMenuBar.cpp @@ -25,6 +25,7 @@ #include "Serialization/SHSerialization.h" #include "Serialization/Configurations/SHConfigurationManager.h" #include "Editor/EditorWindow/SHEditorWindowManager.h" +#include "Physics/System/SHPhysicsDebugDrawSystem.h" const std::string LAYOUT_FOLDER_PATH{ std::string(ASSET_ROOT) + "/Editor/Layouts" }; @@ -202,6 +203,26 @@ namespace SHADE ImGui::EndMenu(); } + if (ImGui::BeginMenu("Physics Settings")) + { + if (auto* physicsDebugDraw = SHSystemManager::GetSystem()) + { + bool drawColliders = physicsDebugDraw->GetFlagState(SHPhysicsDebugDrawSystem::DebugDrawFlags::COLLIDERS); + if (ImGui::Checkbox("Draw Colliders", &drawColliders)) + physicsDebugDraw->SetFlagState(SHPhysicsDebugDrawSystem::DebugDrawFlags::COLLIDERS, drawColliders); + + bool drawContactPoints = physicsDebugDraw->GetFlagState(SHPhysicsDebugDrawSystem::DebugDrawFlags::CONTACTS); + if (ImGui::Checkbox("Draw Contact Points", &drawContactPoints)) + physicsDebugDraw->SetFlagState(SHPhysicsDebugDrawSystem::DebugDrawFlags::CONTACTS, drawContactPoints); + + bool drawBroadphase = physicsDebugDraw->GetFlagState(SHPhysicsDebugDrawSystem::DebugDrawFlags::BROADPHASE); + if (ImGui::Checkbox("Draw Broadphase", &drawBroadphase)) + physicsDebugDraw->SetFlagState(SHPhysicsDebugDrawSystem::DebugDrawFlags::BROADPHASE, drawBroadphase); + } + + ImGui::EndMenu(); + } + ImGui::EndMainMenuBar(); } diff --git a/SHADE_Engine/src/Physics/Dynamics/SHPhysicsWorld.cpp b/SHADE_Engine/src/Physics/Dynamics/SHPhysicsWorld.cpp index 8451bd5b..4e6e8165 100644 --- a/SHADE_Engine/src/Physics/Dynamics/SHPhysicsWorld.cpp +++ b/SHADE_Engine/src/Physics/Dynamics/SHPhysicsWorld.cpp @@ -35,6 +35,11 @@ namespace SHADE /* Getter Functions Definitions */ /*-----------------------------------------------------------------------------------*/ + const SHContactManager::ContactPoints& SHPhysicsWorld::GetContactPoints() const noexcept + { + return contactManager.GetContactPoints(); + } + const SHContactManager::TriggerEvents& SHPhysicsWorld::GetTriggerEvents() const noexcept { return contactManager.GetTriggerEvents(); diff --git a/SHADE_Engine/src/Physics/Dynamics/SHPhysicsWorld.h b/SHADE_Engine/src/Physics/Dynamics/SHPhysicsWorld.h index 9bc2199e..f63bac40 100644 --- a/SHADE_Engine/src/Physics/Dynamics/SHPhysicsWorld.h +++ b/SHADE_Engine/src/Physics/Dynamics/SHPhysicsWorld.h @@ -72,6 +72,7 @@ namespace SHADE /* Getter Functions */ /*---------------------------------------------------------------------------------*/ + const SHContactManager::ContactPoints& GetContactPoints () const noexcept; const SHContactManager::TriggerEvents& GetTriggerEvents () const noexcept; const SHContactManager::CollisionEvents& GetCollisionEvents () const noexcept; diff --git a/SHADE_Engine/src/Physics/System/Routines/SHPhysicsDebugDrawRoutine.cpp b/SHADE_Engine/src/Physics/System/Routines/SHPhysicsDebugDrawRoutine.cpp index 9bf43115..8aad19f5 100644 --- a/SHADE_Engine/src/Physics/System/Routines/SHPhysicsDebugDrawRoutine.cpp +++ b/SHADE_Engine/src/Physics/System/Routines/SHPhysicsDebugDrawRoutine.cpp @@ -38,7 +38,7 @@ namespace SHADE { auto* physicsDebugDrawSystem = reinterpret_cast(GetSystem()); - if (!physicsDebugDrawSystem->GetFlagState(DebugDrawFlags::ACTIVE)) + if (!physicsDebugDrawSystem->IsDebugDrawActive()) return; auto* debugDrawSystem = SHSystemManager::GetSystem(); @@ -74,7 +74,14 @@ namespace SHADE if (DRAW_CONTACTS) { - // TODO + const SHColour& CONTACT_COLOUR = physicsDebugDrawSystem->DEBUG_DRAW_COLOURS[SHUtilities::ConvertEnum(Colours::CONTACT)]; + + const auto& CONTACT_POINTS = physicsSystem->physicsWorld->GetContactPoints(); + for (auto& contactPoint : CONTACT_POINTS) + { + const SHMatrix TRS = SHMatrix::Transform(contactPoint, SHQuaternion::Identity, SHVec3{ 0.1f }); + debugDrawSystem->DrawCube(TRS, CONTACT_COLOUR); + } } if (DRAW_RAYCASTS) diff --git a/SHADE_Engine/src/Physics/System/SHPhysicsDebugDrawSystem.cpp b/SHADE_Engine/src/Physics/System/SHPhysicsDebugDrawSystem.cpp index 958ac61a..9940ad20 100644 --- a/SHADE_Engine/src/Physics/System/SHPhysicsDebugDrawSystem.cpp +++ b/SHADE_Engine/src/Physics/System/SHPhysicsDebugDrawSystem.cpp @@ -44,6 +44,11 @@ namespace SHADE /* Getter Function Definitions */ /*-----------------------------------------------------------------------------------*/ + bool SHPhysicsDebugDrawSystem::IsDebugDrawActive() const noexcept + { + return flags & ACTIVE_FLAG; + } + bool SHPhysicsDebugDrawSystem::GetFlagState(DebugDrawFlags flag) const noexcept { const uint8_t ENUM_VALUE = SHUtilities::ConvertEnum(flag); @@ -58,6 +63,10 @@ namespace SHADE { const uint8_t ENUM_VALUE = SHUtilities::ConvertEnum(flag); state ? flags |= ENUM_VALUE : flags &= ~ENUM_VALUE; + + // If no other debug drawing state is active, turn active off. Otherwise, we maintain + // the active state. + flags == ACTIVE_FLAG ? flags = 0 : flags |= ACTIVE_FLAG; } /*-----------------------------------------------------------------------------------*/ @@ -97,7 +106,7 @@ namespace SHADE if (EVENT_DATA->debugDrawState) { if (collidersToDraw.empty()) - SetFlagState(DebugDrawFlags::ACTIVE, true); + flags |= ACTIVE_FLAG; collidersToDraw.emplace(EVENT_DATA->entityID); } @@ -105,8 +114,9 @@ namespace SHADE { collidersToDraw.erase(EVENT_DATA->entityID); - if (collidersToDraw.empty()) - SetFlagState(DebugDrawFlags::ACTIVE, false); + // if no colliders queued for drawing and no other debug drawing is enabled, disable debug drawing + if (collidersToDraw.empty() && flags == ACTIVE_FLAG) + flags = 0; } return onColliderDrawEvent.get()->handle; diff --git a/SHADE_Engine/src/Physics/System/SHPhysicsDebugDrawSystem.h b/SHADE_Engine/src/Physics/System/SHPhysicsDebugDrawSystem.h index 79b88d3f..14f86364 100644 --- a/SHADE_Engine/src/Physics/System/SHPhysicsDebugDrawSystem.h +++ b/SHADE_Engine/src/Physics/System/SHPhysicsDebugDrawSystem.h @@ -35,11 +35,10 @@ namespace SHADE enum class DebugDrawFlags : uint8_t { - ACTIVE = 0x0001 - , COLLIDERS = 0x0002 - , CONTACTS = 0x0004 - , RAYCASTS = 0x0008 - , BROADPHASE = 0x0010 + COLLIDERS = 0x02 + , CONTACTS = 0x04 + , RAYCASTS = 0x08 + , BROADPHASE = 0x10 }; /*---------------------------------------------------------------------------------*/ @@ -53,7 +52,8 @@ namespace SHADE /* Getter Functions */ /*---------------------------------------------------------------------------------*/ - [[nodiscard]] bool GetFlagState (DebugDrawFlags flag) const noexcept; + [[nodiscard]] bool IsDebugDrawActive () const noexcept; + [[nodiscard]] bool GetFlagState (DebugDrawFlags flag) const noexcept; /*---------------------------------------------------------------------------------*/ /* Setter Functions */ /*---------------------------------------------------------------------------------*/ @@ -64,10 +64,10 @@ namespace SHADE /* Member Functions */ /*---------------------------------------------------------------------------------*/ - void Init () override; - void Exit () override; + void Init () override; + void Exit () override; - void AddRaycast (const SHRay& ray, const SHPhysicsRaycastResult& result) noexcept; + void AddRaycast (const SHRay& ray, const SHPhysicsRaycastResult& result) noexcept; /*---------------------------------------------------------------------------------*/ /* System Routines */ @@ -123,6 +123,8 @@ namespace SHADE /* Data Members */ /*---------------------------------------------------------------------------------*/ + static constexpr uint8_t ACTIVE_FLAG = 0x01; + static const SHColour DEBUG_DRAW_COLOURS[static_cast(Colours::COUNT)]; // 0 0 0 drawBroadphase drawRaycasts drawContacts drawAllColliders debugDrawActive diff --git a/SHADE_Engine/src/Physics/System/SHPhysicsSystem.h b/SHADE_Engine/src/Physics/System/SHPhysicsSystem.h index ab28a299..c7dff6c6 100644 --- a/SHADE_Engine/src/Physics/System/SHPhysicsSystem.h +++ b/SHADE_Engine/src/Physics/System/SHPhysicsSystem.h @@ -52,8 +52,11 @@ namespace SHADE /* Getter Functions */ /*---------------------------------------------------------------------------------*/ - [[nodiscard]] double GetFixedUpdateRate() const noexcept; - [[nodiscard]] double GetFixedDT () const noexcept; + [[nodiscard]] double GetFixedUpdateRate() const noexcept; + [[nodiscard]] double GetFixedDT () const noexcept; + + [[nodiscard]] const std::vector& GetTriggerInfo () const noexcept; + [[nodiscard]] const std::vector& GetCollisionInfo () const noexcept; /*---------------------------------------------------------------------------------*/ /* Setter Functions */ @@ -62,9 +65,6 @@ namespace SHADE void SetFixedUpdateRate(double fixedUpdateRate) noexcept; void SetFixedDT(double fixedDt) noexcept; - const std::vector& GetTriggerInfo () const noexcept; - const std::vector& GetCollisionInfo () const noexcept; - /*---------------------------------------------------------------------------------*/ /* Member Functions */ /*---------------------------------------------------------------------------------*/