diff --git a/include/Physics/CPhysics.h b/include/Physics/CPhysics.h index 8d1f975..4f90b75 100644 --- a/include/Physics/CPhysics.h +++ b/include/Physics/CPhysics.h @@ -11,6 +11,7 @@ struct Physics: public Component { //idk if anything should be here :) bool Static = true; + float GravityScale = 0.f; }; } diff --git a/src/game/Game/LevelSystem.cpp b/src/game/Game/LevelSystem.cpp index b4f5d7d..239b7b8 100755 --- a/src/game/Game/LevelSystem.cpp +++ b/src/game/Game/LevelSystem.cpp @@ -72,10 +72,10 @@ void dd::Systems::LevelSystem::UpdateEntity(double dt, EntityID entity, EntityID if (transformBall->Position.y < -EdgeY() - 4) { if (MultiBalls() != 0) { SetMultiBalls(MultiBalls() - 1); - m_World->RemoveComponent(entity); - m_World->RemoveComponent(entity); - m_World->RemoveComponent(entity); - m_World->RemoveComponent(entity); +// m_World->RemoveComponent(entity); +// m_World->RemoveComponent(entity); +// m_World->RemoveComponent(entity); +// m_World->RemoveComponent(entity); m_World->RemoveEntity(entity); } else if (Lives() == PastLives()) { Events::ResetBall be; @@ -107,8 +107,8 @@ void dd::Systems::LevelSystem::UpdateEntity(double dt, EntityID entity, EntityID auto life = m_World->GetComponent(entity); if (life != nullptr) { if (life->Number + 1 == PastLives()) { - m_World->RemoveComponent(entity); - m_World->RemoveComponent(entity); +// m_World->RemoveComponent(entity); +// m_World->RemoveComponent(entity); m_World->RemoveEntity(entity); SetPastLives(Lives()); } @@ -170,11 +170,12 @@ void dd::Systems::LevelSystem::CreateBrick(int row, int line, glm::vec2 spacesBe { auto brick = m_World->CreateEntity(); std::shared_ptr transform = m_World->AddComponent(brick); - //std::shared_ptr sprite = m_World->AddComponent(brick); auto model = m_World->AddComponent(brick); std::shared_ptr cBrick = m_World->AddComponent(brick); std::shared_ptr cRec = m_World->AddComponent(brick); std::shared_ptr cPhys = m_World->AddComponent(brick); + cPhys->Static = false; + cPhys->GravityScale = 0.f; std::string fileName = "Textures/Bricks/"; fileName.append(std::to_string(num)); fileName.append(".png"); @@ -261,11 +262,14 @@ bool dd::Systems::LevelSystem::OnContact(const dd::Events::Contact &event) if (ball != nullptr && Restarting() == false) { auto ballTransform = m_World->GetComponent(entityBall); - m_World->RemoveComponent(entityBrick); - m_World->RemoveComponent(entityBrick); - m_World->RemoveComponent(entityBrick); - m_World->RemoveComponent(entityBrick); - m_World->RemoveEntity(entityBrick); +// m_World->RemoveComponent(entityBrick); +// m_World->RemoveComponent(entityBrick); +// m_World->RemoveComponent(entityBrick); +// m_World->RemoveComponent(entityBrick); +// m_World->RemoveEntity(entityBrick); + + auto physicsComponent = m_World->GetComponent(entityBrick); + physicsComponent->GravityScale = 1.f; SetNumberOfBricks(NumberOfBricks() - 1); if (NumberOfBricks() <= 0) { diff --git a/src/game/Physics/PhysicsSystem.cpp b/src/game/Physics/PhysicsSystem.cpp index 2a868f6..6994408 100644 --- a/src/game/Physics/PhysicsSystem.cpp +++ b/src/game/Physics/PhysicsSystem.cpp @@ -12,7 +12,7 @@ void dd::Systems::PhysicsSystem::Initialize() { 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_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) { - for (auto i : m_EntitiesToBodies) { - EntityID entity = i.first; - b2Body* body = i.second; - - auto transformComponent = m_World->GetComponent(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; 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(entity); + if (! transformComponent) { + continue; + LOG_ERROR("RigidBody with no TransformComponent"); + } + auto physicsComponent = m_World->GetComponent(entity); + if (! physicsComponent) { + continue; + LOG_ERROR("RigidBody with no PhysicsComponent"); + } - for (auto i : m_EntitiesToBodies) { - EntityID entity = i.first; - b2Body* body = i.second; + if (body == nullptr) { + LOG_ERROR("This body should not exist"); + 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)); + + body->SetGravityScale(physicsComponent->GravityScale); + } } - auto transformComponent = m_World->GetComponent(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(); + for (auto i : m_Impulses) { + i.Body->ApplyLinearImpulse(i.Impulse, i.Point, true); + } + m_Impulses.clear(); - 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(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; + body->SetGravityScale(physicsComponent->GravityScale); m_EntitiesToBodies.insert(std::make_pair(entity, body)); m_BodiesToEntities.insert(std::make_pair(body, entity)); }