Physics Debug Draw
This commit is contained in:
@ -35,6 +35,7 @@ namespace ia::iae::game
|
|||||||
t->BakeAnimations();
|
t->BakeAnimations();
|
||||||
}
|
}
|
||||||
g_playerPhysics = g_player->AddComponent<PhysicsComponent>();
|
g_playerPhysics = g_player->AddComponent<PhysicsComponent>();
|
||||||
|
g_playerPhysics->IsDynamic() = true;
|
||||||
g_playerPhysics->AddCollider({
|
g_playerPhysics->AddCollider({
|
||||||
.IsTrigger = false,
|
.IsTrigger = false,
|
||||||
.Position = {
|
.Position = {
|
||||||
|
|||||||
@ -56,6 +56,7 @@ namespace ia::iae
|
|||||||
|
|
||||||
VOID PhysicsComponent::Move(IN glm::vec2 direction)
|
VOID PhysicsComponent::Move(IN glm::vec2 direction)
|
||||||
{
|
{
|
||||||
|
IA_ASSERT(m_isDynamic);
|
||||||
const auto v = direction * m_movementSpeed;
|
const auto v = direction * m_movementSpeed;
|
||||||
for(const auto& t: m_colliders)
|
for(const auto& t: m_colliders)
|
||||||
{
|
{
|
||||||
|
|||||||
@ -36,6 +36,8 @@ namespace ia::iae
|
|||||||
{
|
{
|
||||||
CONSTEXPR FLOAT32 GAME_UPDATE_INTERVAL = 1000.0f / 60.0f;
|
CONSTEXPR FLOAT32 GAME_UPDATE_INTERVAL = 1000.0f / 60.0f;
|
||||||
|
|
||||||
|
EXTERN BOOL g_physicsDebugDrawEnabled;
|
||||||
|
|
||||||
SDL_Event g_event{};
|
SDL_Event g_event{};
|
||||||
FLOAT32 g_updateTimer{};
|
FLOAT32 g_updateTimer{};
|
||||||
BOOL g_shouldClose{false};
|
BOOL g_shouldClose{false};
|
||||||
@ -132,6 +134,9 @@ namespace ia::iae
|
|||||||
|
|
||||||
VOID Engine::UpdateGame()
|
VOID Engine::UpdateGame()
|
||||||
{
|
{
|
||||||
|
if(Input::WasKeyPressed(Input::KEY_F6))
|
||||||
|
g_physicsDebugDrawEnabled = !g_physicsDebugDrawEnabled;
|
||||||
|
|
||||||
Physics::Update();
|
Physics::Update();
|
||||||
|
|
||||||
if B_LIKELY (g_activeScene)
|
if B_LIKELY (g_activeScene)
|
||||||
|
|||||||
@ -16,6 +16,7 @@
|
|||||||
|
|
||||||
#include <IAEngine/IAEngine.hpp>
|
#include <IAEngine/IAEngine.hpp>
|
||||||
#include <IAEngine/Physics/Physics.hpp>
|
#include <IAEngine/Physics/Physics.hpp>
|
||||||
|
#include <IAEngine/Rendering/DebugDraw.hpp>
|
||||||
|
|
||||||
#include <IAEngine/Nodes/Node.hpp>
|
#include <IAEngine/Nodes/Node.hpp>
|
||||||
|
|
||||||
@ -23,6 +24,8 @@
|
|||||||
|
|
||||||
namespace ia::iae
|
namespace ia::iae
|
||||||
{
|
{
|
||||||
|
BOOL g_physicsDebugDrawEnabled = false;
|
||||||
|
|
||||||
Vector<PhysicsComponent *> g_physicsComponents;
|
Vector<PhysicsComponent *> g_physicsComponents;
|
||||||
|
|
||||||
FLOAT32 Sq(IN FLOAT32 v)
|
FLOAT32 Sq(IN FLOAT32 v)
|
||||||
@ -45,7 +48,7 @@ namespace ia::iae
|
|||||||
const auto dY = abs(d.y);
|
const auto dY = abs(d.y);
|
||||||
if (d.x >= 0)
|
if (d.x >= 0)
|
||||||
{
|
{
|
||||||
if (dX >= dY)
|
if (dX > dY)
|
||||||
{
|
{
|
||||||
return RectSide::RIGHT;
|
return RectSide::RIGHT;
|
||||||
}
|
}
|
||||||
@ -56,7 +59,7 @@ namespace ia::iae
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (dX >= dY)
|
if (dX > dY)
|
||||||
{
|
{
|
||||||
return RectSide::LEFT;
|
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)
|
Handle Physics::RegisterComponent(IN PhysicsComponent *component)
|
||||||
{
|
{
|
||||||
g_physicsComponents.pushBack(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 tMiddle = t->GetNode()->GetPosition() + tc.Position + tc.Size / 2.0f;
|
||||||
const auto tHalfSize = tc.Size / 2.0f;
|
const auto tHalfSize = tc.Size / 2.0f;
|
||||||
const auto rectSide = GetRectSide(tMiddle - middle);
|
const auto rectSide = GetRectSide(tMiddle - middle);
|
||||||
const auto distance = Sq(tMiddle.x - middle.x) + Sq(tMiddle.y - middle.y);
|
|
||||||
switch (rectSide)
|
switch (rectSide)
|
||||||
{
|
{
|
||||||
case RectSide::LEFT:
|
case RectSide::LEFT:
|
||||||
case RectSide::RIGHT:
|
case RectSide::RIGHT:
|
||||||
isColliding = (Sq(tHalfSize.x + halfSize.x) >= distance);
|
isColliding = ((tHalfSize.x + halfSize.x) >= abs(tMiddle.x - middle.x));
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case RectSide::TOP:
|
case RectSide::TOP:
|
||||||
case RectSide::BOTTOM:
|
case RectSide::BOTTOM:
|
||||||
isColliding = (Sq(tHalfSize.y + halfSize.y) >= distance);
|
isColliding = ((tHalfSize.y + halfSize.y) >= abs(tMiddle.y - middle.y));
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
|||||||
@ -21,6 +21,7 @@
|
|||||||
#include <IAEngine/Rendering/GPUTexture.hpp>
|
#include <IAEngine/Rendering/GPUTexture.hpp>
|
||||||
#include <IAEngine/Rendering/Pipeline/UnlitMesh.hpp>
|
#include <IAEngine/Rendering/Pipeline/UnlitMesh.hpp>
|
||||||
#include <IAEngine/Rendering/Renderer.hpp>
|
#include <IAEngine/Rendering/Renderer.hpp>
|
||||||
|
#include <IAEngine/Physics/Physics.hpp>
|
||||||
|
|
||||||
#include <SDL3/SDL_gpu.h>
|
#include <SDL3/SDL_gpu.h>
|
||||||
|
|
||||||
@ -34,23 +35,6 @@ namespace ia::iae
|
|||||||
|
|
||||||
ImGuiIO g_imGUIIO{};
|
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
|
struct UIWindow
|
||||||
{
|
{
|
||||||
PCCHAR Title{""};
|
PCCHAR Title{""};
|
||||||
@ -61,8 +45,6 @@ namespace ia::iae
|
|||||||
};
|
};
|
||||||
|
|
||||||
Vector<UIWindow> g_debugUIWindows;
|
Vector<UIWindow> g_debugUIWindows;
|
||||||
Vector<ShapeLine> g_lineShapes;
|
|
||||||
Vector<ShapeRect> g_rectShapes;
|
|
||||||
|
|
||||||
VOID DebugDraw::Initailize()
|
VOID DebugDraw::Initailize()
|
||||||
{
|
{
|
||||||
@ -107,47 +89,26 @@ namespace ia::iae
|
|||||||
w.ContentDrawCallback();
|
w.ContentDrawCallback();
|
||||||
ImGui::End();
|
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();
|
const auto drawList = ImGui::GetForegroundDrawList();
|
||||||
for (const auto &t : g_lineShapes)
|
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);
|
||||||
{
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Handle DebugDraw::AddLine(IN CONST glm::vec2 &from, IN CONST glm::vec2 &to, IN CONST glm::vec4 &color,
|
VOID DebugDraw::DrawRect(IN CONST glm::vec2 &position, IN CONST glm::vec2 &size, IN CONST glm::vec4 &color, IN FLOAT32 thickness)
|
||||||
IN FLOAT32 thickness)
|
|
||||||
{
|
{
|
||||||
g_lineShapes.pushBack(ShapeLine{.To = ImVec2{to.x, to.y},
|
const auto drawList = ImGui::GetForegroundDrawList();
|
||||||
.From = ImVec2{from.x, from.y},
|
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);
|
||||||
.Color = IM_COL32(color.x * 255, color.y * 255, color.z * 255, color.w * 255),
|
|
||||||
.Thickness = thickness});
|
|
||||||
return g_lineShapes.size() - 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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({
|
const auto drawList = ImGui::GetForegroundDrawList();
|
||||||
.TopLeft =
|
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));
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Handle DebugDraw::AddUIWindow(IN PCCHAR title, IN CONST glm::vec2 &position, IN CONST glm::vec2 &size,
|
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;
|
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)
|
VOID DebugDraw::SetUIWindowVisibility(IN Handle handle, IN BOOL visible)
|
||||||
{
|
{
|
||||||
g_debugUIWindows[handle].Visibility = 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)
|
VOID DebugDraw::ToggleUIWindowVisibility(IN Handle handle)
|
||||||
{
|
{
|
||||||
g_debugUIWindows[handle].Visibility = !g_debugUIWindows[handle].Visibility;
|
g_debugUIWindows[handle].Visibility = !g_debugUIWindows[handle].Visibility;
|
||||||
|
|||||||
@ -33,7 +33,9 @@ namespace ia::iae
|
|||||||
STATIC VOID Terminate();
|
STATIC VOID Terminate();
|
||||||
|
|
||||||
STATIC VOID Update();
|
STATIC VOID Update();
|
||||||
|
STATIC VOID DebugDraw();
|
||||||
|
|
||||||
friend class Engine;
|
friend class Engine;
|
||||||
|
friend class DebugDraw;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -23,18 +23,17 @@ namespace ia::iae
|
|||||||
class DebugDraw
|
class DebugDraw
|
||||||
{
|
{
|
||||||
public:
|
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,
|
STATIC Handle AddUIWindow(IN PCCHAR title, IN CONST glm::vec2 &position, IN CONST glm::vec2 &size,
|
||||||
IN std::function<VOID()> contentDrawCallback);
|
IN std::function<VOID()> contentDrawCallback);
|
||||||
|
|
||||||
STATIC VOID SetLineVisibility(IN Handle handle, IN BOOL visible);
|
STATIC VOID ToggleUIWindowVisibility(IN Handle handle);
|
||||||
STATIC VOID SetRectVisibility(IN Handle handle, IN BOOL visible);
|
|
||||||
STATIC VOID SetUIWindowVisibility(IN Handle handle, IN BOOL visible);
|
STATIC VOID SetUIWindowVisibility(IN Handle handle, IN BOOL visible);
|
||||||
|
|
||||||
STATIC VOID ToggleLineVisibility(IN Handle handle);
|
public:
|
||||||
STATIC VOID ToggleRectVisibility(IN Handle handle);
|
STATIC VOID DrawLine(IN CONST glm::vec2 &from, IN CONST glm::vec2 &to, IN CONST glm::vec4 &color,
|
||||||
STATIC VOID ToggleUIWindowVisibility(IN Handle handle);
|
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:
|
private:
|
||||||
STATIC VOID Initailize();
|
STATIC VOID Initailize();
|
||||||
|
|||||||
Reference in New Issue
Block a user