From 03e2078cfb9872f74e1d8b8a10b42a4e3de1e527 Mon Sep 17 00:00:00 2001 From: viktorljung Date: Wed, 23 Sep 2015 15:12:18 +0200 Subject: [PATCH] Pad modifies ball path and ball facing its direction --- include/Core/Engine.h | 4 ++-- include/Game/CBall.h | 1 + include/Physics/PhysicsSystem.h | 2 -- src/game/Game/BallSystem.cpp | 42 ++++++++++++++++++++++++--------- src/game/Game/PadSystem.cpp | 1 + 5 files changed, 35 insertions(+), 15 deletions(-) diff --git a/include/Core/Engine.h b/include/Core/Engine.h index e453928..1099663 100644 --- a/include/Core/Engine.h +++ b/include/Core/Engine.h @@ -108,14 +108,14 @@ 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(-1.0f, -5.f, 0.f); + transform->Velocity = glm::vec3(0.0f, -10.f, 0.f); auto model = m_World->AddComponent(ent); model->ModelFile = "Models/Test/Ball/Ballopus.obj"; std::shared_ptr circleShape = m_World->AddComponent(ent); std::shared_ptr ball = m_World->AddComponent(ent); - + ball->Speed = 10.f; std::shared_ptr physics = m_World->AddComponent(ent); physics->Static = false; diff --git a/include/Game/CBall.h b/include/Game/CBall.h index df53712..2990a3a 100644 --- a/include/Game/CBall.h +++ b/include/Game/CBall.h @@ -16,6 +16,7 @@ namespace Components struct Ball : Component { int Number = 0; + float Speed = 5.f; }; } diff --git a/include/Physics/PhysicsSystem.h b/include/Physics/PhysicsSystem.h index 16c2cb7..3c456ce 100644 --- a/include/Physics/PhysicsSystem.h +++ b/include/Physics/PhysicsSystem.h @@ -94,8 +94,6 @@ private: e.SignificantNormal = glm::normalize((glm::abs(e.Normal.x) > glm::abs(e.Normal.y)) ? glm::vec2(e.Normal.x, 0) : glm::vec2(0, e.Normal.y)); m_PhysicsSystem->EventBroker->Publish(e); - - LOG_INFO("Entity1 = %i, Entity2 = %i\n Normal: %f, %f", e.Entity1, e.Entity2, e.SignificantNormal.x, e.SignificantNormal.y); } void EndContact(b2Contact* contact) { diff --git a/src/game/Game/BallSystem.cpp b/src/game/Game/BallSystem.cpp index 93fdf26..5ac383b 100644 --- a/src/game/Game/BallSystem.cpp +++ b/src/game/Game/BallSystem.cpp @@ -23,7 +23,14 @@ void dd::Systems::BallSystem::Update(double dt) void dd::Systems::BallSystem::UpdateEntity(double dt, EntityID entity, EntityID parent) { - + auto ballComponent = m_World->GetComponent(entity); + if (ballComponent != nullptr) { + auto transform = m_World->GetComponent(entity); + glm::vec2 dir = glm::normalize(glm::vec2(transform->Velocity.x, transform->Velocity.y)); + glm::vec2 up = glm::vec2(0.f, 1.f); + float angle = glm::acos(glm::dot(dir, up)) * glm::sign(dir.x); + transform->Orientation = glm::rotate(glm::quat(), angle, glm::vec3(0.f, 0.f, -1.f)); + } } void dd::Systems::BallSystem::OnEntityCommit(EntityID entity) @@ -38,35 +45,48 @@ void dd::Systems::BallSystem::OnEntityRemoved(EntityID entity) bool dd::Systems::BallSystem::Contact(const Events::Contact &event) { - auto ballComponent1 = m_World->GetComponent(event.Entity1); - auto ballComponent2 = m_World->GetComponent(event.Entity2); + + Components::Ball* ballComponent1 = m_World->GetComponent(event.Entity1); + Components::Ball* ballComponent2 = m_World->GetComponent(event.Entity2); + Components::Ball* ballComponent; //Which is the ball? EntityID ballEntity, otherEntitiy; - glm::vec2 normal; + if (ballComponent1 != nullptr) { ballEntity = event.Entity1; otherEntitiy = event.Entity2; + ballComponent = ballComponent1; } else if (ballComponent2 != nullptr) { ballEntity = event.Entity2; otherEntitiy = event.Entity1; + ballComponent = ballComponent2; } else { return false; //TODO: Add support for power-up collisions } - //normal.x = -normal.x; - auto ballTransform = m_World->GetComponent(ballEntity); - auto otherTransform = m_World->GetComponent(otherEntitiy); - glm::vec2 ballVelocity = glm::vec2(ballTransform->Velocity.x, ballTransform->Velocity.y); - glm::vec2 reflectedVelocity = glm::reflect(ballVelocity, event.Normal); - ballTransform->Velocity = glm::vec3(reflectedVelocity, 0.f); - //LOG_INFO("Velocity: %f, %f\n", ballTransform->Velocity.x, ballTransform->Velocity.y); + if (m_World->GetProperty(otherEntitiy, "Name") == "Pad"){ + auto padTransform = m_World->GetComponent(otherEntitiy); + float x = ballTransform->Position.x - padTransform->Position.x; + + float movementMultiplier = 5.0f; + float y = glm::cos((abs(x) / ((1.6f) * movementMultiplier)) * glm::pi() / 2.f) + 1.f; + + ballTransform->Velocity = glm::normalize(glm::vec3(x, y ,0.f)) * ballComponent->Speed; + } + else { + glm::vec2 reflectedVelocity = glm::reflect(ballVelocity, event.Normal); + ballTransform->Velocity = glm::vec3(reflectedVelocity, 0.f); + } + + + return true; diff --git a/src/game/Game/PadSystem.cpp b/src/game/Game/PadSystem.cpp index 3427eeb..82f29d5 100644 --- a/src/game/Game/PadSystem.cpp +++ b/src/game/Game/PadSystem.cpp @@ -61,6 +61,7 @@ void dd::Systems::PadSystem::UpdateEntity(double dt, EntityID entity, EntityID p //auto pointlight = m_World->AddComponent(ent); std::shared_ptr circleShape = m_World->AddComponent(ent); std::shared_ptr cball = m_World->AddComponent(ent); + cball->Speed = 10.f; std::shared_ptr physics = m_World->AddComponent(ent); physics->Static = false;