Gravity scale per entity

This commit is contained in:
viktorljung
2015-09-25 00:01:24 +02:00
parent bc6b037923
commit d2301594ce
3 changed files with 84 additions and 68 deletions
+1
View File
@@ -11,6 +11,7 @@ struct Physics: public Component
{ {
//idk if anything should be here :) //idk if anything should be here :)
bool Static = true; bool Static = true;
float GravityScale = 0.f;
}; };
} }
+16 -12
View File
@@ -72,10 +72,10 @@ void dd::Systems::LevelSystem::UpdateEntity(double dt, EntityID entity, EntityID
if (transformBall->Position.y < -EdgeY() - 4) { if (transformBall->Position.y < -EdgeY() - 4) {
if (MultiBalls() != 0) { if (MultiBalls() != 0) {
SetMultiBalls(MultiBalls() - 1); SetMultiBalls(MultiBalls() - 1);
m_World->RemoveComponent<Components::Ball>(entity); // m_World->RemoveComponent<Components::Ball>(entity);
m_World->RemoveComponent<Components::Transform>(entity); // m_World->RemoveComponent<Components::Transform>(entity);
m_World->RemoveComponent<Components::CircleShape>(entity); // m_World->RemoveComponent<Components::CircleShape>(entity);
m_World->RemoveComponent<Components::Physics>(entity); // m_World->RemoveComponent<Components::Physics>(entity);
m_World->RemoveEntity(entity); m_World->RemoveEntity(entity);
} else if (Lives() == PastLives()) { } else if (Lives() == PastLives()) {
Events::ResetBall be; Events::ResetBall be;
@@ -107,8 +107,8 @@ void dd::Systems::LevelSystem::UpdateEntity(double dt, EntityID entity, EntityID
auto life = m_World->GetComponent<Components::Life>(entity); auto life = m_World->GetComponent<Components::Life>(entity);
if (life != nullptr) { if (life != nullptr) {
if (life->Number + 1 == PastLives()) { if (life->Number + 1 == PastLives()) {
m_World->RemoveComponent<Components::Life>(entity); // m_World->RemoveComponent<Components::Life>(entity);
m_World->RemoveComponent<Components::Transform>(entity); // m_World->RemoveComponent<Components::Transform>(entity);
m_World->RemoveEntity(entity); m_World->RemoveEntity(entity);
SetPastLives(Lives()); SetPastLives(Lives());
} }
@@ -170,11 +170,12 @@ void dd::Systems::LevelSystem::CreateBrick(int row, int line, glm::vec2 spacesBe
{ {
auto brick = m_World->CreateEntity(); auto brick = m_World->CreateEntity();
std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(brick); std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(brick);
//std::shared_ptr<Components::Sprite> sprite = m_World->AddComponent<Components::Sprite>(brick);
auto model = m_World->AddComponent<Components::Model>(brick); auto model = m_World->AddComponent<Components::Model>(brick);
std::shared_ptr<Components::Brick> cBrick = m_World->AddComponent<Components::Brick>(brick); std::shared_ptr<Components::Brick> cBrick = m_World->AddComponent<Components::Brick>(brick);
std::shared_ptr<Components::RectangleShape> cRec = m_World->AddComponent<Components::RectangleShape>(brick); std::shared_ptr<Components::RectangleShape> cRec = m_World->AddComponent<Components::RectangleShape>(brick);
std::shared_ptr<Components::Physics> cPhys = m_World->AddComponent<Components::Physics>(brick); std::shared_ptr<Components::Physics> cPhys = m_World->AddComponent<Components::Physics>(brick);
cPhys->Static = false;
cPhys->GravityScale = 0.f;
std::string fileName = "Textures/Bricks/"; std::string fileName = "Textures/Bricks/";
fileName.append(std::to_string(num)); fileName.append(std::to_string(num));
fileName.append(".png"); fileName.append(".png");
@@ -261,11 +262,14 @@ bool dd::Systems::LevelSystem::OnContact(const dd::Events::Contact &event)
if (ball != nullptr && Restarting() == false) { if (ball != nullptr && Restarting() == false) {
auto ballTransform = m_World->GetComponent<Components::Transform>(entityBall); auto ballTransform = m_World->GetComponent<Components::Transform>(entityBall);
m_World->RemoveComponent<Components::Brick>(entityBrick); // m_World->RemoveComponent<Components::Brick>(entityBrick);
m_World->RemoveComponent<Components::Transform>(entityBrick); // m_World->RemoveComponent<Components::Transform>(entityBrick);
m_World->RemoveComponent<Components::RectangleShape>(entityBrick); // m_World->RemoveComponent<Components::RectangleShape>(entityBrick);
m_World->RemoveComponent<Components::Physics>(entityBrick); // m_World->RemoveComponent<Components::Physics>(entityBrick);
m_World->RemoveEntity(entityBrick); // m_World->RemoveEntity(entityBrick);
auto physicsComponent = m_World->GetComponent<Components::Physics>(entityBrick);
physicsComponent->GravityScale = 1.f;
SetNumberOfBricks(NumberOfBricks() - 1); SetNumberOfBricks(NumberOfBricks() - 1);
if (NumberOfBricks() <= 0) { if (NumberOfBricks() <= 0) {
+22 -11
View File
@@ -12,7 +12,7 @@ void dd::Systems::PhysicsSystem::Initialize()
{ {
m_ContactListener = new ContactListener(this); m_ContactListener = new ContactListener(this);
m_Gravity = b2Vec2(0.f, 0.f); m_Gravity = b2Vec2(0.f, -9.82f);
m_PhysicsWorld = new b2World(m_Gravity); m_PhysicsWorld = new b2World(m_Gravity);
m_TimeStep = 1.f/60.f; m_TimeStep = 1.f/60.f;
@@ -50,16 +50,29 @@ bool dd::Systems::PhysicsSystem::SetImpulse(const Events::SetImpulse &event)
void dd::Systems::PhysicsSystem::Update(double dt) void dd::Systems::PhysicsSystem::Update(double dt)
{ {
m_Accumulator += dt;
while(m_Accumulator >= m_TimeStep)
{
for (auto i : m_EntitiesToBodies) { for (auto i : m_EntitiesToBodies) {
EntityID entity = i.first; EntityID entity = i.first;
b2Body* body = i.second; b2Body* body = i.second;
auto transformComponent = m_World->GetComponent<Components::Transform>(entity); auto transformComponent = m_World->GetComponent<Components::Transform>(entity);
if (! transformComponent) if (! transformComponent) {
continue; continue;
LOG_ERROR("RigidBody with no TransformComponent");
}
auto physicsComponent = m_World->GetComponent<Components::Physics>(entity);
if (! physicsComponent) {
continue;
LOG_ERROR("RigidBody with no PhysicsComponent");
}
if (body == nullptr) { if (body == nullptr) {
//LOG_ERROR("This body should not exist"); LOG_ERROR("This body should not exist");
continue; continue;
} }
@@ -67,14 +80,11 @@ void dd::Systems::PhysicsSystem::Update(double dt)
b2Vec2 position; b2Vec2 position;
position.x = transformComponent->Position.x; position.x = transformComponent->Position.x;
position.y = transformComponent->Position.y; position.y = transformComponent->Position.y;
float angle = -glm::eulerAngles(transformComponent->Orientation).z; float angle = -glm::eulerAngles(transformComponent->Orientation).z;
body->SetTransform(position, angle); body->SetTransform(position, angle);
body->SetLinearVelocity(b2Vec2(transformComponent->Velocity.x, transformComponent->Velocity.y)); body->SetLinearVelocity(b2Vec2(transformComponent->Velocity.x, transformComponent->Velocity.y));
body->SetGravityScale(physicsComponent->GravityScale);
} }
} }
@@ -84,12 +94,11 @@ void dd::Systems::PhysicsSystem::Update(double dt)
m_Impulses.clear(); m_Impulses.clear();
m_Accumulator += dt;
while(m_Accumulator >= m_TimeStep)
{
m_PhysicsWorld->Step(m_TimeStep, m_VelocityIterations, m_PositionIterations); m_PhysicsWorld->Step(m_TimeStep, m_VelocityIterations, m_PositionIterations);
m_Accumulator -= dt; m_Accumulator -= dt;
}
for (auto i : m_EntitiesToBodies) { for (auto i : m_EntitiesToBodies) {
@@ -120,6 +129,7 @@ void dd::Systems::PhysicsSystem::Update(double dt)
} }
} }
}
} }
void dd::Systems::PhysicsSystem::UpdateEntity(double dt, EntityID entity, EntityID parent) void dd::Systems::PhysicsSystem::UpdateEntity(double dt, EntityID entity, EntityID parent)
@@ -222,6 +232,7 @@ void dd::Systems::PhysicsSystem::CreateBody(EntityID entity)
delete pShape; delete pShape;
body->SetGravityScale(physicsComponent->GravityScale);
m_EntitiesToBodies.insert(std::make_pair(entity, body)); m_EntitiesToBodies.insert(std::make_pair(entity, body));
m_BodiesToEntities.insert(std::make_pair(body, entity)); m_BodiesToEntities.insert(std::make_pair(body, entity));
} }