Falling bricks and collision filters

This commit is contained in:
viktorljung
2015-09-25 04:28:46 +02:00
parent d2301594ce
commit cfb6d1e4b5
9 changed files with 1992 additions and 22 deletions
+4 -2
View File
@@ -60,9 +60,10 @@ bool dd::Systems::BallSystem::Contact(const Events::Contact &event)
return false;
}
//Which is the ball?
EntityID ballEntity, otherEntitiy;
//Which is the ball?
if (ballComponent1 != nullptr) {
ballEntity = event.Entity1;
otherEntitiy = event.Entity2;
@@ -78,6 +79,8 @@ bool dd::Systems::BallSystem::Contact(const Events::Contact &event)
//TODO: Add support for power-up collisions
}
//if this is a brick thats dead do not collide :)
auto ballTransform = m_World->GetComponent<Components::Transform>(ballEntity);
glm::vec2 ballVelocity = glm::vec2(ballTransform->Velocity.x, ballTransform->Velocity.y);
@@ -85,7 +88,6 @@ bool dd::Systems::BallSystem::Contact(const Events::Contact &event)
auto padTransform = m_World->GetComponent<Components::Transform>(otherEntitiy);
float x = ballTransform->Position.x - padTransform->Position.x;
float movementMultiplier = 5.0f;
float y = glm::cos((abs(x) / (1.6f)) * glm::pi<float>() / 2.f) + 1.f;
ballTransform->Velocity = glm::normalize(glm::vec3(x, y ,0.f)) * ballComponent->Speed;
+23 -2
View File
@@ -176,6 +176,9 @@ void dd::Systems::LevelSystem::CreateBrick(int row, int line, glm::vec2 spacesBe
std::shared_ptr<Components::Physics> cPhys = m_World->AddComponent<Components::Physics>(brick);
cPhys->Static = false;
cPhys->GravityScale = 0.f;
cPhys->Category = CollisionLayer::Type::Brick;
cPhys->Mask = CollisionLayer::Type::Ball;
std::string fileName = "Textures/Bricks/";
fileName.append(std::to_string(num));
fileName.append(".png");
@@ -270,6 +273,20 @@ bool dd::Systems::LevelSystem::OnContact(const dd::Events::Contact &event)
auto physicsComponent = m_World->GetComponent<Components::Physics>(entityBrick);
physicsComponent->GravityScale = 1.f;
physicsComponent->Mask = CollisionLayer::Type::Water | CollisionLayer::Type::Wall;
auto transformComponentBrick = m_World->GetComponent<Components::Transform>(entityBrick);
auto transformComponentBall = m_World->GetComponent<Components::Transform>(entityBall);
Events::SetImpulse e;
e.Entity = entityBrick;
glm::vec2 point = glm::vec2(transformComponentBrick->Position.x + ((transformComponentBrick->Position.x - transformComponentBall->Position.x)/4), transformComponentBrick->Position.y + ((transformComponentBrick->Position.y - transformComponentBall->Position.y )/4));
e.Impulse = glm::normalize(point)/2.f;
e.Point = point;
EventBroker->Publish(e);
//TODO: Bricks dont collide with walls D=
SetNumberOfBricks(NumberOfBricks() - 1);
if (NumberOfBricks() <= 0) {
@@ -279,7 +296,7 @@ bool dd::Systems::LevelSystem::OnContact(const dd::Events::Contact &event)
es.Score = brick->Score;
EventBroker->Publish(es);
std::cout << NumberOfBricks() << std::endl;
// std::cout << NumberOfBricks() << std::endl;
//std::cout << "Score: " << Score() << std::endl;
}
@@ -313,8 +330,12 @@ bool dd::Systems::LevelSystem::OnCreatePowerUp(const dd::Events::CreatePowerUp &
auto model = m_World->AddComponent<Components::Model>(powerUp);
std::shared_ptr<Components::CircleShape> cRec = m_World->AddComponent<Components::CircleShape>(powerUp);
std::shared_ptr<Components::Physics> cPhys = m_World->AddComponent<Components::Physics>(powerUp);
std::shared_ptr<Components::PowerUp> cPow = m_World->AddComponent<Components::PowerUp>(powerUp);
cPhys->Static = false;
cPhys->Category = CollisionLayer::Type::PowerUp;
cPhys->Mask = CollisionLayer::Type::Pad;
std::shared_ptr<Components::PowerUp> cPow = m_World->AddComponent<Components::PowerUp>(powerUp);
transform->Position = event.Position;
transform->Scale = glm::vec3(0.2, 0.2, 0.2);
+6 -4
View File
@@ -108,7 +108,9 @@ EntityID dd::Systems::PadSystem::CreateBall()
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;
physics->Category = CollisionLayer::Type::Ball;
physics->Mask = CollisionLayer::Type::Pad | CollisionLayer::Type::Brick | CollisionLayer::Type::Wall;
physics->Calculate = true;
cball->Speed = 5.f;
m_World->CommitEntity(ent);
@@ -234,9 +236,9 @@ bool dd::Systems::PadSystem::OnContactPowerUp(const dd::Events::Contact &event)
return false;
}
m_World->RemoveComponent<Components::PowerUp>(entityPower);
m_World->RemoveComponent<Components::CircleShape>(entityPower);
m_World->RemoveComponent<Components::Physics>(entityPower);
// m_World->RemoveComponent<Components::PowerUp>(entityPower);
// m_World->RemoveComponent<Components::CircleShape>(entityPower);
// m_World->RemoveComponent<Components::Physics>(entityPower);
m_World->RemoveEntity(entityPower);
Events::PowerUpTaken ep;
ep.Name = "Something";
+20 -7
View File
@@ -85,6 +85,12 @@ void dd::Systems::PhysicsSystem::Update(double dt)
body->SetLinearVelocity(b2Vec2(transformComponent->Velocity.x, transformComponent->Velocity.y));
body->SetGravityScale(physicsComponent->GravityScale);
b2Filter filter;
filter.categoryBits = physicsComponent->Category;
filter.maskBits = physicsComponent->Mask;
body->GetFixtureList()->SetFilterData(filter);
}
}
@@ -106,7 +112,7 @@ void dd::Systems::PhysicsSystem::Update(double dt)
b2Body* body = i.second;
if (body == nullptr) {
//LOG_ERROR("This body should not exist");
LOG_ERROR("This body should not exist");
continue;
}
@@ -149,6 +155,11 @@ void dd::Systems::PhysicsSystem::OnEntityCommit(EntityID entity)
void dd::Systems::PhysicsSystem::OnEntityRemoved(EntityID entity)
{
auto physics = m_World->GetComponent<Components::Physics>(entity);
if (physics == nullptr) {
return;
}
b2Body* body = m_EntitiesToBodies[entity];
if (body != nullptr) {
@@ -180,8 +191,6 @@ void dd::Systems::PhysicsSystem::CreateBody(EntityID entity)
b2BodyDef bodyDef;
bodyDef.position.Set(absoluteTransform.Position.x, absoluteTransform.Position.y);
bodyDef.angle = -glm::eulerAngles(absoluteTransform.Orientation).z;
if (physicsComponent->Static) {
@@ -191,7 +200,6 @@ void dd::Systems::PhysicsSystem::CreateBody(EntityID entity)
}
b2Body* body = m_PhysicsWorld->CreateBody(&bodyDef);
b2Shape* pShape;
@@ -216,12 +224,16 @@ void dd::Systems::PhysicsSystem::CreateBody(EntityID entity)
}
}
b2FixtureDef fixtureDef;
fixtureDef.shape = pShape;
fixtureDef.filter.categoryBits = physicsComponent->Category;
fixtureDef.filter.maskBits = physicsComponent->Mask;
if(physicsComponent->Static) {
body->CreateFixture(pShape, 0); //Density kanske ska vara 0 på statiska kroppar
body->CreateFixture(&fixtureDef); //Density kanske ska vara 0 på statiska kroppar
}
else {
//TODO: FIX THIS SHIT INTO COMPONENTS
b2FixtureDef fixtureDef;
fixtureDef.shape = pShape;
fixtureDef.density = 1.f;
fixtureDef.restitution = 1.0f;
@@ -232,6 +244,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));