From 71cdd1f795275c574dba7c21e413ee406c62d47f Mon Sep 17 00:00:00 2001 From: viktorljung Date: Thu, 10 Sep 2015 22:43:25 +0100 Subject: [PATCH] SetImpulse event --- include/Physics/ESetImpulse.h | 6 +++--- include/Physics/PhysicsSystem.h | 5 +++++ src/game/Physics/PhysicsSystem.cpp | 19 +++++++++++++++++++ 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/include/Physics/ESetImpulse.h b/include/Physics/ESetImpulse.h index b66746d..eff3f15 100644 --- a/include/Physics/ESetImpulse.h +++ b/include/Physics/ESetImpulse.h @@ -9,11 +9,11 @@ namespace dd namespace Events { -struct Contact : Event +struct SetImpulse : Event { EntityID Entity; - glm::vec2 Direction; - float Strength; + glm::vec2 Impulse; + glm::vec2 Point; }; } diff --git a/include/Physics/PhysicsSystem.h b/include/Physics/PhysicsSystem.h index b7c91e4..265e511 100644 --- a/include/Physics/PhysicsSystem.h +++ b/include/Physics/PhysicsSystem.h @@ -11,7 +11,9 @@ #include "Core/CTransform.h" #include "Transform/TransformSystem.h" #include "Physics/EContact.h" +#include "Physics/ESetImpulse.h" #include "Physics/CCircleShape.h" +#include "Core/EventBroker.h" namespace dd @@ -32,6 +34,9 @@ public: ~PhysicsSystem(); + EventRelay m_SetImpulse; + bool SetImpulse(const Events::SetImpulse &event); + void RegisterComponents(ComponentFactory* cf) override; void Initialize() override; diff --git a/src/game/Physics/PhysicsSystem.cpp b/src/game/Physics/PhysicsSystem.cpp index b202abd..17fd361 100644 --- a/src/game/Physics/PhysicsSystem.cpp +++ b/src/game/Physics/PhysicsSystem.cpp @@ -21,6 +21,25 @@ void dd::Systems::PhysicsSystem::Initialize() m_PhysicsWorld->SetContactListener(m_ContactListener); + + EVENT_SUBSCRIBE_MEMBER(m_SetImpulse, PhysicsSystem::SetImpulse); +} + +bool dd::Systems::PhysicsSystem::SetImpulse(const Events::SetImpulse &event) +{ + b2Body* body = m_EntitiesToBodies[event.Entity]; + + b2Vec2 impulse; + impulse.x = event.Impulse.x; + impulse.y = event.Impulse.y; + + b2Vec2 point; + point.x = event.Point.x; + point.y = event.Point.y; + + body->ApplyLinearImpulse(impulse, point, true); + + return true; } void dd::Systems::PhysicsSystem::Update(double dt)