Skip to content

Commit

Permalink
box2d integration, yay
Browse files Browse the repository at this point in the history
  • Loading branch information
DanPeled committed May 13, 2024
1 parent ad1004d commit 46d5cb4
Show file tree
Hide file tree
Showing 18 changed files with 217 additions and 17 deletions.
9 changes: 7 additions & 2 deletions MakeFile
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
SOURCES := $(wildcard $(shell find src imgui -type f -name '*.cpp'))
OBJECTS := $(patsubst %.cpp, %.o, $(SOURCES))

INCLUDE := -Ibox2d -Iimgui -Isrc
CPP_VER := -std=c++20
LIBRARIES := -lsfml-graphics -lsfml-window -lsfml-system -lGL -lbox2d
LIB_DIR = -Llib/

all: compile link

compile: $(OBJECTS)

%.o: %.cpp
g++ -c $< -o $@ -Ibox2d -Iimgui -Isrc -std=c++20
g++ -c $< -o $@ $(LIB_DIR) $(INCLUDE) $(CPP_VER)

link: $(OBJECTS)
g++ $^ -o main -lsfml-graphics -lsfml-window -lsfml-system -lGL -Ibox2d -Iimgui -Isrc -std=c++20
g++ $^ -o main $(LIB_DIR) $(INCLUDE) $(LIBRARIES) $(CPP_VER)


clean:
Expand Down
6 changes: 5 additions & 1 deletion imgui.ini
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ Pos=635,26
Size=349,94

[Window][Entities]
Pos=807,3
Pos=810,4
Size=261,703

[Window][Test]
Pos=332,40
Size=406,58

Binary file modified main
Binary file not shown.
37 changes: 37 additions & 0 deletions src/WoopWoop/ECS/Components/Box2D/BoxCollider.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "BoxCollider.hpp"
#include "WoopWoop/Subsystems/Box2DIntegration.hpp"

namespace wpwp
{

void BoxCollider::start()
{
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.position.Set(entity->transform->getPosition()->x, entity->transform->getPosition()->y);
m_body = Box2DIntegration::getWorld()->CreateBody(&bodyDef);

m_shape = new b2PolygonShape();

m_shape->SetAsBox(entity->transform->getScale()->x / 2.0f, entity->transform->getScale()->y / 2.0f);

b2FixtureDef fixtureDef;
fixtureDef.shape = m_shape;
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.3f;

m_fixture = m_body->CreateFixture(&fixtureDef);

entity->transform->onTransformChanged += [&]()
{
m_shape->SetAsBox(entity->transform->getScale()->x / 2.0f, entity->transform->getScale()->y / 2.0f);
bodyDef.position.Set(entity->transform->getPosition()->x, entity->transform->getPosition()->y);
m_body->SetTransform(m_body->GetWorldCenter(), entity->transform->getRotation()->z * (float)(3.14 / 180));
};
}

void BoxCollider::update()
{
entity->transform->setPosition(sf::Vector3f(m_body->GetPosition().x, m_body->GetPosition().y, 0));
}
}
25 changes: 25 additions & 0 deletions src/WoopWoop/ECS/Components/Box2D/BoxCollider.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef BOX_COLLIDER_HPP
#define BOX_COLLIDER_HPP

#include <WoopWoop/WoopWoop.hpp>
#include <WoopWoop/Subsystems/Box2DIntegration.hpp>

namespace wpwp
{
struct BoxCollider : public Component
{
public:
void start() override;
void update() override;

b2Body *getBody() const { return m_body; }
b2Fixture *getFixture() const { return m_fixture; }

private:
b2Body *m_body;
b2Fixture *m_fixture;
b2PolygonShape *m_shape;
};
};

#endif
28 changes: 28 additions & 0 deletions src/WoopWoop/ECS/Components/Transform.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "Transform.hpp"

namespace wpwp
{
sf::Vector3f *Transform::getPosition() { return &m_globalPosition; }

void Transform::setPosition(const sf::Vector3f &position)
{
m_globalPosition = position;
onTransformChanged.invoke();
}

sf::Vector3f *Transform::getRotation() { return &m_rotation; }

void Transform::setRotation(const sf::Vector3f &rotation)
{
m_rotation = rotation;
onTransformChanged.invoke();
}

sf::Vector3f *Transform::getScale() { return &m_scale; }

void Transform::setScale(const sf::Vector3f &scale)
{
m_scale = scale;
onTransformChanged.invoke();
}
}
14 changes: 8 additions & 6 deletions src/WoopWoop/ECS/Components/Transform.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,42 +19,44 @@ namespace wpwp
*
* @return A pointer to the position vector.
*/
sf::Vector3f *getPosition() { return &m_globalPosition; }
sf::Vector3f *getPosition();

/**
* @brief Sets the position of the transform.
*
* @param position The new position vector.
*/
void setPosition(const sf::Vector3f &position) { m_globalPosition = position; }
void setPosition(const sf::Vector3f &position);

/**
* @brief Gets the rotation of the transform.
*
* @return A pointer to the rotation vector.
*/
sf::Vector3f *getRotation() { return &m_rotation; }
sf::Vector3f *getRotation();

/**
* @brief Sets the rotation of the transform.
*
* @param rotation The new rotation vector.
*/
void setRotation(const sf::Vector3f &rotation) { m_rotation = rotation; }
void setRotation(const sf::Vector3f &rotation);

/**
* @brief Gets the scale of the transform.
*
* @return A pointer to the scale vector.
*/
sf::Vector3f *getScale() { return &m_scale; }
sf::Vector3f *getScale();

/**
* @brief Sets the scale of the transform.
*
* @param scale The new scale vector.
*/
void setScale(const sf::Vector3f &scale) { m_scale = scale; }
void setScale(const sf::Vector3f &scale);

Signal<> onTransformChanged;

/**
* @brief Gets the name of the component.
Expand Down
1 change: 1 addition & 0 deletions src/WoopWoop/ECS/Enitiy.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "Entity.hpp"
#include <iostream>
#include "WoopWoop/Util/Util.hpp"
#include "Components/Transform.hpp"

namespace wpwp
{
Expand Down
4 changes: 3 additions & 1 deletion src/WoopWoop/ECS/Entity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
#include <SFML/Graphics.hpp>
#include <vector>
#include "Component.hpp"
#include "Components/Transform.hpp"
#include <memory>
#include <iostream>
#include <unordered_map>
#include "WoopWoop/Util/Signal.hpp"

namespace wpwp
{
struct Transform;

/**
* @brief Class representing an entity in the game.
*/
Expand Down
4 changes: 2 additions & 2 deletions src/WoopWoop/Editor/Editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ namespace wpwp::Editor
ImGui::InputFloat("Y scale", &selectedEntity->transform->getScale()->y);

ImGui::Dummy(ImVec2(0.0f, 20.0f));
float rads = selectedEntity->transform->getScale()->z * 3.14 / 180;
float rads = selectedEntity->transform->getRotation()->z * 3.14 / 180;
ImGui::SliderAngle("Angle", &rads, 0, 360);
selectedEntity->transform->getScale()->z = rads * 180 / 3.14;
selectedEntity->transform->getRotation()->z = rads * 180 / 3.14;

for (auto i : comps)
{
Expand Down
9 changes: 7 additions & 2 deletions src/WoopWoop/Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "WoopWoop.hpp"
#include "Subsystems/ImGuiSub.hpp"
#include "Subsystems/RenderingSub.hpp"
#include "Subsystems/Box2DIntegration.hpp"
#include "Scene.hpp"

namespace wpwp
Expand All @@ -17,8 +18,9 @@ namespace wpwp
Editor::Editor editor;
ImGuiSubsystem imguiSub;
RenderingSub renderingSub;
Box2DIntegration box2d;

Engine::Engine(std::string title) : window(sf::VideoMode(1080, 720), title, sf::Style::Close)
Engine::Engine(std::string title) : window(sf::VideoMode(1080, 720), title)
{
init();

Expand Down Expand Up @@ -60,8 +62,10 @@ namespace wpwp

addSubsystem(input);
addSubsystem(editor);
addSubsystem(imguiSub);
addSubsystem(box2d);
addSubsystem(renderingSub);

addSubsystem(imguiSub);
}

Engine *Engine::getInstance()
Expand All @@ -82,6 +86,7 @@ namespace wpwp
sf::Time elapsedTime = m_clock.restart(); // Restart the clock and get elapsed time
float fps = 1.0f / elapsedTime.asSeconds(); // Calculate FPS
m_fpsText.setString("FPS: " + std::to_string(static_cast<int>(fps))); // Convert FPS to string
Util::m_deltaTime = elapsedTime.asSeconds();

onStartOfFrame.invoke();

Expand Down
61 changes: 61 additions & 0 deletions src/WoopWoop/Subsystems/Box2DIntegration.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include "Box2DIntegration.hpp"
#include "WoopWoop/Util/Util.hpp"
#include "WoopWoop/ECS/Entity.hpp"
#include "WoopWoop/ECS/Components/Transform.hpp"
#include "ImGuiSub.hpp"

namespace wpwp
{
// struct Box // TODO: convert box struct to BoxCollider component
// {
// public:
// Box() = default;
// ~Box() = default;
// void init(b2World *world, Entity *e)
// {
// b2BodyDef bodyDef;
// bodyDef.type = b2_dynamicBody;
// bodyDef.position.Set(e->transform->getPosition()->x, e->transform->getPosition()->y);

// b2Body *body = world->CreateBody(&bodyDef);

// b2PolygonShape boxShape;
// boxShape.SetAsBox(e->transform->getScale()->x / 2.0f, e->transform->getScale()->y / 2.0f);

// b2FixtureDef fixtureDef;
// fixtureDef.shape = &boxShape;
// fixtureDef.density = 1.0f;
// fixtureDef.friction = 0.3f;

// b2Fixture *fixture = body->CreateFixture(&fixtureDef);
// };
// };
std::unique_ptr<b2World> Box2DIntegration::m_World;

void Box2DIntegration::init()
{
b2Vec2 gravity(0, 98.1f);
m_World = std::make_unique<b2World>(gravity);

b2BodyDef groundBodyDef;
groundBodyDef.position.Set(0.0f, 300.f);
b2Body *groundBody = m_World->CreateBody(&groundBodyDef);
b2PolygonShape groundBox;
groundBox.SetAsBox(50.0f, 10.0f);
groundBody->CreateFixture(&groundBox, 0);
}

void Box2DIntegration::update()
{
static float y = 300;
m_World->Step(Util::deltaTime(), 6, 2);
sf::Vertex line[] =
{
sf::Vertex(sf::Vector2f(0, y)),
sf::Vertex(sf::Vector2f(1000, y))};
ImGui::Begin("Test");
ImGui::SliderFloat("Height", &y, 0, 1000);
ImGui::End();
Engine::getInstance()->window.draw(line, 2, sf::Lines);
}
}
22 changes: 22 additions & 0 deletions src/WoopWoop/Subsystems/Box2DIntegration.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef BOX2D_INTEGRATION_HPP
#define BOX2D_INTEGRATION_HPP

#include <WoopWoop/Util/Subsystem.hpp>
#include <memory>
#include "../../../box2d/box2d.h"

namespace wpwp
{
class Box2DIntegration : public Subsystem
{
public:
void init() override;
void update() override;

static b2World *getWorld() { return m_World.get(); }

private:
static std::unique_ptr<b2World> m_World;
};
};
#endif
3 changes: 2 additions & 1 deletion src/WoopWoop/Util/Util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ namespace wpwp
std::mt19937 Util::gen(rd());
std::uniform_int_distribution<> Util::dis(0, 15);
std::uniform_int_distribution<> Util::dis2(8, 11);

float Util::m_deltaTime;

std::string Util::generate_uuid_v4()
{
std::stringstream ss;
Expand Down
5 changes: 5 additions & 0 deletions src/WoopWoop/Util/Util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <random>
#include <sstream>
#include "../Engine.hpp"

namespace wpwp
{
Expand All @@ -18,12 +19,16 @@ namespace wpwp
* @return A string representing the generated UUID.
*/
static std::string generate_uuid_v4();
static float deltaTime() { return m_deltaTime; }

private:
static std::random_device rd; // Random device for generating seeds.
static std::mt19937 gen; // Mersenne Twister random number generator.
static std::uniform_int_distribution<> dis; // Uniform distribution for integers.
static std::uniform_int_distribution<> dis2; // Uniform distribution for integers.
static float m_deltaTime;

friend Engine;
};
} // namespace wpwp

Expand Down
2 changes: 2 additions & 0 deletions src/WoopWoop/WoopWoop.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@
#include "ECS/Components/Graphics/Animation/Animation.hpp"
#include "ECS/Components/Graphics/Animation/Animator.hpp"

#include "ECS/Components/Box2D/BoxCollider.hpp"

#endif
Binary file modified src/demo/components/mouseController.o
Binary file not shown.
Loading

0 comments on commit 46d5cb4

Please sign in to comment.