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
+12
View File
@@ -0,0 +1,12 @@
# Blender MTL File: 'submarine.blend'
# Material Count: 1
newmtl Material
Ns 96.078431
Ka 0.000000 0.000000 0.000000
Kd 0.640000 0.640000 0.640000
Ks 0.500000 0.500000 0.500000
Ni 1.000000
d 1.000000
illum 2
map_Kd SubmarineTexture.png
File diff suppressed because it is too large Load Diff
+14 -1
View File
@@ -135,6 +135,9 @@ public:
ball->Speed = 5.f;
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;
auto plight = m_World->AddComponent<Components::PointLight>(ent);
plight->Radius = 2.f;
@@ -176,6 +179,8 @@ public:
std::shared_ptr<Components::Physics> physics = m_World->AddComponent<Components::Physics>(topWall);
physics->Static = true;
physics->Category = CollisionLayer::Type::Wall;
physics->Mask = CollisionLayer::Type::Ball | CollisionLayer::Type::Brick;
m_World->CommitEntity(topWall);
}
@@ -192,6 +197,8 @@ public:
std::shared_ptr<Components::Physics> physics = m_World->AddComponent<Components::Physics>(leftWall);
physics->Static = true;
physics->Category = CollisionLayer::Type::Wall;
physics->Mask = CollisionLayer::Type::Ball | CollisionLayer::Type::Brick;
m_World->CommitEntity(leftWall);
}
@@ -208,6 +215,8 @@ public:
std::shared_ptr<Components::Physics> physics = m_World->AddComponent<Components::Physics>(rightWall);
physics->Static = true;
physics->Category = CollisionLayer::Type::Wall;
physics->Mask = CollisionLayer::Type::Ball | CollisionLayer::Type::Brick;
m_World->CommitEntity(rightWall);
}
@@ -221,8 +230,12 @@ public:
auto rectangle = m_World->AddComponent<Components::RectangleShape>(ent);
auto physics = m_World->AddComponent<Components::Physics>(ent);
physics->Static = false;
physics->Category = CollisionLayer::Type::Pad;
physics->Mask = CollisionLayer::Type::Ball | CollisionLayer::Type::PowerUp;
physics->Calculate = true;
auto cModel = m_World->AddComponent<Components::Model>(ent);
cModel->ModelFile = "Models/Submarine.obj";
cModel->ModelFile = "Models/Submarine2.obj";
auto pad = m_World->AddComponent<Components::Pad>(ent);
m_World->CommitEntity(ent);
}
+24 -2
View File
@@ -4,18 +4,40 @@
namespace dd
{
namespace CollisionLayer
{
enum Type
{
Pad = 1 << 0,
Ball = 1 << 1,
Brick = 1 << 2,
Water = 1 << 3,
PowerUp = 1 << 4,
Other = 1 << 5,
Wall = 1 << 6
};
}
namespace Components
{
struct Physics: public Component
{
//idk if anything should be here :)
bool Static = true;
bool Calculate = false;
float GravityScale = 0.f;
CollisionLayer::Type Category = CollisionLayer::Type::Other;
CollisionLayer::Type Mask =
CollisionLayer::Type::Pad |
CollisionLayer::Type::Ball |
CollisionLayer::Type::Brick |
CollisionLayer::Type::Water |
CollisionLayer::Type::PowerUp |
CollisionLayer::Type::Wall |
CollisionLayer::Type::Other;
};
}
}
#endif //DAYDREAM_CPHYSICS_H
+8 -4
View File
@@ -15,6 +15,7 @@
#include "Physics/CCircleShape.h"
#include "Core/EventBroker.h"
#include "Game/CPad.h"
#include "Game/CBall.h"
namespace dd
@@ -102,16 +103,19 @@ private:
void PreSolve(b2Contact* contact, const b2Manifold* oldManifold)
{
EntityID entityA = m_PhysicsSystem->m_BodiesToEntities[contact->GetFixtureA()->GetBody()];
EntityID entityB = m_PhysicsSystem->m_BodiesToEntities[contact->GetFixtureA()->GetBody()];
EntityID entityB = m_PhysicsSystem->m_BodiesToEntities[contact->GetFixtureB()->GetBody()];
auto physicsComponentA = m_PhysicsSystem->m_World->GetComponent<Components::Physics>(entityA);
auto physicsComponentB = m_PhysicsSystem->m_World->GetComponent<Components::Physics>(entityB);
if (physicsComponentA != nullptr || physicsComponentB != nullptr) {
// Turn of collisions
contact->SetEnabled(false);
if (physicsComponentA != nullptr && physicsComponentB != nullptr) {
if (physicsComponentA->Calculate || physicsComponentB->Calculate) {
// Turn of collisions
contact->SetEnabled(false);
}
}
}
void PostSolve(b2Contact* contact, const b2ContactImpulse* impulse)
{
+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));