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
+2 -2
View File
@@ -108,14 +108,14 @@ public:
std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(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<Components::Model>(ent);
model->ModelFile = "Models/Test/Ball/Ballopus.obj";
std::shared_ptr<Components::CircleShape> circleShape = m_World->AddComponent<Components::CircleShape>(ent);
std::shared_ptr<Components::Ball> ball = m_World->AddComponent<Components::Ball>(ent);
ball->Speed = 10.f;
std::shared_ptr<Components::Physics> physics = m_World->AddComponent<Components::Physics>(ent);
physics->Static = false;
+1
View File
@@ -16,6 +16,7 @@ namespace Components
struct Ball : Component
{
int Number = 0;
float Speed = 5.f;
};
}
-2
View File
@@ -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)
{
+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;
+1
View File
@@ -61,6 +61,7 @@ void dd::Systems::PadSystem::UpdateEntity(double dt, EntityID entity, EntityID p
//auto pointlight = m_World->AddComponent<Components::PointLight>(ent);
std::shared_ptr<Components::CircleShape> circleShape = m_World->AddComponent<Components::CircleShape>(ent);
std::shared_ptr<Components::Ball> cball = m_World->AddComponent<Components::Ball>(ent);
cball->Speed = 10.f;
std::shared_ptr<Components::Physics> physics = m_World->AddComponent<Components::Physics>(ent);
physics->Static = false;