From 5f112f753117400dff0d3ee6a10ac769fa60d528 Mon Sep 17 00:00:00 2001 From: Isuru Samarathunga Date: Mon, 29 Sep 2025 03:36:31 +0530 Subject: [PATCH] Collision 1/2 --- Src/IAESandbox/imp/cpp/Game.cpp | 29 ++++++- Src/IAEngine/CMakeLists.txt | 1 + Src/IAEngine/imp/cpp/Components/Physics.cpp | 75 +++++++++++++++++++ Src/IAEngine/imp/cpp/Physics/Physics.cpp | 25 +++++++ .../inc/IAEngine/Components/Physics.hpp | 75 +++++++++++++++++++ Src/IAEngine/inc/IAEngine/Physics/Physics.hpp | 8 ++ 6 files changed, 210 insertions(+), 3 deletions(-) create mode 100644 Src/IAEngine/imp/cpp/Components/Physics.cpp create mode 100644 Src/IAEngine/inc/IAEngine/Components/Physics.hpp diff --git a/Src/IAESandbox/imp/cpp/Game.cpp b/Src/IAESandbox/imp/cpp/Game.cpp index c36728d..c2528ab 100644 --- a/Src/IAESandbox/imp/cpp/Game.cpp +++ b/Src/IAESandbox/imp/cpp/Game.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include @@ -12,6 +13,7 @@ namespace ia::iae::game RefPtr scene; RefPtr g_player; + PhysicsComponent* g_playerPhysics; VOID Game::Initialize() { @@ -32,8 +34,18 @@ namespace ia::iae::game }); t->BakeAnimations(); } + g_playerPhysics = g_player->AddComponent(); + g_playerPhysics->AddCollider({ + .IsTrigger = false, + .Position = { + 0.0f, 0.0f, + }, + .Size = { + 25.0f, 25.0f + } + }); g_player->Tags() = NODE_TAG_PLAYER; - g_player->SetLocalPosition({200, 200}); + g_player->SetLocalPosition({200, 300}); const auto obstacle = MakeRefPtr(); { @@ -42,14 +54,23 @@ namespace ia::iae::game .ShouldLoop = true, .Keys = { SpriteRendererComponent::AnimationKeyFrame { - .Scale = {3.0f, 0.5f}, + .Scale = {1.0f, 0.5f}, .ColorOverlay = {0.75f, 0.0f, 0.0f, 1.0f} } }, }); t->BakeAnimations(); } - + const auto obstaclePhysics = obstacle->AddComponent(); + obstaclePhysics->AddCollider({ + .IsTrigger = false, + .Position = { + 0.0f, 0.0f, + }, + .Size = { + 100.0f, 50.0f + } + }); obstacle->Tags() = NODE_TAG_GROUND; obstacle->SetLocalSortIndex(20); obstacle->SetLocalPosition({200, 400}); @@ -66,5 +87,7 @@ namespace ia::iae::game VOID Game::Update() { + const auto d = Input::GetDirectionalInput(); + g_playerPhysics->Move(d); } } \ No newline at end of file diff --git a/Src/IAEngine/CMakeLists.txt b/Src/IAEngine/CMakeLists.txt index 60d5379..745a578 100644 --- a/Src/IAEngine/CMakeLists.txt +++ b/Src/IAEngine/CMakeLists.txt @@ -31,6 +31,7 @@ set(IAEngine_Sources imp/cpp/Components/SoundEmitter.cpp imp/cpp/Components/ParticleEmitter.cpp imp/cpp/Components/TextureRenderer.cpp + imp/cpp/Components/Physics.cpp ) add_library(IAEngine STATIC ${IAEngine_Sources}) diff --git a/Src/IAEngine/imp/cpp/Components/Physics.cpp b/Src/IAEngine/imp/cpp/Components/Physics.cpp new file mode 100644 index 0000000..10d3b6c --- /dev/null +++ b/Src/IAEngine/imp/cpp/Components/Physics.cpp @@ -0,0 +1,75 @@ +// 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 . + +#include +#include +#include + +namespace ia::iae +{ + PhysicsComponent::PhysicsComponent(IN Node *node) : IComponent(node) + { + m_physicsHandle = Physics::RegisterComponent(this); + } + + PhysicsComponent::~PhysicsComponent() + { + } + + VOID PhysicsComponent::Draw() + { + } + + VOID PhysicsComponent::Update() + { + } + + Handle PhysicsComponent::CreateCollider() + { + m_colliders.pushBack({}); + return m_colliders.size() - 1; + } + + Handle PhysicsComponent::AddCollider(IN Collider collider) + { + m_colliders.pushBack(collider); + return m_colliders.size() - 1; + } + + PhysicsComponent::Collider &PhysicsComponent::GetCollider(IN Handle handle) + { + return m_colliders[handle]; + } + + VOID PhysicsComponent::Move(IN glm::vec2 direction) + { + const auto v = direction * m_movementSpeed; + for(const auto& t: m_colliders) + { + if(!Physics::CanMove(m_physicsHandle, t, v)) + return; + } + m_node->SetLocalPosition(m_node->GetLocalPosition() + v); + } + + VOID PhysicsComponent::Jump(IN FLOAT32 force) + { + } + + VOID PhysicsComponent::OnCollision(IN PhysicsComponent *other) + { + } +} // namespace ia::iae \ No newline at end of file diff --git a/Src/IAEngine/imp/cpp/Physics/Physics.cpp b/Src/IAEngine/imp/cpp/Physics/Physics.cpp index 08446cf..6b5f14a 100644 --- a/Src/IAEngine/imp/cpp/Physics/Physics.cpp +++ b/Src/IAEngine/imp/cpp/Physics/Physics.cpp @@ -23,6 +23,8 @@ namespace ia::iae { + Vector g_physicsComponents; + VOID Physics::Initialize() { } @@ -34,4 +36,27 @@ namespace ia::iae VOID Physics::Update() { } + + 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; + for (const auto &t : g_physicsComponents) + { + if (t == comp) + continue; + for(const auto& tc: t->Colliders()) + { + const auto tMiddle = t->GetNode()->GetPosition() + tc.Position + tc.Size / 2.0f; + + } + } + return true; + } } // namespace ia::iae \ No newline at end of file diff --git a/Src/IAEngine/inc/IAEngine/Components/Physics.hpp b/Src/IAEngine/inc/IAEngine/Components/Physics.hpp new file mode 100644 index 0000000..d72b723 --- /dev/null +++ b/Src/IAEngine/inc/IAEngine/Components/Physics.hpp @@ -0,0 +1,75 @@ +// 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 . + +#pragma once + +#include + +namespace ia::iae +{ + class PhysicsComponent : public IComponent + { + public: + struct Collider + { + BOOL IsTrigger{false}; + glm::vec2 Position{}; + glm::vec2 Size{}; + }; + + public: + PhysicsComponent(IN Node *node); + ~PhysicsComponent(); + + Handle CreateCollider(); + Handle AddCollider(IN Collider collider); + Collider &GetCollider(IN Handle handle); + + VOID Move(IN glm::vec2 direction); + + VOID Jump(IN FLOAT32 force); + + public: + BOOL &IsDynamic() + { + return m_isDynamic; + } + + FLOAT32 &MovementSpeed() + { + return m_movementSpeed; + } + + Vector &Colliders() + { + return m_colliders; + } + + public: + VOID Draw(); + VOID Update(); + + private: + BOOL m_isDynamic{false}; + FLOAT32 m_movementSpeed{2.0f}; + Vector m_colliders; + Handle m_physicsHandle{INVALID_HANDLE}; + + VOID OnCollision(IN PhysicsComponent *other); + + friend class Physics; + }; +} // namespace ia::iae \ No newline at end of file diff --git a/Src/IAEngine/inc/IAEngine/Physics/Physics.hpp b/Src/IAEngine/inc/IAEngine/Physics/Physics.hpp index 1102d78..27f86b8 100644 --- a/Src/IAEngine/inc/IAEngine/Physics/Physics.hpp +++ b/Src/IAEngine/inc/IAEngine/Physics/Physics.hpp @@ -17,15 +17,23 @@ #pragma once #include +#include namespace ia::iae { class Physics { public: + STATIC Handle RegisterComponent(IN PhysicsComponent* component); + + STATIC BOOL CanMove(IN Handle handle, IN CONST PhysicsComponent::Collider& collider, IN glm::vec2 movement); + + private: STATIC VOID Initialize(); STATIC VOID Terminate(); STATIC VOID Update(); + + friend class Engine; }; } \ No newline at end of file