Partial Physics

This commit is contained in:
Isuru Samarathunga
2025-09-09 12:42:15 +05:30
parent 0440bc6f13
commit 2856bfac6c
21 changed files with 242 additions and 102 deletions

View File

@ -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
)

View File

@ -1,25 +1,29 @@
#include <Game.hpp>
#include <Player.hpp>
#include <Map.hpp>
#include <Ground.hpp>
#include <IAEngine/ResourceManager.hpp>
namespace ia::iae::game
{
RefPtr<iae::Scene> scene;
ResourceManager* g_resourceManager{};
VOID Game::Initialize()
{
const auto player = MakeRefPtr<Player>(m_engine);
player->SetLocalPosition({200, 150, 0});
const auto map = MakeRefPtr<TiledMap>(m_engine);
g_resourceManager = m_engine->RegisterResourceManager<ResourceManager>();
scene = m_engine->CreateScene();
scene->AddNode(map);
scene->AddNode(player);
m_engine->ChangeScene(scene);
const auto player = MakeRefPtr<Player>(m_engine);
player->SetLocalPosition({100.0f, 200.0f, 0.0f});
scene->AddNode(player);
const auto ground = MakeRefPtr<Ground>(m_engine);
ground->SetLocalPosition({50.0f, 500.0f, 0.0f});
scene->AddNode(ground);
}
VOID Game::Terminate()

View File

@ -0,0 +1,49 @@
#include <Ground.hpp>
#include <IAEngine/Physics/Physics.hpp>
#include <IACore/File.hpp>
namespace ia::iae::game
{
extern ResourceManager* g_resourceManager;
Handle m_id2;
Ground::Ground(IN Engine *engine) : m_engine(engine)
{
m_spriteRenderer = AddComponent<SpriteRendererComponent>();
}
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

View File

@ -1,11 +1,16 @@
#include <Player.hpp>
#include <IAEngine/Physics/Physics.hpp>
#include <IAEngine/Input.hpp>
#include <IACore/File.hpp>
namespace ia::iae::game
{
extern ResourceManager* g_resourceManager;
Handle m_id;
Player::Player(IN Engine *engine) : m_engine(engine)
{
m_spriteRenderer = AddComponent<SpriteRendererComponent>();
@ -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

View File

@ -0,0 +1,27 @@
#pragma once
#include <IAEngine/Components/SoundEmitter.hpp>
#include <IAEngine/Components/SpriteRenderer.hpp>
#include <IAEngine/IAEngine.hpp>
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<SpriteRendererComponent> m_spriteRenderer;
};
} // namespace ia::iae::game

View File

@ -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})

View File

@ -0,0 +1,6 @@
#include <IAEngine/Components/PhysicsBody.hpp>
namespace ia::iae
{
}

View File

@ -3,6 +3,7 @@
#include <IAEngine/Input.hpp>
#include <IAEngine/Random.hpp>
#include <IAEngine/Time.hpp>
#include <IAEngine/Physics/Physics.hpp>
#include <SDL3/SDL.h>
@ -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();
}

View File

@ -0,0 +1,37 @@
#include <IAEngine/Physics/Physics.hpp>
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

View File

@ -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 <https://www.gnu.org/licenses/>.
#pragma once
#include <IAEngine/Components/Component.hpp>
namespace ia::iae
{
}

View File

@ -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 <https://www.gnu.org/licenses/>.
#pragma once
#include <IAEngine/Base.hpp>
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);
};
}

View File

@ -26,6 +26,8 @@ namespace ia::iae
class ResourceManager
{
public:
ResourceManager(IN Engine *engine);
RefPtr<Texture> CreateTexture(IN CONST Span<CONST UINT8> &encodedData);
RefPtr<Texture> CreateTexture(IN PCUINT8 encodedData, IN SIZE_T encodedDataSize);
RefPtr<Texture> 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