From ce342f695f080e158951ee6d6746604b9a518531 Mon Sep 17 00:00:00 2001 From: viktorljung Date: Wed, 9 Sep 2015 20:46:04 +0100 Subject: [PATCH 1/4] Base PhysicsSystem. --- include/Core/Physics/PhysicsSystem.h | 42 ++++++++++++++++++++++++++++ src/game/CMakeLists.txt | 2 +- src/game/Physics/PhysicsSystem.cpp | 34 ++++++++++++++++++++++ 3 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 include/Core/Physics/PhysicsSystem.h create mode 100644 src/game/Physics/PhysicsSystem.cpp diff --git a/include/Core/Physics/PhysicsSystem.h b/include/Core/Physics/PhysicsSystem.h new file mode 100644 index 0000000..512f0f7 --- /dev/null +++ b/include/Core/Physics/PhysicsSystem.h @@ -0,0 +1,42 @@ +#ifndef DAYDREAM_PHYSICSSYSTEM_H +#define DAYDREAM_PHYSICSSYSTEM_H + +#include "Core/System.h" +#include + +namespace dd +{ + +namespace Systems { + + +class PhysicsSystem : public System { + PhysicsSystem(World* world, std::shared_ptr eventBroker) + : System(world, eventBroker) {} + + ~PhysicsSystem(); + + void Initialize(); + + // Called once per system every tick + void Update(double dt); + // Called once for every entity in the world every tick + void UpdateEntity(double dt, EntityID entity, EntityID parent); + + // Called when components are committed to an entity + void OnEntityCommit(EntityID entity); + + // Called when an entity is removed + void OnEntityRemoved(EntityID entity); + + + b2Vec2 m_Gravity; + b2World* m_PhysicsWorld; + float m_TimeStep; + int m_VelocityIterations, m_PositionIterations; +}; + +} +} + +#endif //DAYDREAM_PHYSICSSYSTEM_H diff --git a/src/game/CMakeLists.txt b/src/game/CMakeLists.txt index fc38081..13b3912 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) 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 new file mode 100644 index 0000000..e3bcedc --- /dev/null +++ b/src/game/Physics/PhysicsSystem.cpp @@ -0,0 +1,34 @@ +#include "PrecompiledHeader.h" +#include "Core/Physics/PhysicsSystem.h" + +void dd::Systems::PhysicsSystem::Initialize(){ + m_Gravity = b2Vec2(0.f, 9.82f); + m_PhysicsWorld = new b2World(m_Gravity); + + m_TimeStep = 1.f/60.f; + m_VelocityIterations = 6; + m_PositionIterations = 2; +} + +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::OnEntityCommit(EntityID entity){ + +} + + +void dd::Systems::PhysicsSystem::OnEntityRemoved(EntityID entity){ + +} + +dd::Systems::PhysicsSystem::~PhysicsSystem(){ + delete m_PhysicsWorld; +} + From a98806d5516def3f58e2cf72582a9d0a7d7d58b3 Mon Sep 17 00:00:00 2001 From: viktorljung Date: Thu, 10 Sep 2015 16:27:14 +0100 Subject: [PATCH 2/4] Make box on entity commit --- include/Core/CBoxShape.h | 21 ++++++++ include/Core/Engine.h | 79 ++++++++++++++++++---------- include/Core/Physics/CPhysics.h | 20 +++++++ include/Core/Physics/PhysicsSystem.h | 35 ++++++++---- src/game/CMakeLists.txt | 2 +- src/game/Physics/PhysicsSystem.cpp | 75 +++++++++++++++++++++++--- 6 files changed, 186 insertions(+), 46 deletions(-) create mode 100644 include/Core/CBoxShape.h create mode 100644 include/Core/Physics/CPhysics.h 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)); } + + From 0983d76444eccdb5d765f417c32a87f2e3aa2697 Mon Sep 17 00:00:00 2001 From: viktorljung Date: Thu, 10 Sep 2015 18:12:15 +0100 Subject: [PATCH 3/4] Working rotations and positions --- include/Core/CBoxShape.h | 11 +++---- include/Core/Engine.h | 25 ++++++++++++--- include/Core/Physics/PhysicsSystem.h | 3 +- src/game/Physics/PhysicsSystem.cpp | 46 +++++++++++++++++++++++----- 4 files changed, 66 insertions(+), 19 deletions(-) diff --git a/include/Core/CBoxShape.h b/include/Core/CBoxShape.h index 430dcef..cc05ca4 100644 --- a/include/Core/CBoxShape.h +++ b/include/Core/CBoxShape.h @@ -1,5 +1,5 @@ -#ifndef DAYDREAM_CBOXSHAPE_H -#define DAYDREAM_CBOXSHAPE_H +#ifndef DAYDREAM_CRECTANGLESHAPE_H +#define DAYDREAM_CRECTANGLESHAPE_H #include "Core/Component.h" @@ -8,14 +8,13 @@ namespace dd namespace Components { - struct BoxShape: public Component + struct RectangleShape : public Component { - float x; - float y; + }; } } -#endif //DAYDREAM_CBOXSHAPE_H +#endif //DAYDREAM_CRECTANGLESHAPE_H diff --git a/include/Core/Engine.h b/include/Core/Engine.h index 24c43ae..ef3d80e 100644 --- a/include/Core/Engine.h +++ b/include/Core/Engine.h @@ -68,7 +68,7 @@ public: m_World->ComponentFactory.Register(); - m_World->ComponentFactory.Register(); + m_World->ComponentFactory.Register(); m_World->ComponentFactory.Register(); m_World->SystemFactory.Register( [this]() { return new Systems::PhysicsSystem(m_World.get(), m_EventBroker); }); @@ -86,9 +86,26 @@ public: std::shared_ptr sprite = m_World->AddComponent(ent); sprite->SpriteFile = "Textures/Core/ErrorTexture.png"; - std::shared_ptr boxShape = m_World->AddComponent(ent); - boxShape->x = 5.f; - boxShape->y = 5.f; + std::shared_ptr boxShape = m_World->AddComponent(ent); + + std::shared_ptr physics = m_World->AddComponent(ent); + physics->Static = false; + + m_World->CommitEntity(ent); + } + + + { + auto ent = m_World->CreateEntity(); + std::shared_ptr transform = m_World->AddComponent(ent); + transform->Position = glm::vec3(0.f, -3.f, -9.f); + transform->Scale = glm::vec3(2.f, 0.5f, 1.f); + transform->Orientation = glm::rotate(transform->Orientation, glm::radians(45.f), glm::vec3(0, 0, -1)); + + std::shared_ptr sprite = m_World->AddComponent(ent); + sprite->SpriteFile = "Textures/Core/ErrorTexture.png"; + + std::shared_ptr boxShape = m_World->AddComponent(ent); std::shared_ptr physics = m_World->AddComponent(ent); physics->Static = true; diff --git a/include/Core/Physics/PhysicsSystem.h b/include/Core/Physics/PhysicsSystem.h index 3919238..c20365b 100644 --- a/include/Core/Physics/PhysicsSystem.h +++ b/include/Core/Physics/PhysicsSystem.h @@ -8,7 +8,8 @@ #include "Core/CBoxShape.h" #include "Core/Physics/CPhysics.h" #include -#include +#include "Core/CTransform.h" +#include "Transform/TransformSystem.h" namespace dd diff --git a/src/game/Physics/PhysicsSystem.cpp b/src/game/Physics/PhysicsSystem.cpp index c50e8b0..0011a27 100644 --- a/src/game/Physics/PhysicsSystem.cpp +++ b/src/game/Physics/PhysicsSystem.cpp @@ -10,7 +10,7 @@ void dd::Systems::PhysicsSystem::RegisterComponents(ComponentFactory* cf) void dd::Systems::PhysicsSystem::Initialize() { - m_Gravity = b2Vec2(0.f, 9.82f); + m_Gravity = b2Vec2(0.f, -9.82f); m_PhysicsWorld = new b2World(m_Gravity); m_TimeStep = 1.f/60.f; @@ -22,6 +22,26 @@ void dd::Systems::PhysicsSystem::Update(double dt) { m_PhysicsWorld->Step(m_TimeStep, m_VelocityIterations, m_PositionIterations); + for(auto i : m_Bodies) + { + EntityID entity = i.first; + b2Body* body = i.second; + + auto transformComponent = m_World->GetComponent(entity); + + + if(m_World->GetEntityParent(entity) == 0) { + b2Vec2 position = body->GetPosition(); + transformComponent->Position.x = position.x; + transformComponent->Position.y = position.y; + + float angle = body->GetAngle(); + + //TODO: CHECK IF THIS IS CORRECT + transformComponent->Orientation = glm::quat(glm::vec3(0, 0, -angle)); + } + // auto absoluteTransform = m_World->GetSystem()-> + } } void dd::Systems::PhysicsSystem::UpdateEntity(double dt, EntityID entity, EntityID parent) @@ -49,16 +69,25 @@ void dd::Systems::PhysicsSystem::OnEntityRemoved(EntityID entity) void dd::Systems::PhysicsSystem::CreateBody(EntityID entity) { auto physicsComponent = m_World->GetComponent(entity); - if(!physicsComponent) + if(!physicsComponent){ + LOG_ERROR("No PhysicsComponent in CreateBody"); return; + } + auto transformComponent = m_World->GetComponent(entity); - if(!transformComponent) + if(!transformComponent) { + LOG_ERROR("No TransformComponent in CreateBody"); return; + } + + auto absoluteTransform = m_World->GetSystem()->AbsoluteTransform(entity); b2BodyDef bodyDef; - bodyDef.position.Set(transformComponent->Position.x, transformComponent->Position.y); - bodyDef.angle = glm::eulerAngles(transformComponent->Orientation).z; //TODO: CHECK IF THIS IS CORRECT + bodyDef.position.Set(absoluteTransform.Position.x, absoluteTransform.Position.y); + + + bodyDef.angle = glm::degrees(-glm::eulerAngles(absoluteTransform.Orientation).z); //TODO: CHECK IF THIS IS CORRECT if(physicsComponent->Static) bodyDef.type = b2_staticBody; @@ -69,9 +98,10 @@ void dd::Systems::PhysicsSystem::CreateBody(EntityID entity) b2PolygonShape pShape; - auto boxComponent = m_World->GetComponent(entity); + auto boxComponent = m_World->GetComponent(entity); if(boxComponent){ - pShape.SetAsBox(boxComponent->x, boxComponent->y); + + pShape.SetAsBox(absoluteTransform.Scale.x/2, absoluteTransform.Scale.y/2); } if(physicsComponent->Static){ @@ -82,7 +112,7 @@ void dd::Systems::PhysicsSystem::CreateBody(EntityID entity) b2FixtureDef fixtureDef; fixtureDef.shape = &pShape; fixtureDef.density = 1.f; - fixtureDef.restitution = 1.f; + fixtureDef.restitution = 0.2f; fixtureDef.friction = 0.3f; body->CreateFixture(&fixtureDef); From 9bf0ca0e46658ede07d4635db5060822552edc93 Mon Sep 17 00:00:00 2001 From: viktorljung Date: Thu, 10 Sep 2015 21:25:34 +0100 Subject: [PATCH 4/4] ContactListener and contact events --- include/Core/Physics/PhysicsSystem.h | 60 ----------------- include/{Core => }/Physics/CPhysics.h | 0 include/Physics/EContact.h | 27 ++++++++ include/Physics/PhysicsSystem.h | 94 +++++++++++++++++++++++++++ src/game/CMakeLists.txt | 2 +- src/game/Physics/PhysicsSystem.cpp | 25 +++++-- 6 files changed, 140 insertions(+), 68 deletions(-) delete mode 100644 include/Core/Physics/PhysicsSystem.h rename include/{Core => }/Physics/CPhysics.h (100%) create mode 100644 include/Physics/EContact.h create mode 100644 include/Physics/PhysicsSystem.h diff --git a/include/Core/Physics/PhysicsSystem.h b/include/Core/Physics/PhysicsSystem.h deleted file mode 100644 index c20365b..0000000 --- a/include/Core/Physics/PhysicsSystem.h +++ /dev/null @@ -1,60 +0,0 @@ -#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 "Core/CTransform.h" -#include "Transform/TransformSystem.h" - - -namespace dd -{ - -namespace Systems -{ - -//KOLLA http://box2d.org/manual.pdf p52 Pre-Solve Event - -class PhysicsSystem : public System -{ -public: - PhysicsSystem(World* world, std::shared_ptr eventBroker) - : System(world, eventBroker) {} - - void RegisterComponents(ComponentFactory* cf) override; - void Initialize() override; - - // Called once per system every tick - void Update(double dt) override; - // Called once for every entity in the world every tick - void UpdateEntity(double dt, EntityID entity, EntityID parent) override; - - // Called when components are committed to an entity - void OnEntityCommit(EntityID entity) override; - - // Called when an entity is removed - 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); -}; - -} -} - -#endif //DAYDREAM_PHYSICSSYSTEM_H diff --git a/include/Core/Physics/CPhysics.h b/include/Physics/CPhysics.h similarity index 100% rename from include/Core/Physics/CPhysics.h rename to include/Physics/CPhysics.h diff --git a/include/Physics/EContact.h b/include/Physics/EContact.h new file mode 100644 index 0000000..b2d5f57 --- /dev/null +++ b/include/Physics/EContact.h @@ -0,0 +1,27 @@ +// +// Created by Viktor on 10/09/2015. +// + +#ifndef Events_ECONTACT_h__ +#define Events_ECONTACT_h__ + + +#include +#include "Core/EventBroker.h" +#include "Core/Entity.h" + +namespace dd +{ + namespace Events + { + struct Contact : Event + { + EntityID Entity1; + EntityID Entity2; + glm::vec2 ContactPoint; + }; + } + +} + +#endif //Events_ECONTACT_h__ diff --git a/include/Physics/PhysicsSystem.h b/include/Physics/PhysicsSystem.h new file mode 100644 index 0000000..add84dc --- /dev/null +++ b/include/Physics/PhysicsSystem.h @@ -0,0 +1,94 @@ +#ifndef DAYDREAM_PHYSICSSYSTEM_H +#define DAYDREAM_PHYSICSSYSTEM_H + +#include + +#include "Core/System.h" +#include "Core/World.h" +#include "Core/CBoxShape.h" +#include "Physics/CPhysics.h" +#include +#include "Core/CTransform.h" +#include "Transform/TransformSystem.h" +#include "Physics/EContact.h" + + +namespace dd +{ + +namespace Systems +{ + +//KOLLA http://box2d.org/manual.pdf p52 Pre-Solve Event + +class PhysicsSystem : public System +{ + friend class ContractListener; + +public: + PhysicsSystem(World* world, std::shared_ptr eventBroker) + : System(world, eventBroker) {} + + ~PhysicsSystem(); + + void RegisterComponents(ComponentFactory* cf) override; + void Initialize() override; + + // Called once per system every tick + void Update(double dt) override; + // Called once for every entity in the world every tick + void UpdateEntity(double dt, EntityID entity, EntityID parent) override; + + // Called when components are committed to an entity + void OnEntityCommit(EntityID entity) override; + + // Called when an entity is removed + void OnEntityRemoved(EntityID entity) override; + +private: + b2Vec2 m_Gravity; + b2World* m_PhysicsWorld; + float m_TimeStep; + int m_VelocityIterations, m_PositionIterations; + + std::unordered_map m_EntitiesToBodies; + std::unordered_map m_BodiesToEntities; + + void CreateBody(EntityID entity); + + + class ContactListener : public b2ContactListener + { + public: + ContactListener(PhysicsSystem* physicsSystem) + : m_PhysicsSystem(physicsSystem) { } + + void BeginContact(b2Contact* contact) + { + b2Vec2 contactPoint = contact->GetManifold()->localPoint; + + Events::Contact e; + e.ContactPoint = glm::vec2(contactPoint.x, contactPoint.y); + e.Entity1 = m_PhysicsSystem->m_BodiesToEntities[contact->GetFixtureA()->GetBody()]; + e.Entity2 = m_PhysicsSystem->m_BodiesToEntities[contact->GetFixtureB()->GetBody()]; + + m_PhysicsSystem->EventBroker->Publish(e); + } + void EndContact(b2Contact* contact) + { /* handle end event */ } + void PreSolve(b2Contact* contact, const b2Manifold* oldManifold) + { /* handle pre-solve event */ } + void PostSolve(b2Contact* contact, const b2ContactImpulse* impulse) + { /* handle post-solve event */ } + + private: + PhysicsSystem* m_PhysicsSystem; + }; + + ContactListener* m_ContactListener; +}; + +} +} + +#endif //DAYDREAM_PHYSICSSYSTEM_H diff --git a/src/game/CMakeLists.txt b/src/game/CMakeLists.txt index 7fde6ae..c1c8611 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 ../../include/Core/CBoxShape.h ../../include/Core/Physics/CPhysics.h) + Physics/PhysicsSystem.cpp ../../include/Physics/PhysicsSystem.h ../../include/Core/CBoxShape.h ../../include/Physics/CPhysics.h ../../include/Physics/EContact.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 0011a27..b9e2421 100644 --- a/src/game/Physics/PhysicsSystem.cpp +++ b/src/game/Physics/PhysicsSystem.cpp @@ -1,5 +1,5 @@ #include "PrecompiledHeader.h" -#include "Core/Physics/PhysicsSystem.h" +#include "Physics/PhysicsSystem.h" @@ -10,19 +10,24 @@ void dd::Systems::PhysicsSystem::RegisterComponents(ComponentFactory* cf) void dd::Systems::PhysicsSystem::Initialize() { + m_ContactListener = new ContactListener(this); + m_Gravity = b2Vec2(0.f, -9.82f); m_PhysicsWorld = new b2World(m_Gravity); m_TimeStep = 1.f/60.f; m_VelocityIterations = 6; m_PositionIterations = 2; + + + m_PhysicsWorld->SetContactListener(m_ContactListener); } void dd::Systems::PhysicsSystem::Update(double dt) { m_PhysicsWorld->Step(m_TimeStep, m_VelocityIterations, m_PositionIterations); - for(auto i : m_Bodies) + for(auto i : m_EntitiesToBodies) { EntityID entity = i.first; b2Body* body = i.second; @@ -101,7 +106,7 @@ void dd::Systems::PhysicsSystem::CreateBody(EntityID entity) auto boxComponent = m_World->GetComponent(entity); if(boxComponent){ - pShape.SetAsBox(absoluteTransform.Scale.x/2, absoluteTransform.Scale.y/2); + pShape.SetAsBox(absoluteTransform.Scale.x/4, absoluteTransform.Scale.y/4); //TODO: THIS SUCKS DUDE } if(physicsComponent->Static){ @@ -112,14 +117,20 @@ void dd::Systems::PhysicsSystem::CreateBody(EntityID entity) b2FixtureDef fixtureDef; fixtureDef.shape = &pShape; fixtureDef.density = 1.f; - fixtureDef.restitution = 0.2f; + fixtureDef.restitution = 1.0f; fixtureDef.friction = 0.3f; body->CreateFixture(&fixtureDef); } - m_Bodies.insert(std::make_pair(entity, body)); + m_EntitiesToBodies.insert(std::make_pair(entity, body)); + m_BodiesToEntities.insert(std::make_pair(body, entity)); } - - +dd::Systems::PhysicsSystem::~PhysicsSystem() +{ + if (m_ContactListener != nullptr) { + delete m_ContactListener; + m_ContactListener = nullptr; + } +}