Collision 1/2
This commit is contained in:
@ -4,6 +4,7 @@
|
||||
#include <IAEngine/Rendering/Camera.hpp>
|
||||
#include <IAEngine/Rendering/DebugDraw.hpp>
|
||||
#include <IAEngine/Components/SpriteRenderer.hpp>
|
||||
#include <IAEngine/Components/Physics.hpp>
|
||||
|
||||
#include <IACore/File.hpp>
|
||||
|
||||
@ -12,6 +13,7 @@ namespace ia::iae::game
|
||||
RefPtr<Scene> scene;
|
||||
|
||||
RefPtr<Node> g_player;
|
||||
PhysicsComponent* g_playerPhysics;
|
||||
|
||||
VOID Game::Initialize()
|
||||
{
|
||||
@ -32,8 +34,18 @@ namespace ia::iae::game
|
||||
});
|
||||
t->BakeAnimations();
|
||||
}
|
||||
g_playerPhysics = g_player->AddComponent<PhysicsComponent>();
|
||||
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<Node>();
|
||||
{
|
||||
@ -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<PhysicsComponent>();
|
||||
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);
|
||||
}
|
||||
}
|
||||
@ -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})
|
||||
|
||||
75
Src/IAEngine/imp/cpp/Components/Physics.cpp
Normal file
75
Src/IAEngine/imp/cpp/Components/Physics.cpp
Normal file
@ -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 <https://www.gnu.org/licenses/>.
|
||||
|
||||
#include <IAEngine/Components/Physics.hpp>
|
||||
#include <IAEngine/Physics/Physics.hpp>
|
||||
#include <IAEngine/Nodes/Node.hpp>
|
||||
|
||||
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
|
||||
@ -23,6 +23,8 @@
|
||||
|
||||
namespace ia::iae
|
||||
{
|
||||
Vector<PhysicsComponent *> 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
|
||||
75
Src/IAEngine/inc/IAEngine/Components/Physics.hpp
Normal file
75
Src/IAEngine/inc/IAEngine/Components/Physics.hpp
Normal file
@ -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 <https://www.gnu.org/licenses/>.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <IAEngine/Components/Component.hpp>
|
||||
|
||||
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<Collider> &Colliders()
|
||||
{
|
||||
return m_colliders;
|
||||
}
|
||||
|
||||
public:
|
||||
VOID Draw();
|
||||
VOID Update();
|
||||
|
||||
private:
|
||||
BOOL m_isDynamic{false};
|
||||
FLOAT32 m_movementSpeed{2.0f};
|
||||
Vector<Collider> m_colliders;
|
||||
Handle m_physicsHandle{INVALID_HANDLE};
|
||||
|
||||
VOID OnCollision(IN PhysicsComponent *other);
|
||||
|
||||
friend class Physics;
|
||||
};
|
||||
} // namespace ia::iae
|
||||
@ -17,15 +17,23 @@
|
||||
#pragma once
|
||||
|
||||
#include <IAEngine/Physics/Physics.hpp>
|
||||
#include <IAEngine/Components/Physics.hpp>
|
||||
|
||||
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;
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user