diff --git a/include/Core/Engine.h b/include/Core/Engine.h index 5c560f1..3dd2376 100644 --- a/include/Core/Engine.h +++ b/include/Core/Engine.h @@ -114,7 +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); + transform->Velocity = glm::vec3(0.0f, -5.f, 0.f); std::shared_ptr sprite = m_World->AddComponent(ent); sprite->SpriteFile = "Textures/Ball.png"; @@ -130,11 +130,7 @@ public: m_World->CommitEntity(ent); - Events::SetImpulse e; - e.Entity = ent; - e.Impulse = glm::vec2(0.f, -7.f); - e.Point = glm::vec2(0.5f, 0.f); - m_EventBroker->Publish(e); + } { @@ -213,6 +209,7 @@ public: ctransform->Scale = glm::vec3(3.2, 0.8, 0.); auto rectangle = m_World->AddComponent(ent); auto physics = m_World->AddComponent(ent); + physics->Static = false; auto csprite = m_World->AddComponent(ent); auto pad = m_World->AddComponent(ent); csprite->SpriteFile = "Textures/Pad.png"; diff --git a/include/Physics/PhysicsSystem.h b/include/Physics/PhysicsSystem.h index 559d815..2e0aa02 100644 --- a/include/Physics/PhysicsSystem.h +++ b/include/Physics/PhysicsSystem.h @@ -95,9 +95,12 @@ private: void EndContact(b2Contact* contact) { /* handle end event */ } void PreSolve(b2Contact* contact, const b2Manifold* oldManifold) - { /* handle pre-solve event */ } + { /* handle pre-solve event */} void PostSolve(b2Contact* contact, const b2ContactImpulse* impulse) - { /* handle post-solve event */ } + { /* handle post-solve event */ + contact->GetFixtureA()->GetBody()->SetAngularVelocity(0.f); + contact->GetFixtureB()->GetBody()->SetAngularVelocity(0.f); + } private: PhysicsSystem* m_PhysicsSystem; diff --git a/src/game/Game/PadSystem.cpp b/src/game/Game/PadSystem.cpp index a465b8b..4dc699d 100644 --- a/src/game/Game/PadSystem.cpp +++ b/src/game/Game/PadSystem.cpp @@ -49,8 +49,11 @@ void dd::Systems::PadSystem::Update(double dt) transform->Velocity.x = 400.f; } transform->Position += transform->Velocity * (float)dt; - transform->Velocity += acceleration * (float)dt; + transform->Velocity += acceleration * (float)dt; transform->Velocity -= transform->Velocity * (0.9f * (float)dt); + transform->Position.y = -5.f; + //transform->Orientation = glm::quat(); + std::cout << transform->Velocity.x << ", " << transform->Velocity.y << std::endl; if (left) { @@ -135,31 +138,18 @@ bool dd::Systems::PadSystem::OnContact(const dd::Events::Contact &event) float movementMultiplier = 2.f; - float movementX = (event.ContactPoint.x - transformPad->Position.x) * movementMultiplier; - float movementY = glm::cos((abs(movementX) / (3.2f * movementMultiplier)) * 3.14159265359f / 2) * 2.f; + //float movementX = (event.ContactPoint.x - transformPad->Position.x) * movementMultiplier; + //float movementY = glm::cos((abs(movementX) / (3.2f * movementMultiplier)) * 3.14159265359f / 2) * 2.f; - std::cout << movementX << " " << movementY << std::endl; + // std::cout << movementX << " " << movementY << std::endl; - //Temporary solution. Add a new ball and delete the old one. - auto ent = m_World->CreateEntity(); - std::shared_ptr transform = m_World->AddComponent(ent); - transform->Position = glm::vec3(transformBall->Position.x, transformBall->Position.y + 0.1f, -10.f); - transform->Scale = glm::vec3(1.f, 1.f, 1.f); - std::shared_ptr sprite = m_World->AddComponent(ent); - sprite->SpriteFile = "Textures/Ball.png"; - std::shared_ptr circleShape = m_World->AddComponent(ent); - std::shared_ptr cball = m_World->AddComponent(ent); - std::shared_ptr physics = m_World->AddComponent(ent); - physics->Static = false; + float len = glm::length(transformBall->Velocity); - m_World->RemoveEntity(entityBall); - m_World->CommitEntity(ent); + transformBall->Velocity += glm::vec3(transformPad->Velocity.x, 0, 0); + transformBall->Velocity = glm::normalize(transformBall->Velocity) * len; - Events::SetImpulse e; - e.Entity = ent; - e.Impulse = glm::vec2(movementX, movementY); - e.Point = glm::vec2(event.ContactPoint); - EventBroker->Publish(e); + + //transform->Velocity = glm::vec3(movementX, movementY, 0.f); } bool dd::Systems::PadSystem::PadSteeringInputController::OnCommand(const Events::InputCommand &event) diff --git a/src/game/Physics/PhysicsSystem.cpp b/src/game/Physics/PhysicsSystem.cpp index 863ad74..5880016 100644 --- a/src/game/Physics/PhysicsSystem.cpp +++ b/src/game/Physics/PhysicsSystem.cpp @@ -66,7 +66,10 @@ void dd::Systems::PhysicsSystem::Update(double dt) body->SetTransform(position, angle); + body->SetLinearVelocity(b2Vec2(transformComponent->Velocity.x, transformComponent->Velocity.y)); + + } } @@ -84,13 +87,6 @@ void dd::Systems::PhysicsSystem::Update(double dt) } - - - - - - - for (auto i : m_EntitiesToBodies) { EntityID entity = i.first; b2Body* body = i.second; @@ -98,6 +94,7 @@ void dd::Systems::PhysicsSystem::Update(double dt) auto transformComponent = m_World->GetComponent(entity); + if (m_World->GetEntityParent(entity) == 0) { b2Vec2 position = body->GetPosition(); transformComponent->Position.x = position.x; @@ -109,9 +106,9 @@ void dd::Systems::PhysicsSystem::Update(double dt) transformComponent->Orientation = glm::quat(glm::vec3(0, 0, -angle)); b2Vec2 velocity = body->GetLinearVelocity(); - transformComponent->Velocity.x = velocity.x; transformComponent->Velocity.y = velocity.y; + } } } @@ -169,6 +166,7 @@ void dd::Systems::PhysicsSystem::CreateBody(EntityID entity) bodyDef.type = b2_staticBody; } else { bodyDef.type = b2_dynamicBody; + } @@ -205,7 +203,7 @@ void dd::Systems::PhysicsSystem::CreateBody(EntityID entity) fixtureDef.shape = pShape; fixtureDef.density = 1.f; fixtureDef.restitution = 1.0f; - fixtureDef.friction = 0.3f; + fixtureDef.friction = 0.0f; body->CreateFixture(&fixtureDef); }