Remove Box2D

This commit is contained in:
Isuru Samarathunga
2025-09-28 21:53:23 +05:30
parent 2bd9fb91fc
commit 3c5792e975
11 changed files with 3 additions and 482 deletions

View File

@ -2,8 +2,6 @@
#include <IAEngine/Input.hpp> #include <IAEngine/Input.hpp>
#include <IAEngine/Rendering/Camera.hpp> #include <IAEngine/Rendering/Camera.hpp>
#include <IAEngine/Components/PhysicsBody2D.hpp>
#include <IAEngine/Components/BoxCollider2D.hpp>
#include <IAEngine/Components/SpriteRenderer.hpp> #include <IAEngine/Components/SpriteRenderer.hpp>
#include <IACore/File.hpp> #include <IACore/File.hpp>
@ -13,7 +11,6 @@ namespace ia::iae::game
RefPtr<Scene> scene; RefPtr<Scene> scene;
RefPtr<Node> g_player; RefPtr<Node> g_player;
PhysicsBody2DComponent* g_playerPhysicsBody{};
VOID Game::Initialize() VOID Game::Initialize()
{ {
@ -34,18 +31,6 @@ namespace ia::iae::game
}); });
t->BakeAnimations(); t->BakeAnimations();
} }
{
g_playerPhysicsBody = g_player->AddComponent<PhysicsBody2DComponent>();
g_playerPhysicsBody->IsDynamic() = true;
const auto collider = g_player->AddComponent<BoxCollider2DComponent>();
collider->Rect() = {
0,
0,
g_player->DrawnSize().x,
g_player->DrawnSize().y,
};
collider->IsDebugDrawEnabled() = true;
}
g_player->Tags() = NODE_TAG_PLAYER; g_player->Tags() = NODE_TAG_PLAYER;
g_player->SetLocalPosition({200, 200}); g_player->SetLocalPosition({200, 200});
@ -63,16 +48,7 @@ namespace ia::iae::game
}); });
t->BakeAnimations(); t->BakeAnimations();
} }
{
obstacle->AddComponent<PhysicsBody2DComponent>();
const auto collider = obstacle->AddComponent<BoxCollider2DComponent>();
collider->Rect() = {
0, 0,
obstacle->DrawnSize().x,
obstacle->DrawnSize().y
};
collider->IsDebugDrawEnabled() = true;
}
obstacle->Tags() = NODE_TAG_GROUND; obstacle->Tags() = NODE_TAG_GROUND;
obstacle->SetLocalSortIndex(20); obstacle->SetLocalSortIndex(20);
obstacle->SetLocalPosition({200, 400}); obstacle->SetLocalPosition({200, 400});
@ -89,8 +65,5 @@ namespace ia::iae::game
VOID Game::Update() VOID Game::Update()
{ {
if(Input::WasKeyPressed(Input::KEY_SPACE))
g_playerPhysicsBody->Jump(200.0f);
g_playerPhysicsBody->SetVelocityX(Input::GetDirectionalInput().x * 100.0f);
} }
} }

View File

@ -25,11 +25,9 @@ set(IAEngine_Sources
imp/cpp/Nodes/Node.cpp imp/cpp/Nodes/Node.cpp
imp/cpp/Components/AtlasRenderer.cpp imp/cpp/Components/AtlasRenderer.cpp
imp/cpp/Components/BoxCollider2D.cpp
imp/cpp/Components/SpriteRenderer.cpp imp/cpp/Components/SpriteRenderer.cpp
imp/cpp/Components/SoundEmitter.cpp imp/cpp/Components/SoundEmitter.cpp
imp/cpp/Components/ParticleEmitter.cpp imp/cpp/Components/ParticleEmitter.cpp
imp/cpp/Components/PhysicsBody2D.cpp
imp/cpp/Components/TextureRenderer.cpp imp/cpp/Components/TextureRenderer.cpp
) )
@ -39,4 +37,4 @@ target_include_directories(IAEngine PUBLIC inc/)
target_include_directories(IAEngine PRIVATE imp/hpp) target_include_directories(IAEngine PRIVATE imp/hpp)
target_link_libraries(IAEngine PUBLIC IACore ImGui glm::glm) target_link_libraries(IAEngine PUBLIC IACore ImGui glm::glm)
target_link_libraries(IAEngine PRIVATE SDL3::SDL3 SDL3_mixer::SDL3_mixer box2d RmlUi::RmlUi) target_link_libraries(IAEngine PRIVATE SDL3::SDL3 SDL3_mixer::SDL3_mixer RmlUi::RmlUi)

View File

@ -1,62 +0,0 @@
// 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/BoxCollider2D.hpp>
#include <IAEngine/Components/PhysicsBody2D.hpp>
#include <IAEngine/Components/TextureRenderer.hpp>
#include <IAEngine/Rendering/Renderer.hpp>
#include <IAEngine/Physics/Physics.hpp>
#include <IAEngine/Nodes/Node.hpp>
namespace ia::iae
{
EXTERN Texture g_whiteStrokeTexture;
BoxCollider2DComponent::BoxCollider2DComponent(IN Node *node) : IComponent(node)
{
IA_RELEASE_ASSERT(m_body = node->GetComponent<PhysicsBody2DComponent>());
m_physicsHandle = Physics::AddColliderToBody(m_body->PhysicsHandle(), this);
}
VOID BoxCollider2DComponent::Draw()
{
if (!m_isDebugDrawEnabled)
return;
Renderer::Draw(Renderer::GetMesh_Quad(), g_whiteStrokeTexture.GetHandle(), m_node->GetPosition() + glm::vec2{m_rect.x, m_rect.y},
glm::vec2{m_rect.z, m_rect.w}, 0, Renderer::MAX_LAYER_INDEX,
0, {1.0f, 1.0f, 1.0f, 1.0f});
}
VOID BoxCollider2DComponent::Update()
{
}
VOID BoxCollider2DComponent::OnCollisionEnter(IN Node *other)
{
if (other->HasTag(NODE_TAG_GROUND))
m_body->IsGrounded() = true;
if(m_collisionEnterCallback)
m_collisionEnterCallback(other);
}
VOID BoxCollider2DComponent::OnCollisionExit(IN Node *other)
{
if (other->HasTag(NODE_TAG_GROUND))
m_body->IsGrounded() = false;
if(m_collisionExitCallback)
m_collisionExitCallback(other);
}
} // namespace ia::iae

View File

@ -1,66 +0,0 @@
// 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/BoxCollider2D.hpp>
#include <IAEngine/Components/PhysicsBody2D.hpp>
#include <IAEngine/Nodes/Node.hpp>
#include <IAEngine/Physics/Physics.hpp>
namespace ia::iae
{
PhysicsBody2DComponent::PhysicsBody2DComponent(IN Node *node) : IComponent(node)
{
m_physicsHandle = Physics::AddBody(this);
}
VOID PhysicsBody2DComponent::Draw()
{
}
VOID PhysicsBody2DComponent::Update()
{
if (m_isDynamic)
m_node->SetLocalPosition(Physics::GetBodyPosition(m_physicsHandle));
if (!m_isRotationLocked)
m_node->SetLocalRotation(Physics::GetBodyRotation(m_physicsHandle));
}
VOID PhysicsBody2DComponent::SetVelocity(IN glm::vec2 v)
{
Physics::SetBodyVelocity(m_physicsHandle, v);
}
VOID PhysicsBody2DComponent::SetVelocityX(IN FLOAT32 v)
{
Physics::SetBodyVelocityX(m_physicsHandle, v);
}
VOID PhysicsBody2DComponent::SetVelocityY(IN FLOAT32 v)
{
Physics::SetBodyVelocityY(m_physicsHandle, v);
}
VOID PhysicsBody2DComponent::ApplyForce(IN glm::vec2 force)
{
Physics::ApplyBodyForce(m_physicsHandle, force);
}
VOID PhysicsBody2DComponent::Jump(IN FLOAT32 velocity)
{
if(m_isGrounded)
Physics::SetBodyVelocityY(m_physicsHandle, -velocity);
}
} // namespace ia::iae

View File

@ -19,171 +19,19 @@
#include <IAEngine/Nodes/Node.hpp> #include <IAEngine/Nodes/Node.hpp>
#include <box2d/box2d.h>
#include <map> #include <map>
namespace ia::iae namespace ia::iae
{ {
struct Body
{
struct Collider
{
b2ShapeId ShapeId{};
BoxCollider2DComponent *ColliderComponent{};
};
b2BodyId BodyId{.world0 = 0xFFFF};
PhysicsBody2DComponent *BodyComponent;
Vector<Collider> Colliders;
};
b2WorldId g_worldId{};
Vector<Body> g_bodies;
std::map<Handle, BoxCollider2DComponent*> g_shapeColliders;
INLINE Handle ShapeIdToHandle(IN b2ShapeId id)
{
return *reinterpret_cast<Handle*>(&id.index1);
}
VOID Physics::Initialize() VOID Physics::Initialize()
{ {
auto worldDef = b2DefaultWorldDef();
worldDef.gravity = b2Vec2{0.0f, 1000.0f};
g_worldId = b2CreateWorld(&worldDef);
} }
VOID Physics::Terminate() VOID Physics::Terminate()
{ {
b2DestroyWorld(g_worldId);
} }
VOID Physics::Update() VOID Physics::Update()
{ {
CONSTEXPR FLOAT32 TIME_STEP = 1.0f / 60.0f;
CONSTEXPR INT32 SUB_STEP_COUNT = 4;
b2World_Step(g_worldId, TIME_STEP, SUB_STEP_COUNT);
// Process contact events
const auto contactEvents = b2World_GetContactEvents(g_worldId);
for(INT32 i = 0; i < contactEvents.beginCount; i++)
{
const auto& shapeA = g_shapeColliders[ShapeIdToHandle(contactEvents.beginEvents[i].shapeIdA)];
const auto& shapeB = g_shapeColliders[ShapeIdToHandle(contactEvents.beginEvents[i].shapeIdB)];
shapeA->OnCollisionEnter(shapeB->GetNode());
shapeB->OnCollisionEnter(shapeA->GetNode());
}
for(INT32 i = 0; i < contactEvents.endCount; i++)
{
const auto& shapeA = g_shapeColliders[ShapeIdToHandle(contactEvents.endEvents[i].shapeIdA)];
const auto& shapeB = g_shapeColliders[ShapeIdToHandle(contactEvents.endEvents[i].shapeIdB)];
shapeA->OnCollisionExit(shapeB->GetNode());
shapeB->OnCollisionExit(shapeA->GetNode());
}
// Process sensor events
const auto sensorEvents = b2World_GetSensorEvents(g_worldId);
for(INT32 i = 0; i < sensorEvents.beginCount; i++)
{
const auto& shapeA = g_shapeColliders[ShapeIdToHandle(sensorEvents.beginEvents[i].sensorShapeId)];
const auto& shapeB = g_shapeColliders[ShapeIdToHandle(sensorEvents.beginEvents[i].visitorShapeId)];
shapeA->OnCollisionEnter(shapeB->GetNode());
shapeB->OnCollisionEnter(shapeA->GetNode());
}
for(INT32 i = 0; i < sensorEvents.endCount; i++)
{
const auto& shapeA = g_shapeColliders[ShapeIdToHandle(sensorEvents.endEvents[i].sensorShapeId)];
const auto& shapeB = g_shapeColliders[ShapeIdToHandle(sensorEvents.endEvents[i].visitorShapeId)];
shapeA->OnCollisionExit(shapeB->GetNode());
shapeB->OnCollisionExit(shapeA->GetNode());
}
}
VOID Physics::Bake()
{
for (auto &b : g_bodies)
{
const auto drawnSize = b.BodyComponent->GetNode()->DrawnSize();
const auto pos = b.BodyComponent->GetNode()->GetPosition();
auto bodyDef = b2DefaultBodyDef();
if (b.BodyComponent->IsDynamic())
bodyDef.type = b2_dynamicBody;
bodyDef.position = b2Vec2{pos.x + drawnSize.x/2.0f, pos.y + drawnSize.y/2.0f};
if (b.BodyId.world0 != 0xFFFF)
b2DestroyBody(b.BodyId);
b.BodyId = b2CreateBody(g_worldId, &bodyDef);
for (auto &c : b.Colliders)
{
const auto rect = c.ColliderComponent->Rect();
const auto halfW = rect.z/2.0f;
const auto halfH = rect.w/2.0f;
const auto box = b2MakeOffsetBox(halfW, halfH, {rect.x, rect.y}, b2MakeRot(0));
auto boxShapeDef = b2DefaultShapeDef();
boxShapeDef.density = 1.0f;
boxShapeDef.isSensor = c.ColliderComponent->IsTrigger();
boxShapeDef.enableContactEvents = c.ColliderComponent->CollisionsEnabled();
boxShapeDef.enableSensorEvents = boxShapeDef.enableContactEvents;
c.ShapeId = b2CreatePolygonShape(b.BodyId, &boxShapeDef, &box);
g_shapeColliders[ShapeIdToHandle(c.ShapeId)] = c.ColliderComponent;
}
}
}
Handle Physics::AddBody(IN PhysicsBody2DComponent *body)
{
g_bodies.pushBack(Body{
.BodyComponent = body,
});
return g_bodies.size() - 1;
}
Handle Physics::AddColliderToBody(IN Handle bodyHandle, IN BoxCollider2DComponent *collider)
{
auto &b = g_bodies[bodyHandle];
b.Colliders.pushBack(Body::Collider{
.ColliderComponent = collider,
});
return b.Colliders.size() - 1;
}
FLOAT32 Physics::GetBodyRotation(IN Handle handle)
{
const auto &b = g_bodies[handle];
return acosf(b2Body_GetRotation(b.BodyId).c);
}
glm::vec2 Physics::GetBodyPosition(IN Handle handle)
{
const auto &b = g_bodies[handle];
const auto drawnSize = b.BodyComponent->GetNode()->DrawnSize();
const auto v = b2Body_GetPosition(b.BodyId);
return {v.x - drawnSize.x/2.0f, v.y - drawnSize.y/2.0f};
}
VOID Physics::ApplyBodyForce(IN Handle handle, IN glm::vec2 force)
{
const auto &b = g_bodies[handle];
b2Body_ApplyForce(b.BodyId, {force.x, force.y}, b2Body_GetLocalCenterOfMass(b.BodyId), true);
}
VOID Physics::SetBodyVelocity(IN Handle handle, IN glm::vec2 v)
{
const auto &b = g_bodies[handle];
b2Body_SetLinearVelocity(b.BodyId, {v.x, v.y});
}
VOID Physics::SetBodyVelocityX(IN Handle handle, IN FLOAT32 v)
{
const auto &b = g_bodies[handle];
b2Body_SetLinearVelocity(b.BodyId, {v, b2Body_GetLinearVelocity(b.BodyId).y});
}
VOID Physics::SetBodyVelocityY(IN Handle handle, IN FLOAT32 v)
{
const auto &b = g_bodies[handle];
b2Body_SetLinearVelocity(b.BodyId, {b2Body_GetLinearVelocity(b.BodyId).x, v});
} }
} // namespace ia::iae } // namespace ia::iae

View File

@ -27,7 +27,6 @@ namespace ia::iae
VOID Scene::OnActivate() VOID Scene::OnActivate()
{ {
Physics::Bake();
} }
VOID Scene::OnDeactivate() VOID Scene::OnDeactivate()

View File

@ -1,85 +0,0 @@
// 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 Physics;
class PhysicsBody2DComponent;
class BoxCollider2DComponent : public IComponent
{
public:
BoxCollider2DComponent(IN Node *node);
VOID OnCollisionEnter(IN Node *other);
VOID OnCollisionExit(IN Node *other);
public:
VOID SetCollisionEnterCallback(IN std::function<VOID(IN Node *)> callback)
{
m_collisionEnterCallback = callback;
}
VOID SetCollisionExitCallback(IN std::function<VOID(IN Node *)> callback)
{
m_collisionExitCallback = callback;
}
public:
BOOL &IsDebugDrawEnabled()
{
return m_isDebugDrawEnabled;
}
BOOL &CollisionsEnabled()
{
return m_collisionsEnabled;
}
BOOL &IsTrigger()
{
return m_isTrigger;
}
glm::vec4 &Rect()
{
return m_rect;
}
Handle PhysicsHandle() CONST
{
return m_physicsHandle;
}
public:
VOID Draw();
VOID Update();
private:
glm::vec4 m_rect{};
BOOL m_isTrigger{false};
BOOL m_isDebugDrawEnabled{false};
BOOL m_collisionsEnabled{true};
Handle m_physicsHandle{INVALID_HANDLE};
PhysicsBody2DComponent *m_body{};
std::function<VOID(IN Node *)> m_collisionEnterCallback{};
std::function<VOID(IN Node *)> m_collisionExitCallback{};
};
} // namespace ia::iae

View File

@ -1,66 +0,0 @@
// 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 PhysicsBody2DComponent : public IComponent
{
public:
PhysicsBody2DComponent(IN Node *node);
VOID SetVelocity(IN glm::vec2 v);
VOID SetVelocityX(IN FLOAT32 v);
VOID SetVelocityY(IN FLOAT32 v);
VOID ApplyForce(IN glm::vec2 force);
VOID Jump(IN FLOAT32 velocity);
public:
BOOL &IsGrounded()
{
return m_isGrounded;
}
BOOL &IsDynamic()
{
return m_isDynamic;
}
BOOL &IsRotationLocked()
{
return m_isRotationLocked;
}
Handle PhysicsHandle() CONST
{
return m_physicsHandle;
}
public:
VOID Draw();
VOID Update();
private:
BOOL m_isDynamic{};
BOOL m_isGrounded{};
BOOL m_isRotationLocked{true};
Handle m_physicsHandle{INVALID_HANDLE};
};
} // namespace ia::iae

View File

@ -16,8 +16,7 @@
#pragma once #pragma once
#include <IAEngine/Components/BoxCollider2D.hpp> #include <IAEngine/Physics/Physics.hpp>
#include <IAEngine/Components/PhysicsBody2D.hpp>
namespace ia::iae namespace ia::iae
{ {
@ -27,18 +26,6 @@ namespace ia::iae
STATIC VOID Initialize(); STATIC VOID Initialize();
STATIC VOID Terminate(); STATIC VOID Terminate();
STATIC VOID Bake();
STATIC VOID Update(); STATIC VOID Update();
STATIC Handle AddBody(IN PhysicsBody2DComponent* body);
STATIC Handle AddColliderToBody(IN Handle bodyHandle, IN BoxCollider2DComponent* collider);
STATIC FLOAT32 GetBodyRotation(IN Handle handle);
STATIC glm::vec2 GetBodyPosition(IN Handle handle);
STATIC VOID SetBodyVelocity(IN Handle handle, IN glm::vec2 v);
STATIC VOID SetBodyVelocityX(IN Handle handle, IN FLOAT32 v);
STATIC VOID SetBodyVelocityY(IN Handle handle, IN FLOAT32 v);
STATIC VOID ApplyBodyForce(IN Handle handle, IN glm::vec2 force);
}; };
} }

View File

@ -54,7 +54,3 @@ add_library(Freetype::Freetype ALIAS freetype)
# ----------------------------------------------- # -----------------------------------------------
add_subdirectory(RmlUI/) add_subdirectory(RmlUI/)
# -----------------------------------------------
# Box2D
# -----------------------------------------------
add_subdirectory(box2d/)

1
Vendor/box2d vendored

Submodule Vendor/box2d deleted from f86d1827eb