Pad modifies ball path and ball facing its direction

This commit is contained in:
viktorljung
2015-09-23 15:12:18 +02:00
parent 8ca01ae9c7
commit 03e2078cfb
5 changed files with 35 additions and 15 deletions
+31 -11
View File
@@ -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<Components::Ball>(entity);
if (ballComponent != nullptr) {
auto transform = m_World->GetComponent<Components::Transform>(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<float>(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<Components::Ball>(event.Entity1);
auto ballComponent2 = m_World->GetComponent<Components::Ball>(event.Entity2);
Components::Ball* ballComponent1 = m_World->GetComponent<Components::Ball>(event.Entity1);
Components::Ball* ballComponent2 = m_World->GetComponent<Components::Ball>(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<Components::Transform>(ballEntity);
auto otherTransform = m_World->GetComponent<Components::Transform>(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<std::string>(otherEntitiy, "Name") == "Pad"){
auto padTransform = m_World->GetComponent<Components::Transform>(otherEntitiy);
float x = ballTransform->Position.x - padTransform->Position.x;
float movementMultiplier = 5.0f;
float y = glm::cos((abs(x) / ((1.6f) * movementMultiplier)) * glm::pi<float>() / 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;