diff --git a/include/Core/CBoxShape.h b/include/Core/CBoxShape.h new file mode 100644 index 0000000..430dcef --- /dev/null +++ b/include/Core/CBoxShape.h @@ -0,0 +1,21 @@ +#ifndef DAYDREAM_CBOXSHAPE_H +#define DAYDREAM_CBOXSHAPE_H + +#include "Core/Component.h" + +namespace dd +{ +namespace Components +{ + + struct BoxShape: public Component + { + float x; + float y; + }; + +} + +} + +#endif //DAYDREAM_CBOXSHAPE_H diff --git a/include/Core/Engine.h b/include/Core/Engine.h index 8b16650..bfa4038 100644 --- a/include/Core/Engine.h +++ b/include/Core/Engine.h @@ -34,6 +34,11 @@ #include "CTemplate.h" #include "Transform/TransformSystem.h" +#include "Physics/PhysicsSystem.h" +#include "Physics/CPhysics.h" + +#include "CBoxShape.h" + namespace dd { @@ -42,30 +47,46 @@ class Engine public: Engine(int argc, char* argv[]) { -// m_EventBroker = std::make_shared(); + m_EventBroker = std::make_shared(); m_Renderer = std::make_shared(); m_Renderer->SetFullscreen(false); m_Renderer->SetResolution(Rectangle(0, 0, 1280, 720)); m_Renderer->Initialize(); -// m_InputManager = std::make_shared(m_Renderer->Window(), m_EventBroker); -// -// m_World = std::make_shared(m_EventBroker); -// -// //TODO: Move this out of engine.h -// m_World->ComponentFactory.Register(); -// m_World->SystemFactory.Register([this]() { return new Systems::TransformSystem(m_World.get(), m_EventBroker); }); -// m_World->AddSystem(); -// m_World->ComponentFactory.Register(); -// m_World->ComponentFactory.Register(); -// m_World->Initialize(); + m_InputManager = std::make_shared(m_Renderer->Window(), m_EventBroker); + + m_World = std::make_shared(m_EventBroker); + + //TODO: Move this out of engine.h + m_World->ComponentFactory.Register(); + m_World->SystemFactory.Register([this]() { return new Systems::TransformSystem(m_World.get(), m_EventBroker); }); + m_World->AddSystem(); + + m_World->ComponentFactory.Register(); + m_World->ComponentFactory.Register(); + m_World->SystemFactory.Register([this]() { return new Systems::PhysicsSystem(m_World.get(), m_EventBroker); }); + m_World->AddSystem(); + + m_World->ComponentFactory.Register(); + m_World->ComponentFactory.Register(); + m_World->Initialize(); + + auto ent = m_World->CreateEntity(); + std::shared_ptr transform = m_World->AddComponent(ent); + transform->Position = glm::vec3(0.f, 0.f, -10.f); + std::shared_ptr model = m_World->AddComponent(ent); + model->ModelFile = "Models/Core/UnitSphere.obj"; + + std::shared_ptr boxShape = m_World->AddComponent(ent); + boxShape->x = 5.f; + boxShape->y = 5.f; + + std::shared_ptr physics = m_World->AddComponent(ent); + physics->Static = true; + + m_World->CommitEntity(ent); -// auto ent = m_World->CreateEntity(); -// std::shared_ptr transform = m_World->AddComponent(ent); -// transform->Position = glm::vec3(0.f, 0.f, -10.f); -// std::shared_ptr model = m_World->AddComponent(ent); -// model->ModelFile = "Models/Core/UnitSphere.obj"; m_LastTime = glfwGetTime(); @@ -79,26 +100,26 @@ public: double dt = currentTime - m_LastTime; m_LastTime = currentTime; -// ResourceManager::Update(); -// + ResourceManager::Update(); + // // Update input -// m_InputManager->Update(dt); -// -// m_World->Update(dt); + m_InputManager->Update(dt); + + m_World->Update(dt); // // if (glfwGetKey(m_Renderer->Window(), GLFW_KEY_R)) { // ResourceManager::Reload("Shaders/Deferred/3/Fragment.glsl"); // } -// -// //TODO Fill up the renderQueue with models (Temp fix) + + //TODO Fill up the renderQueue with models (Temp fix) // TEMPAddToRenderQueue(); -// -// // Render scene -// //TODO send renderqueue to draw. + + // Render scene + //TODO send renderqueue to draw. m_Renderer->Draw(m_RendererQueue); // Swap event queues -// m_EventBroker->Clear(); + m_EventBroker->Clear(); glfwPollEvents(); } @@ -168,7 +189,7 @@ public: } private: - //std::shared_ptr m_ResourceManager; + std::shared_ptr m_ResourceManager; std::shared_ptr m_EventBroker; std::shared_ptr m_Renderer; RenderQueueCollection m_RendererQueue; diff --git a/include/Core/Physics/CPhysics.h b/include/Core/Physics/CPhysics.h new file mode 100644 index 0000000..d6c3b21 --- /dev/null +++ b/include/Core/Physics/CPhysics.h @@ -0,0 +1,20 @@ +#ifndef DAYDREAM_CPHYSICS_H +#define DAYDREAM_CPHYSICS_H +#include "Core/Component.h" + +namespace dd +{ +namespace Components +{ + + struct Physics: public Component + { + //idk if anything should be here :) + bool Static = true; + }; + +} + +} + +#endif //DAYDREAM_CPHYSICS_H diff --git a/include/Core/Physics/PhysicsSystem.h b/include/Core/Physics/PhysicsSystem.h index 512f0f7..3919238 100644 --- a/include/Core/Physics/PhysicsSystem.h +++ b/include/Core/Physics/PhysicsSystem.h @@ -1,39 +1,56 @@ #ifndef DAYDREAM_PHYSICSSYSTEM_H #define DAYDREAM_PHYSICSSYSTEM_H +#include + #include "Core/System.h" +#include "Core/World.h" +#include "Core/CBoxShape.h" +#include "Core/Physics/CPhysics.h" #include +#include + namespace dd { -namespace Systems { +namespace Systems +{ +//KOLLA http://box2d.org/manual.pdf p52 Pre-Solve Event -class PhysicsSystem : public System { +class PhysicsSystem : public System +{ +public: PhysicsSystem(World* world, std::shared_ptr eventBroker) : System(world, eventBroker) {} - ~PhysicsSystem(); - - void Initialize(); + void RegisterComponents(ComponentFactory* cf) override; + void Initialize() override; // Called once per system every tick - void Update(double dt); + void Update(double dt) override; // Called once for every entity in the world every tick - void UpdateEntity(double dt, EntityID entity, EntityID parent); + void UpdateEntity(double dt, EntityID entity, EntityID parent) override; // Called when components are committed to an entity - void OnEntityCommit(EntityID entity); + void OnEntityCommit(EntityID entity) override; // Called when an entity is removed - void OnEntityRemoved(EntityID entity); + void OnEntityRemoved(EntityID entity) override; + +private: + b2Vec2 m_Gravity; b2World* m_PhysicsWorld; float m_TimeStep; int m_VelocityIterations, m_PositionIterations; + + std::unordered_map m_Bodies; + + void CreateBody(EntityID entity); }; } diff --git a/src/game/CMakeLists.txt b/src/game/CMakeLists.txt index 13b3912..7fde6ae 100755 --- a/src/game/CMakeLists.txt +++ b/src/game/CMakeLists.txt @@ -69,7 +69,7 @@ set(SOURCE_FILES ${SOURCE_FILES_Rendering} ${SOURCE_FILES_Transform} ${SOURCE_FILES_Game} - Physics/PhysicsSystem.cpp ../../include/Core/Physics/PhysicsSystem.h) + Physics/PhysicsSystem.cpp ../../include/Core/Physics/PhysicsSystem.h ../../include/Core/CBoxShape.h ../../include/Core/Physics/CPhysics.h) if(CMAKE_COMPILER_IS_GNUCXX) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread") diff --git a/src/game/Physics/PhysicsSystem.cpp b/src/game/Physics/PhysicsSystem.cpp index e3bcedc..c50e8b0 100644 --- a/src/game/Physics/PhysicsSystem.cpp +++ b/src/game/Physics/PhysicsSystem.cpp @@ -1,7 +1,15 @@ #include "PrecompiledHeader.h" #include "Core/Physics/PhysicsSystem.h" -void dd::Systems::PhysicsSystem::Initialize(){ + + +void dd::Systems::PhysicsSystem::RegisterComponents(ComponentFactory* cf) +{ + +} + +void dd::Systems::PhysicsSystem::Initialize() +{ m_Gravity = b2Vec2(0.f, 9.82f); m_PhysicsWorld = new b2World(m_Gravity); @@ -10,25 +18,78 @@ void dd::Systems::PhysicsSystem::Initialize(){ m_PositionIterations = 2; } -void dd::Systems::PhysicsSystem::Update(double dt){ +void dd::Systems::PhysicsSystem::Update(double dt) +{ m_PhysicsWorld->Step(m_TimeStep, m_VelocityIterations, m_PositionIterations); + } -void dd::Systems::PhysicsSystem::UpdateEntity(double dt, EntityID entity, EntityID parent){ +void dd::Systems::PhysicsSystem::UpdateEntity(double dt, EntityID entity, EntityID parent) +{ } -void dd::Systems::PhysicsSystem::OnEntityCommit(EntityID entity){ +void dd::Systems::PhysicsSystem::OnEntityCommit(EntityID entity) +{ + + auto physicsComponent = m_World->GetComponent(entity); + if(physicsComponent) + CreateBody(entity); } -void dd::Systems::PhysicsSystem::OnEntityRemoved(EntityID entity){ +void dd::Systems::PhysicsSystem::OnEntityRemoved(EntityID entity) +{ } -dd::Systems::PhysicsSystem::~PhysicsSystem(){ - delete m_PhysicsWorld; + +void dd::Systems::PhysicsSystem::CreateBody(EntityID entity) +{ + auto physicsComponent = m_World->GetComponent(entity); + if(!physicsComponent) + return; + + auto transformComponent = m_World->GetComponent(entity); + if(!transformComponent) + return; + + b2BodyDef bodyDef; + bodyDef.position.Set(transformComponent->Position.x, transformComponent->Position.y); + bodyDef.angle = glm::eulerAngles(transformComponent->Orientation).z; //TODO: CHECK IF THIS IS CORRECT + + if(physicsComponent->Static) + bodyDef.type = b2_staticBody; + else + bodyDef.type = b2_dynamicBody; + + b2Body* body = m_PhysicsWorld->CreateBody(&bodyDef); + + b2PolygonShape pShape; + + auto boxComponent = m_World->GetComponent(entity); + if(boxComponent){ + pShape.SetAsBox(boxComponent->x, boxComponent->y); + } + + if(physicsComponent->Static){ + body->CreateFixture(&pShape, 0); //Density kanske ska vara 0 på statiska kroppar + } + else{ + //TODO: FIX THIS SHIT INTO COMPONENTS + b2FixtureDef fixtureDef; + fixtureDef.shape = &pShape; + fixtureDef.density = 1.f; + fixtureDef.restitution = 1.f; + fixtureDef.friction = 0.3f; + body->CreateFixture(&fixtureDef); + + } + + m_Bodies.insert(std::make_pair(entity, body)); } + +