pad moving with physics velocity

This commit is contained in:
viktorljung
2015-09-15 14:27:35 +01:00
parent 2d7b32b7a3
commit 8963e94cd6
4 changed files with 27 additions and 39 deletions
+3 -6
View File
@@ -114,7 +114,7 @@ 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(0.0f, 0.f, 0.f);
transform->Velocity = glm::vec3(0.0f, -5.f, 0.f);
std::shared_ptr<Components::Sprite> sprite = m_World->AddComponent<Components::Sprite>(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<Components::RectangleShape>(ent);
auto physics = m_World->AddComponent<Components::Physics>(ent);
physics->Static = false;
auto csprite = m_World->AddComponent<Components::Sprite>(ent);
auto pad = m_World->AddComponent<Components::Pad>(ent);
csprite->SpriteFile = "Textures/Pad.png";
+5 -2
View File
@@ -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;
+12 -22
View File
@@ -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<Components::Transform> transform = m_World->AddComponent<Components::Transform>(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<Components::Sprite> sprite = m_World->AddComponent<Components::Sprite>(ent);
sprite->SpriteFile = "Textures/Ball.png";
std::shared_ptr<Components::CircleShape> circleShape = m_World->AddComponent<Components::CircleShape>(ent);
std::shared_ptr<Components::Ball> cball = m_World->AddComponent<Components::Ball>(ent);
std::shared_ptr<Components::Physics> physics = m_World->AddComponent<Components::Physics>(ent);
physics->Static = false;
float len = glm::length<float>(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)
+7 -9
View File
@@ -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<Components::Transform>(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);
}