Files
IAEngine/Src/IAEngine/imp/cpp/Physics/Physics.cpp
Isuru Samarathunga 9269306146 Physics Debug Draw
2025-09-29 14:53:06 +05:30

155 lines
4.3 KiB
C++

// IAEngine: 2D Game Engine by IA
// Copyright (C) 2025 IAS (ias@iasoft.dev)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <IAEngine/IAEngine.hpp>
#include <IAEngine/Physics/Physics.hpp>
#include <IAEngine/Rendering/DebugDraw.hpp>
#include <IAEngine/Nodes/Node.hpp>
#include <map>
namespace ia::iae
{
BOOL g_physicsDebugDrawEnabled = false;
Vector<PhysicsComponent *> g_physicsComponents;
FLOAT32 Sq(IN FLOAT32 v)
{
return v * v;
}
enum class RectSide
{
NONE,
LEFT,
RIGHT,
TOP,
BOTTOM,
};
RectSide GetRectSide(IN glm::vec2 d)
{
const auto dX = abs(d.x);
const auto dY = abs(d.y);
if (d.x >= 0)
{
if (dX > dY)
{
return RectSide::RIGHT;
}
else
{
return (d.y < 0) ? RectSide::TOP : RectSide::BOTTOM;
}
}
else
{
if (dX > dY)
{
return RectSide::LEFT;
}
else
{
return (d.y < 0) ? RectSide::TOP : RectSide::BOTTOM;
}
}
return RectSide::NONE;
}
VOID Physics::Initialize()
{
}
VOID Physics::Terminate()
{
}
VOID Physics::Update()
{
}
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);
return g_physicsComponents.size() - 1;
}
BOOL Physics::CanMove(IN Handle handle, IN CONST PhysicsComponent::Collider &collider, IN glm::vec2 movement)
{
const auto comp = g_physicsComponents[handle];
const auto middle = comp->GetNode()->GetPosition() + movement + collider.Position + collider.Size / 2.0f;
const auto halfSize = collider.Size / 2.0f;
for (const auto &t : g_physicsComponents)
{
if (t == comp)
continue;
for (const auto &tc : t->Colliders())
{
BOOL isColliding = false;
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);
switch (rectSide)
{
case RectSide::LEFT:
case RectSide::RIGHT:
isColliding = ((tHalfSize.x + halfSize.x) >= abs(tMiddle.x - middle.x));
break;
case RectSide::TOP:
case RectSide::BOTTOM:
isColliding = ((tHalfSize.y + halfSize.y) >= abs(tMiddle.y - middle.y));
break;
default:
isColliding = false;
break;
}
if (isColliding)
{
// Collision callback
comp->OnCollision(t);
t->OnCollision(comp);
// Overlap block
if (tc.IsTrigger)
continue;
return false;
}
}
}
return true;
}
} // namespace ia::iae