Files
IAEngine/Src/IAEngine/imp/cpp/Physics/Physics.cpp
Isuru Samarathunga 9038fa54b0 Final Collisions
2025-09-29 15:52:27 +05:30

137 lines
3.7 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 pos = comp->GetNode()->GetPosition() + movement + collider.Position;
for (const auto &t : g_physicsComponents)
{
if (t == comp)
continue;
for (const auto &tc : t->Colliders())
{
const auto tPos = t->GetNode()->GetPosition() + tc.Position;
const auto xColliding = ((pos.x + collider.Size.x) >= tPos.x) && ((tPos.x + tc.Size.x) >= pos.x);
const auto yColliding = ((pos.y + collider.Size.y) >= tPos.y) && ((tPos.y + tc.Size.y) >= pos.y);
if (xColliding && yColliding)
{
// Collision callback
comp->OnCollision(t);
t->OnCollision(comp);
// Overlap block
if (tc.IsTrigger)
continue;
return false;
}
}
}
return true;
}
} // namespace ia::iae