diff --git a/include/Core/Engine.h b/include/Core/Engine.h index 1df8465..5c560f1 100644 --- a/include/Core/Engine.h +++ b/include/Core/Engine.h @@ -114,6 +114,7 @@ public: std::shared_ptr transform = m_World->AddComponent(ent); transform->Position = glm::vec3(0.5f, 0.f, -10.f); transform->Scale = glm::vec3(1.f, 1.f, 1.f); + transform->Velocity = glm::vec3(0.0f, 0.f, 0.f); std::shared_ptr sprite = m_World->AddComponent(ent); sprite->SpriteFile = "Textures/Ball.png"; diff --git a/include/Physics/PhysicsSystem.h b/include/Physics/PhysicsSystem.h index 57bad34..559d815 100644 --- a/include/Physics/PhysicsSystem.h +++ b/include/Physics/PhysicsSystem.h @@ -65,6 +65,16 @@ private: void CreateBody(EntityID entity); + struct Impulse + { + b2Body* Body; + b2Vec2 Impulse; + b2Vec2 Point; + }; + std::list m_Impulses; + + + class ContactListener : public b2ContactListener { public: diff --git a/src/game/Physics/PhysicsSystem.cpp b/src/game/Physics/PhysicsSystem.cpp index 3d244a7..863ad74 100644 --- a/src/game/Physics/PhysicsSystem.cpp +++ b/src/game/Physics/PhysicsSystem.cpp @@ -37,7 +37,13 @@ bool dd::Systems::PhysicsSystem::SetImpulse(const Events::SetImpulse &event) point.x = event.Point.x; point.y = event.Point.y; - body->ApplyLinearImpulse(impulse, point, true); + + Impulse i; + i.Body = body; + i.Impulse = impulse; + i.Point = point; + + m_Impulses.push_back(i); return true; } @@ -51,7 +57,7 @@ void dd::Systems::PhysicsSystem::Update(double dt) auto transformComponent = m_World->GetComponent(entity); - if (m_World->GetEntityParent(entity) == 0) { + if (m_World->GetEntityParent(entity) == 0) { //TODO: Make this work with childs too b2Vec2 position; position.x = transformComponent->Position.x; position.y = transformComponent->Position.y; @@ -59,10 +65,15 @@ void dd::Systems::PhysicsSystem::Update(double dt) float angle = -glm::eulerAngles(transformComponent->Orientation).z; body->SetTransform(position, angle); + + body->SetLinearVelocity(b2Vec2(transformComponent->Velocity.x, transformComponent->Velocity.y)); } } - + for (auto i : m_Impulses) { + i.Body->ApplyLinearImpulse(i.Impulse, i.Point, true); + } + m_Impulses.clear(); m_Accumulator += dt; @@ -94,8 +105,13 @@ void dd::Systems::PhysicsSystem::Update(double dt) float angle = body->GetAngle(); - //TODO: CHECK IF THIS IS CORRECT + transformComponent->Orientation = glm::quat(glm::vec3(0, 0, -angle)); + + b2Vec2 velocity = body->GetLinearVelocity(); + + transformComponent->Velocity.x = velocity.x; + transformComponent->Velocity.y = velocity.y; } } }