From 0983d76444eccdb5d765f417c32a87f2e3aa2697 Mon Sep 17 00:00:00 2001 From: viktorljung Date: Thu, 10 Sep 2015 18:12:15 +0100 Subject: [PATCH] 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);