From 2856bfac6c2a62807e6c068637e33cae5943b07e Mon Sep 17 00:00:00 2001 From: Isuru Samarathunga Date: Tue, 9 Sep 2025 12:42:15 +0530 Subject: [PATCH] Partial Physics --- .gitmodules | 1 + .vscode/launch.json | 11 +-- .vscode/settings.json | 72 ------------------- .vscode/tasks.json | 26 +++---- Src/IAESandbox/CMakeLists.txt | 1 + Src/IAESandbox/imp/cpp/Game.cpp | 22 +++--- Src/IAESandbox/imp/cpp/Ground.cpp | 49 +++++++++++++ Src/IAESandbox/imp/cpp/Player.cpp | 17 +++++ Src/IAESandbox/imp/hpp/Ground.hpp | 27 +++++++ Src/IAEngine/CMakeLists.txt | 5 +- Src/IAEngine/imp/cpp/Components/Collider.cpp | 0 .../imp/cpp/Components/PhysicsBody.cpp | 6 ++ .../imp/cpp/Components/StackedCollider.cpp | 0 Src/IAEngine/imp/cpp/IAEngine.cpp | 5 ++ Src/IAEngine/imp/cpp/Physics/Physics.cpp | 37 ++++++++++ .../inc/IAEngine/Components/Collider.hpp | 0 .../inc/IAEngine/Components/PhysicsBody.hpp | 24 +++++++ .../IAEngine/Components/StackedCollider.hpp | 0 Src/IAEngine/inc/IAEngine/Physics/Physics.hpp | 37 ++++++++++ Src/IAEngine/inc/IAEngine/ResourceManager.hpp | 3 +- Vendor/CMakeLists.txt | 1 + 21 files changed, 242 insertions(+), 102 deletions(-) delete mode 100644 .vscode/settings.json create mode 100644 Src/IAESandbox/imp/cpp/Ground.cpp create mode 100644 Src/IAESandbox/imp/hpp/Ground.hpp delete mode 100644 Src/IAEngine/imp/cpp/Components/Collider.cpp create mode 100644 Src/IAEngine/imp/cpp/Components/PhysicsBody.cpp delete mode 100644 Src/IAEngine/imp/cpp/Components/StackedCollider.cpp create mode 100644 Src/IAEngine/imp/cpp/Physics/Physics.cpp delete mode 100644 Src/IAEngine/inc/IAEngine/Components/Collider.hpp create mode 100644 Src/IAEngine/inc/IAEngine/Components/PhysicsBody.hpp delete mode 100644 Src/IAEngine/inc/IAEngine/Components/StackedCollider.hpp create mode 100644 Src/IAEngine/inc/IAEngine/Physics/Physics.hpp diff --git a/.gitmodules b/.gitmodules index a0a333a..72f5a9f 100644 --- a/.gitmodules +++ b/.gitmodules @@ -13,3 +13,4 @@ [submodule "Dependencies/IAMath"] path = Dependencies/IAMath url = https://git.iasoft.dev/dev0/IAMath + \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json index dd2b20f..42f9632 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -5,16 +5,17 @@ "version": "0.2.0", "configurations": [ { - "name": "C/C++: debiug", + "name": "(Windows) Launch", "type": "cppvsdbg", "request": "launch", - "program": "${workspaceFolder}/build/bin/IAESandbox.exe", + "program": "${workspaceFolder}/build/bin/Debug/IAESandbox.exe", "args": [], "stopAtEntry": false, - "cwd": "${workspaceFolder}/Src/IAESandbox", + "cwd": "${workspaceFolder}/Src/IAESandbox/Res", "environment": [], - "preLaunchTask": "build", - "console": "integratedTerminal" + "console": "externalTerminal", + "preLaunchTask": "CMake: build" } + ] } \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index 5383c6a..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,72 +0,0 @@ -{ - "files.associations": { - "stdexcept": "cpp", - "chrono": "cpp", - "forward_list": "cpp", - "initializer_list": "cpp", - "list": "cpp", - "vector": "cpp", - "xhash": "cpp", - "xiosbase": "cpp", - "xstring": "cpp", - "xutility": "cpp", - "format": "cpp", - "functional": "cpp", - "filesystem": "cpp", - "unordered_map": "cpp", - "algorithm": "cpp", - "iterator": "cpp", - "xmemory": "cpp", - "cmath": "cpp", - "atomic": "cpp", - "bit": "cpp", - "cctype": "cpp", - "charconv": "cpp", - "clocale": "cpp", - "codecvt": "cpp", - "compare": "cpp", - "concepts": "cpp", - "cstddef": "cpp", - "cstdint": "cpp", - "cstdio": "cpp", - "cstdlib": "cpp", - "cstring": "cpp", - "ctime": "cpp", - "cwchar": "cpp", - "exception": "cpp", - "fstream": "cpp", - "iomanip": "cpp", - "ios": "cpp", - "iosfwd": "cpp", - "istream": "cpp", - "limits": "cpp", - "locale": "cpp", - "memory": "cpp", - "new": "cpp", - "optional": "cpp", - "ratio": "cpp", - "sstream": "cpp", - "stop_token": "cpp", - "streambuf": "cpp", - "string": "cpp", - "system_error": "cpp", - "thread": "cpp", - "tuple": "cpp", - "type_traits": "cpp", - "typeinfo": "cpp", - "utility": "cpp", - "xfacet": "cpp", - "xlocale": "cpp", - "xlocbuf": "cpp", - "xlocinfo": "cpp", - "xlocmes": "cpp", - "xlocmon": "cpp", - "xlocnum": "cpp", - "xloctime": "cpp", - "xtr1common": "cpp", - "iostream": "cpp", - "map": "cpp", - "ostream": "cpp", - "xtree": "cpp" - } -} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 4b7c314..cb1ae40 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -1,19 +1,19 @@ { - "tasks": [ + "version": "2.0.0", + "tasks": [ { - "label": "build", - "type": "shell", - "command": "cmake -S. -B./build -G Ninja -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ && cmake --build build", - "group": { + "type": "cmake", + "label": "CMake: build", + "command": "build", + "targets": [ + "ALL_BUILD" + ], + "group":{ "kind": "build", "isDefault": true - } + }, + "problemMatcher": [], + "detail": "CMake template build task" } - ], - "options": { - "env": { - "INCLUDE": "C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.44.35207/include;C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.44.35207/ATLMFC/include;C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Auxiliary/VS/include;C:/Program Files (x86)/Windows Kits/10/include/10.0.26100.0/ucrt;C:/Program Files (x86)/Windows Kits/10//include/10.0.26100.0//um;C:/Program Files (x86)/Windows Kits/10//include/10.0.26100.0//shared;C:/Program Files (x86)/Windows Kits/10//include/10.0.26100.0//winrt;C:/Program Files (x86)/Windows Kits/10//include/10.0.26100.0//cppwinrt;C:/Program Files (x86)/Windows Kits/NETFXSDK/4.8/include/um", - "LIB": "C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.44.35207/ATLMFC/lib/x64;C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.44.35207/lib/x64;C:/Program Files (x86)/Windows Kits/NETFXSDK/4.8/lib/um/x64;C:/Program Files (x86)/Windows Kits/10/lib/10.0.26100.0/ucrt/x64;C:/Program Files (x86)/Windows Kits/10//lib/10.0.26100.0//um/x64" - } - } + ] } \ No newline at end of file diff --git a/Src/IAESandbox/CMakeLists.txt b/Src/IAESandbox/CMakeLists.txt index cbe933d..05963f4 100644 --- a/Src/IAESandbox/CMakeLists.txt +++ b/Src/IAESandbox/CMakeLists.txt @@ -3,6 +3,7 @@ set(IAESandbox_Sources imp/cpp/Main.cpp imp/cpp/Game.cpp imp/cpp/Map.cpp + imp/cpp/Ground.cpp imp/cpp/Player.cpp ) diff --git a/Src/IAESandbox/imp/cpp/Game.cpp b/Src/IAESandbox/imp/cpp/Game.cpp index 41c115d..81f18a3 100644 --- a/Src/IAESandbox/imp/cpp/Game.cpp +++ b/Src/IAESandbox/imp/cpp/Game.cpp @@ -1,25 +1,29 @@ #include #include -#include +#include + +#include namespace ia::iae::game { RefPtr scene; + ResourceManager* g_resourceManager{}; + VOID Game::Initialize() { - const auto player = MakeRefPtr(m_engine); - player->SetLocalPosition({200, 150, 0}); - - const auto map = MakeRefPtr(m_engine); + g_resourceManager = m_engine->RegisterResourceManager(); scene = m_engine->CreateScene(); - scene->AddNode(map); - scene->AddNode(player); - m_engine->ChangeScene(scene); - + const auto player = MakeRefPtr(m_engine); + player->SetLocalPosition({100.0f, 200.0f, 0.0f}); + scene->AddNode(player); + + const auto ground = MakeRefPtr(m_engine); + ground->SetLocalPosition({50.0f, 500.0f, 0.0f}); + scene->AddNode(ground); } VOID Game::Terminate() diff --git a/Src/IAESandbox/imp/cpp/Ground.cpp b/Src/IAESandbox/imp/cpp/Ground.cpp new file mode 100644 index 0000000..2f56b4f --- /dev/null +++ b/Src/IAESandbox/imp/cpp/Ground.cpp @@ -0,0 +1,49 @@ +#include + +#include + +#include + +namespace ia::iae::game +{ + extern ResourceManager* g_resourceManager; + + Handle m_id2; + + Ground::Ground(IN Engine *engine) : m_engine(engine) + { + m_spriteRenderer = AddComponent(); + } + + VOID Ground::OnAdded(IN Scene *scene) + { + Node::OnAdded(scene); + + iae::SpriteRendererComponent::AnimationKeyFrame keyFrame{}; + const auto d = File::ReadToVector("Graphics/red.png"); + keyFrame.Texture = g_resourceManager->CreateTexture(d.data(), d.size()); + keyFrame.Scale = {3.0f, 0.25f, 1.0f}; + + m_spriteRenderer->AddAnimation({.ShouldLoop = true, .Keys = {keyFrame}}); + m_spriteRenderer->BakeAnimations(); + + m_id2 = Physics::CreateStaticBody({GetPosition().X + 300, GetPosition().Y + 25}); + Physics::AddBoxCollider(m_id2, {600.0f, 50.0f}); + } + + VOID Ground::OnRemoved() + { + + Node::OnRemoved(); + } + + VOID Ground::Draw() + { + Node::Draw(); + } + + VOID Ground::Update() + { + Node::Update(); + } +} // namespace ia::iae::game \ No newline at end of file diff --git a/Src/IAESandbox/imp/cpp/Player.cpp b/Src/IAESandbox/imp/cpp/Player.cpp index e157bc9..986df2e 100644 --- a/Src/IAESandbox/imp/cpp/Player.cpp +++ b/Src/IAESandbox/imp/cpp/Player.cpp @@ -1,11 +1,16 @@ #include +#include #include #include namespace ia::iae::game { + extern ResourceManager* g_resourceManager; + + Handle m_id; + Player::Player(IN Engine *engine) : m_engine(engine) { m_spriteRenderer = AddComponent(); @@ -14,6 +19,17 @@ namespace ia::iae::game VOID Player::OnAdded(IN Scene *scene) { Node::OnAdded(scene); + + iae::SpriteRendererComponent::AnimationKeyFrame keyFrame{}; + const auto d = File::ReadToVector("Graphics/green.png"); + keyFrame.Texture = g_resourceManager->CreateTexture(d.data(), d.size()); + keyFrame.Scale = {0.2f, 0.2f, 1.0f}; + + m_spriteRenderer->AddAnimation({.ShouldLoop = true, .Keys = {keyFrame}}); + m_spriteRenderer->BakeAnimations(); + + m_id = Physics::CreateDynamicBody({GetPosition().X + 20, GetPosition().Y + 20}); + Physics::AddBoxCollider(m_id, {40.0f, 40.0f}); } VOID Player::OnRemoved() @@ -30,5 +46,6 @@ namespace ia::iae::game VOID Player::Update() { Node::Update(); + SetLocalPosition(Physics::GetBodyPosition(m_id) - iam::Vec3f{20.0f, 20.0f, 0.0f}); } } // namespace ia::iae::game \ No newline at end of file diff --git a/Src/IAESandbox/imp/hpp/Ground.hpp b/Src/IAESandbox/imp/hpp/Ground.hpp new file mode 100644 index 0000000..e23423d --- /dev/null +++ b/Src/IAESandbox/imp/hpp/Ground.hpp @@ -0,0 +1,27 @@ +#pragma once + +#include +#include +#include + +namespace ia::iae::game +{ + class Ground : public Node + { + public: + Ground(IN Engine *engine); + + VIRTUAL VOID OnAdded(IN Scene *scene) OVERRIDE; + VIRTUAL VOID OnRemoved() OVERRIDE; + VIRTUAL VOID Draw() OVERRIDE; + VIRTUAL VOID Update() OVERRIDE; + + private: + INT32 m_speed{}; + UINT8 m_direction{}; + + private: + Engine *CONST m_engine; + RefPtr m_spriteRenderer; + }; +} // namespace ia::iae::game \ No newline at end of file diff --git a/Src/IAEngine/CMakeLists.txt b/Src/IAEngine/CMakeLists.txt index 016e617..4d80b83 100644 --- a/Src/IAEngine/CMakeLists.txt +++ b/Src/IAEngine/CMakeLists.txt @@ -10,6 +10,8 @@ set(IAEngine_Sources imp/cpp/ResourceManager.cpp + imp/cpp/Physics/Physics.cpp + imp/cpp/Events/Event.cpp imp/cpp/Nodes/Transform.cpp @@ -18,9 +20,8 @@ set(IAEngine_Sources imp/cpp/Components/AtlasRenderer.cpp imp/cpp/Components/SpriteRenderer.cpp imp/cpp/Components/SoundEmitter.cpp - imp/cpp/Components/Collider.cpp imp/cpp/Components/ParticleEmitter.cpp - imp/cpp/Components/StackedCollider.cpp + imp/cpp/Components/PhysicsBody.cpp ) add_library(IAEngine STATIC ${IAEngine_Sources}) diff --git a/Src/IAEngine/imp/cpp/Components/Collider.cpp b/Src/IAEngine/imp/cpp/Components/Collider.cpp deleted file mode 100644 index e69de29..0000000 diff --git a/Src/IAEngine/imp/cpp/Components/PhysicsBody.cpp b/Src/IAEngine/imp/cpp/Components/PhysicsBody.cpp new file mode 100644 index 0000000..11b2cfc --- /dev/null +++ b/Src/IAEngine/imp/cpp/Components/PhysicsBody.cpp @@ -0,0 +1,6 @@ +#include + +namespace ia::iae +{ + +} \ No newline at end of file diff --git a/Src/IAEngine/imp/cpp/Components/StackedCollider.cpp b/Src/IAEngine/imp/cpp/Components/StackedCollider.cpp deleted file mode 100644 index e69de29..0000000 diff --git a/Src/IAEngine/imp/cpp/IAEngine.cpp b/Src/IAEngine/imp/cpp/IAEngine.cpp index 8fa1cdd..1119621 100644 --- a/Src/IAEngine/imp/cpp/IAEngine.cpp +++ b/Src/IAEngine/imp/cpp/IAEngine.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include @@ -77,6 +78,7 @@ namespace ia::iae Random::Initialize(); Input::Initialize(); Audio::Initialize(); + Physics::Initialize(); return true; } @@ -85,6 +87,7 @@ namespace ia::iae { IAE_LOG_INFO("Shutting down IAEngine"); + Physics::Terminate(); Audio::Terminate(); ImGui_ImplSDLRenderer3_Shutdown(); @@ -145,6 +148,8 @@ namespace ia::iae VOID Engine::UpdateGame() { + Physics::Update(); + if B_LIKELY (m_activeScene) m_activeScene->Update(); } diff --git a/Src/IAEngine/imp/cpp/Physics/Physics.cpp b/Src/IAEngine/imp/cpp/Physics/Physics.cpp new file mode 100644 index 0000000..04813d7 --- /dev/null +++ b/Src/IAEngine/imp/cpp/Physics/Physics.cpp @@ -0,0 +1,37 @@ +#include + +namespace ia::iae +{ + + + VOID Physics::Initialize() + { + } + + VOID Physics::Terminate() + { + } + + VOID Physics::Update() + { + } + + Handle Physics::CreateStaticBody(IN iam::Vec3f position) + { + return INVALID_HANDLE; + } + + Handle Physics::CreateDynamicBody(IN iam::Vec3f position) + { + return INVALID_HANDLE; + } + + VOID Physics::AddBoxCollider(IN Handle body, IN iam::Vec3f size) + { + } + + iam::Vec3f Physics::GetBodyPosition(IN Handle body) + { + return {}; + } +} // namespace ia::iae \ No newline at end of file diff --git a/Src/IAEngine/inc/IAEngine/Components/Collider.hpp b/Src/IAEngine/inc/IAEngine/Components/Collider.hpp deleted file mode 100644 index e69de29..0000000 diff --git a/Src/IAEngine/inc/IAEngine/Components/PhysicsBody.hpp b/Src/IAEngine/inc/IAEngine/Components/PhysicsBody.hpp new file mode 100644 index 0000000..c817ef4 --- /dev/null +++ b/Src/IAEngine/inc/IAEngine/Components/PhysicsBody.hpp @@ -0,0 +1,24 @@ +// 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 +{ + +} \ No newline at end of file diff --git a/Src/IAEngine/inc/IAEngine/Components/StackedCollider.hpp b/Src/IAEngine/inc/IAEngine/Components/StackedCollider.hpp deleted file mode 100644 index e69de29..0000000 diff --git a/Src/IAEngine/inc/IAEngine/Physics/Physics.hpp b/Src/IAEngine/inc/IAEngine/Physics/Physics.hpp new file mode 100644 index 0000000..aeb50d1 --- /dev/null +++ b/Src/IAEngine/inc/IAEngine/Physics/Physics.hpp @@ -0,0 +1,37 @@ +// 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 Physics + { + public: + STATIC VOID Initialize(); + STATIC VOID Terminate(); + + STATIC VOID Update(); + + STATIC Handle CreateStaticBody(IN iam::Vec3f position); + STATIC Handle CreateDynamicBody(IN iam::Vec3f position); + STATIC VOID AddBoxCollider(IN Handle body, IN iam::Vec3f size); + + STATIC iam::Vec3f GetBodyPosition(IN Handle body); + }; +} \ No newline at end of file diff --git a/Src/IAEngine/inc/IAEngine/ResourceManager.hpp b/Src/IAEngine/inc/IAEngine/ResourceManager.hpp index 53cdf51..1cc223d 100644 --- a/Src/IAEngine/inc/IAEngine/ResourceManager.hpp +++ b/Src/IAEngine/inc/IAEngine/ResourceManager.hpp @@ -26,6 +26,8 @@ namespace ia::iae class ResourceManager { public: + ResourceManager(IN Engine *engine); + RefPtr CreateTexture(IN CONST Span &encodedData); RefPtr CreateTexture(IN PCUINT8 encodedData, IN SIZE_T encodedDataSize); RefPtr CreateTexture(IN PCUINT8 rgbaData, IN INT32 width, IN INT32 height); @@ -44,7 +46,6 @@ namespace ia::iae protected: Engine *CONST m_engine; - ResourceManager(IN Engine *engine); friend class Engine; }; } // namespace ia::iae \ No newline at end of file diff --git a/Vendor/CMakeLists.txt b/Vendor/CMakeLists.txt index 5f7d41a..9e69248 100644 --- a/Vendor/CMakeLists.txt +++ b/Vendor/CMakeLists.txt @@ -36,3 +36,4 @@ target_include_directories( "imgui/" "imgui/backends" ) +