-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
217 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Oops, something went wrong.