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) {
+67 -56
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,74 +50,84 @@ bool dd::Systems::PhysicsSystem::SetImpulse(const Events::SetImpulse &event)
void dd::Systems::PhysicsSystem::Update(double dt) void dd::Systems::PhysicsSystem::Update(double dt)
{ {
for (auto i : m_EntitiesToBodies) {
EntityID entity = i.first;
b2Body* body = i.second;
auto transformComponent = m_World->GetComponent<Components::Transform>(entity);
if (! transformComponent)
continue;
if (body == nullptr) {
//LOG_ERROR("This body should not exist");
continue;
}
if (m_World->GetEntityParent(entity) == 0) { //TODO: Make this work with childs too
b2Vec2 position;
position.x = transformComponent->Position.x;
position.y = transformComponent->Position.y;
float angle = -glm::eulerAngles(transformComponent->Orientation).z;
body->SetTransform(position, angle);
body->SetLinearVelocity(b2Vec2(transformComponent->Velocity.x, transformComponent->Velocity.y));
}
}
for (auto i : m_Impulses) {
i.Body->ApplyLinearImpulse(i.Impulse, i.Point, true);
}
m_Impulses.clear();
m_Accumulator += dt; m_Accumulator += dt;
while(m_Accumulator >= m_TimeStep) while(m_Accumulator >= m_TimeStep)
{ {
m_PhysicsWorld->Step(m_TimeStep, m_VelocityIterations, m_PositionIterations);
m_Accumulator -= dt; for (auto i : m_EntitiesToBodies) {
} EntityID entity = i.first;
b2Body* body = i.second;
auto transformComponent = m_World->GetComponent<Components::Transform>(entity);
if (! transformComponent) {
continue;
LOG_ERROR("RigidBody with no TransformComponent");
}
auto physicsComponent = m_World->GetComponent<Components::Physics>(entity);
if (! physicsComponent) {
continue;
LOG_ERROR("RigidBody with no PhysicsComponent");
}
for (auto i : m_EntitiesToBodies) { if (body == nullptr) {
EntityID entity = i.first; LOG_ERROR("This body should not exist");
b2Body* body = i.second; continue;
}
if (body == nullptr) { if (m_World->GetEntityParent(entity) == 0) { //TODO: Make this work with childs too
//LOG_ERROR("This body should not exist"); b2Vec2 position;
continue; position.x = transformComponent->Position.x;
position.y = transformComponent->Position.y;
float angle = -glm::eulerAngles(transformComponent->Orientation).z;
body->SetTransform(position, angle);
body->SetLinearVelocity(b2Vec2(transformComponent->Velocity.x, transformComponent->Velocity.y));
body->SetGravityScale(physicsComponent->GravityScale);
}
} }
auto transformComponent = m_World->GetComponent<Components::Transform>(entity); for (auto i : m_Impulses) {
if (! transformComponent) i.Body->ApplyLinearImpulse(i.Impulse, i.Point, true);
continue; }
if (m_World->GetEntityParent(entity) == 0) { m_Impulses.clear();
b2Vec2 position = body->GetPosition();
transformComponent->Position.x = position.x;
transformComponent->Position.y = position.y;
float angle = body->GetAngle();
transformComponent->Orientation = glm::quat(glm::vec3(0, 0, -angle));
b2Vec2 velocity = body->GetLinearVelocity();
transformComponent->Velocity.x = velocity.x;
transformComponent->Velocity.y = velocity.y;
m_PhysicsWorld->Step(m_TimeStep, m_VelocityIterations, m_PositionIterations);
m_Accumulator -= dt;
for (auto i : m_EntitiesToBodies) {
EntityID entity = i.first;
b2Body* body = i.second;
if (body == nullptr) {
//LOG_ERROR("This body should not exist");
continue;
}
auto transformComponent = m_World->GetComponent<Components::Transform>(entity);
if (! transformComponent)
continue;
if (m_World->GetEntityParent(entity) == 0) {
b2Vec2 position = body->GetPosition();
transformComponent->Position.x = position.x;
transformComponent->Position.y = position.y;
float angle = body->GetAngle();
transformComponent->Orientation = glm::quat(glm::vec3(0, 0, -angle));
b2Vec2 velocity = body->GetLinearVelocity();
transformComponent->Velocity.x = velocity.x;
transformComponent->Velocity.y = velocity.y;
}
} }
} }
} }
@@ -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));
} }