From 92693061464f3aa7f682eec97baf6ebc536a7375 Mon Sep 17 00:00:00 2001 From: Isuru Samarathunga Date: Mon, 29 Sep 2025 14:53:06 +0530 Subject: [PATCH] Physics Debug Draw --- Src/IAESandbox/imp/cpp/Game.cpp | 1 + Src/IAEngine/imp/cpp/Components/Physics.cpp | 1 + Src/IAEngine/imp/cpp/IAEngine.cpp | 5 ++ Src/IAEngine/imp/cpp/Physics/Physics.cpp | 29 +++++-- Src/IAEngine/imp/cpp/Rendering/DebugDraw.cpp | 87 +++---------------- Src/IAEngine/inc/IAEngine/Physics/Physics.hpp | 2 + .../inc/IAEngine/Rendering/DebugDraw.hpp | 15 ++-- 7 files changed, 54 insertions(+), 86 deletions(-) diff --git a/Src/IAESandbox/imp/cpp/Game.cpp b/Src/IAESandbox/imp/cpp/Game.cpp index c2528ab..8f17ca2 100644 --- a/Src/IAESandbox/imp/cpp/Game.cpp +++ b/Src/IAESandbox/imp/cpp/Game.cpp @@ -35,6 +35,7 @@ namespace ia::iae::game t->BakeAnimations(); } g_playerPhysics = g_player->AddComponent(); + g_playerPhysics->IsDynamic() = true; g_playerPhysics->AddCollider({ .IsTrigger = false, .Position = { diff --git a/Src/IAEngine/imp/cpp/Components/Physics.cpp b/Src/IAEngine/imp/cpp/Components/Physics.cpp index 10d3b6c..f594b01 100644 --- a/Src/IAEngine/imp/cpp/Components/Physics.cpp +++ b/Src/IAEngine/imp/cpp/Components/Physics.cpp @@ -56,6 +56,7 @@ namespace ia::iae VOID PhysicsComponent::Move(IN glm::vec2 direction) { + IA_ASSERT(m_isDynamic); const auto v = direction * m_movementSpeed; for(const auto& t: m_colliders) { diff --git a/Src/IAEngine/imp/cpp/IAEngine.cpp b/Src/IAEngine/imp/cpp/IAEngine.cpp index 1035dad..62c3674 100644 --- a/Src/IAEngine/imp/cpp/IAEngine.cpp +++ b/Src/IAEngine/imp/cpp/IAEngine.cpp @@ -36,6 +36,8 @@ namespace ia::iae { CONSTEXPR FLOAT32 GAME_UPDATE_INTERVAL = 1000.0f / 60.0f; + EXTERN BOOL g_physicsDebugDrawEnabled; + SDL_Event g_event{}; FLOAT32 g_updateTimer{}; BOOL g_shouldClose{false}; @@ -132,6 +134,9 @@ namespace ia::iae VOID Engine::UpdateGame() { + if(Input::WasKeyPressed(Input::KEY_F6)) + g_physicsDebugDrawEnabled = !g_physicsDebugDrawEnabled; + Physics::Update(); if B_LIKELY (g_activeScene) diff --git a/Src/IAEngine/imp/cpp/Physics/Physics.cpp b/Src/IAEngine/imp/cpp/Physics/Physics.cpp index 54a6e17..3b79eef 100644 --- a/Src/IAEngine/imp/cpp/Physics/Physics.cpp +++ b/Src/IAEngine/imp/cpp/Physics/Physics.cpp @@ -16,6 +16,7 @@ #include #include +#include #include @@ -23,6 +24,8 @@ namespace ia::iae { + BOOL g_physicsDebugDrawEnabled = false; + Vector g_physicsComponents; FLOAT32 Sq(IN FLOAT32 v) @@ -45,7 +48,7 @@ namespace ia::iae const auto dY = abs(d.y); if (d.x >= 0) { - if (dX >= dY) + if (dX > dY) { return RectSide::RIGHT; } @@ -56,7 +59,7 @@ namespace ia::iae } else { - if (dX >= dY) + if (dX > dY) { return RectSide::LEFT; } @@ -80,6 +83,23 @@ namespace ia::iae { } + VOID Physics::DebugDraw() + { + if(!g_physicsDebugDrawEnabled) + return; + + for(const auto& t: g_physicsComponents) + { + for(const auto& c: t->Colliders()) + { + auto color = glm::vec4{0.75f, 0.0f, 0.0f, 1.0f}; + if(c.IsTrigger) + color = {0.25f, 0.45f, 0.75f, 0.75f}; + DebugDraw::DrawRect(t->GetNode()->GetPosition() + c.Position, c.Size, color, 2.0f); + } + } + } + Handle Physics::RegisterComponent(IN PhysicsComponent *component) { g_physicsComponents.pushBack(component); @@ -101,17 +121,16 @@ namespace ia::iae const auto tMiddle = t->GetNode()->GetPosition() + tc.Position + tc.Size / 2.0f; const auto tHalfSize = tc.Size / 2.0f; const auto rectSide = GetRectSide(tMiddle - middle); - const auto distance = Sq(tMiddle.x - middle.x) + Sq(tMiddle.y - middle.y); switch (rectSide) { case RectSide::LEFT: case RectSide::RIGHT: - isColliding = (Sq(tHalfSize.x + halfSize.x) >= distance); + isColliding = ((tHalfSize.x + halfSize.x) >= abs(tMiddle.x - middle.x)); break; case RectSide::TOP: case RectSide::BOTTOM: - isColliding = (Sq(tHalfSize.y + halfSize.y) >= distance); + isColliding = ((tHalfSize.y + halfSize.y) >= abs(tMiddle.y - middle.y)); break; default: diff --git a/Src/IAEngine/imp/cpp/Rendering/DebugDraw.cpp b/Src/IAEngine/imp/cpp/Rendering/DebugDraw.cpp index 9ca3a72..bcdbfac 100644 --- a/Src/IAEngine/imp/cpp/Rendering/DebugDraw.cpp +++ b/Src/IAEngine/imp/cpp/Rendering/DebugDraw.cpp @@ -21,6 +21,7 @@ #include #include #include +#include #include @@ -34,23 +35,6 @@ namespace ia::iae ImGuiIO g_imGUIIO{}; - struct ShapeLine - { - ImVec2 To{}; - ImVec2 From{}; - ImColor Color{}; - FLOAT32 Thickness{1.0f}; - BOOL Visibility{true}; - }; - - struct ShapeRect - { - ImVec2 TopLeft{}; - ImVec2 BottomRight{}; - ImColor Color{}; - BOOL Visibility{true}; - }; - struct UIWindow { PCCHAR Title{""}; @@ -61,8 +45,6 @@ namespace ia::iae }; Vector g_debugUIWindows; - Vector g_lineShapes; - Vector g_rectShapes; VOID DebugDraw::Initailize() { @@ -107,47 +89,26 @@ namespace ia::iae w.ContentDrawCallback(); ImGui::End(); } + Physics::DebugDraw(); + } + + VOID DebugDraw::DrawLine(IN CONST glm::vec2 &from, IN CONST glm::vec2 &to, IN CONST glm::vec4 &color, + IN FLOAT32 thickness) + { const auto drawList = ImGui::GetForegroundDrawList(); - for (const auto &t : g_lineShapes) - { - if (!t.Visibility) - continue; - drawList->AddLine(t.From, t.To, t.Color, t.Thickness); - } - for (const auto &t : g_rectShapes) - { - if (!t.Visibility) - continue; - drawList->AddRectFilled(t.TopLeft, t.BottomRight, t.Color); - } + drawList->AddLine(ImVec2(from.x, from.y), ImVec2(to.x, to.y), IM_COL32(color.x * 255, color.y * 255, color.z * 255, color.w * 255), thickness); } - Handle DebugDraw::AddLine(IN CONST glm::vec2 &from, IN CONST glm::vec2 &to, IN CONST glm::vec4 &color, - IN FLOAT32 thickness) + VOID DebugDraw::DrawRect(IN CONST glm::vec2 &position, IN CONST glm::vec2 &size, IN CONST glm::vec4 &color, IN FLOAT32 thickness) { - g_lineShapes.pushBack(ShapeLine{.To = ImVec2{to.x, to.y}, - .From = ImVec2{from.x, from.y}, - .Color = IM_COL32(color.x * 255, color.y * 255, color.z * 255, color.w * 255), - .Thickness = thickness}); - return g_lineShapes.size() - 1; + const auto drawList = ImGui::GetForegroundDrawList(); + drawList->AddRect(ImVec2(position.x, position.y), ImVec2(position.x + size.x, position.y + size.y), IM_COL32(color.x * 255, color.y * 255, color.z * 255, color.w * 255), 0.0f, 0, thickness); } - Handle DebugDraw::AddRect(IN CONST glm::vec2 &position, IN CONST glm::vec2 &size, IN CONST glm::vec4 &color) + VOID DebugDraw::DrawRectFilled(IN CONST glm::vec2 &position, IN CONST glm::vec2 &size, IN CONST glm::vec4 &color) { - g_rectShapes.pushBack({ - .TopLeft = - ImVec2{ - position.x, - position.y, - }, - .BottomRight = - ImVec2{ - position.x + size.x, - position.y + size.y, - }, - .Color = IM_COL32(color.x * 255, color.y * 255, color.z * 255, color.w * 255), - }); - return g_rectShapes.size() - 1; + const auto drawList = ImGui::GetForegroundDrawList(); + drawList->AddRectFilled(ImVec2(position.x, position.y), ImVec2(position.x + size.x, position.y + size.y), IM_COL32(color.x * 255, color.y * 255, color.z * 255, color.w * 255)); } Handle DebugDraw::AddUIWindow(IN PCCHAR title, IN CONST glm::vec2 &position, IN CONST glm::vec2 &size, @@ -157,31 +118,11 @@ namespace ia::iae return g_debugUIWindows.size() - 1; } - VOID DebugDraw::SetLineVisibility(IN Handle handle, IN BOOL visible) - { - g_lineShapes[handle].Visibility = visible; - } - - VOID DebugDraw::SetRectVisibility(IN Handle handle, IN BOOL visible) - { - g_rectShapes[handle].Visibility = visible; - } - VOID DebugDraw::SetUIWindowVisibility(IN Handle handle, IN BOOL visible) { g_debugUIWindows[handle].Visibility = visible; } - VOID DebugDraw::ToggleLineVisibility(IN Handle handle) - { - g_lineShapes[handle].Visibility = !g_lineShapes[handle].Visibility; - } - - VOID DebugDraw::ToggleRectVisibility(IN Handle handle) - { - g_rectShapes[handle].Visibility = !g_rectShapes[handle].Visibility; - } - VOID DebugDraw::ToggleUIWindowVisibility(IN Handle handle) { g_debugUIWindows[handle].Visibility = !g_debugUIWindows[handle].Visibility; diff --git a/Src/IAEngine/inc/IAEngine/Physics/Physics.hpp b/Src/IAEngine/inc/IAEngine/Physics/Physics.hpp index 27f86b8..91845ba 100644 --- a/Src/IAEngine/inc/IAEngine/Physics/Physics.hpp +++ b/Src/IAEngine/inc/IAEngine/Physics/Physics.hpp @@ -33,7 +33,9 @@ namespace ia::iae STATIC VOID Terminate(); STATIC VOID Update(); + STATIC VOID DebugDraw(); friend class Engine; + friend class DebugDraw; }; } \ No newline at end of file diff --git a/Src/IAEngine/inc/IAEngine/Rendering/DebugDraw.hpp b/Src/IAEngine/inc/IAEngine/Rendering/DebugDraw.hpp index a3a8654..b06150b 100644 --- a/Src/IAEngine/inc/IAEngine/Rendering/DebugDraw.hpp +++ b/Src/IAEngine/inc/IAEngine/Rendering/DebugDraw.hpp @@ -23,18 +23,17 @@ namespace ia::iae class DebugDraw { public: - STATIC Handle AddLine(IN CONST glm::vec2& from, IN CONST glm::vec2& to, IN CONST glm::vec4& color, IN FLOAT32 thickness = 1.0f); - STATIC Handle AddRect(IN CONST glm::vec2& position, IN CONST glm::vec2& size, IN CONST glm::vec4& color); STATIC Handle AddUIWindow(IN PCCHAR title, IN CONST glm::vec2 &position, IN CONST glm::vec2 &size, - IN std::function contentDrawCallback); + IN std::function contentDrawCallback); - STATIC VOID SetLineVisibility(IN Handle handle, IN BOOL visible); - STATIC VOID SetRectVisibility(IN Handle handle, IN BOOL visible); + STATIC VOID ToggleUIWindowVisibility(IN Handle handle); STATIC VOID SetUIWindowVisibility(IN Handle handle, IN BOOL visible); - STATIC VOID ToggleLineVisibility(IN Handle handle); - STATIC VOID ToggleRectVisibility(IN Handle handle); - STATIC VOID ToggleUIWindowVisibility(IN Handle handle); + public: + STATIC VOID DrawLine(IN CONST glm::vec2 &from, IN CONST glm::vec2 &to, IN CONST glm::vec4 &color, + IN FLOAT32 thickness = 1.0f); + STATIC VOID DrawRect(IN CONST glm::vec2 &position, IN CONST glm::vec2 &size, IN CONST glm::vec4 &color, IN FLOAT32 thickness = 1.0f); + STATIC VOID DrawRectFilled(IN CONST glm::vec2 &position, IN CONST glm::vec2 &size, IN CONST glm::vec4 &color); private: STATIC VOID Initailize();