Physics Debug Draw
This commit is contained in:
@ -35,6 +35,7 @@ namespace ia::iae::game
|
||||
t->BakeAnimations();
|
||||
}
|
||||
g_playerPhysics = g_player->AddComponent<PhysicsComponent>();
|
||||
g_playerPhysics->IsDynamic() = true;
|
||||
g_playerPhysics->AddCollider({
|
||||
.IsTrigger = false,
|
||||
.Position = {
|
||||
|
||||
@ -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)
|
||||
{
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -16,6 +16,7 @@
|
||||
|
||||
#include <IAEngine/IAEngine.hpp>
|
||||
#include <IAEngine/Physics/Physics.hpp>
|
||||
#include <IAEngine/Rendering/DebugDraw.hpp>
|
||||
|
||||
#include <IAEngine/Nodes/Node.hpp>
|
||||
|
||||
@ -23,6 +24,8 @@
|
||||
|
||||
namespace ia::iae
|
||||
{
|
||||
BOOL g_physicsDebugDrawEnabled = false;
|
||||
|
||||
Vector<PhysicsComponent *> 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:
|
||||
|
||||
@ -21,6 +21,7 @@
|
||||
#include <IAEngine/Rendering/GPUTexture.hpp>
|
||||
#include <IAEngine/Rendering/Pipeline/UnlitMesh.hpp>
|
||||
#include <IAEngine/Rendering/Renderer.hpp>
|
||||
#include <IAEngine/Physics/Physics.hpp>
|
||||
|
||||
#include <SDL3/SDL_gpu.h>
|
||||
|
||||
@ -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<UIWindow> g_debugUIWindows;
|
||||
Vector<ShapeLine> g_lineShapes;
|
||||
Vector<ShapeRect> g_rectShapes;
|
||||
|
||||
VOID DebugDraw::Initailize()
|
||||
{
|
||||
@ -107,47 +89,26 @@ namespace ia::iae
|
||||
w.ContentDrawCallback();
|
||||
ImGui::End();
|
||||
}
|
||||
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);
|
||||
}
|
||||
Physics::DebugDraw();
|
||||
}
|
||||
|
||||
Handle DebugDraw::AddLine(IN CONST glm::vec2 &from, IN CONST glm::vec2 &to, IN CONST glm::vec4 &color,
|
||||
VOID DebugDraw::DrawLine(IN CONST glm::vec2 &from, IN CONST glm::vec2 &to, 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->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::AddRect(IN CONST glm::vec2 &position, IN CONST glm::vec2 &size, 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)
|
||||
{
|
||||
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->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);
|
||||
}
|
||||
|
||||
VOID DebugDraw::DrawRectFilled(IN CONST glm::vec2 &position, IN CONST glm::vec2 &size, IN CONST glm::vec4 &color)
|
||||
{
|
||||
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;
|
||||
|
||||
@ -33,7 +33,9 @@ namespace ia::iae
|
||||
STATIC VOID Terminate();
|
||||
|
||||
STATIC VOID Update();
|
||||
STATIC VOID DebugDraw();
|
||||
|
||||
friend class Engine;
|
||||
friend class DebugDraw;
|
||||
};
|
||||
}
|
||||
@ -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<VOID()> 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();
|
||||
|
||||
Reference in New Issue
Block a user