Falling bricks and collision filters
This commit is contained in:
@@ -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
@@ -135,6 +135,9 @@ public:
|
|||||||
ball->Speed = 5.f;
|
ball->Speed = 5.f;
|
||||||
std::shared_ptr<Components::Physics> physics = m_World->AddComponent<Components::Physics>(ent);
|
std::shared_ptr<Components::Physics> physics = m_World->AddComponent<Components::Physics>(ent);
|
||||||
physics->Static = false;
|
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);
|
auto plight = m_World->AddComponent<Components::PointLight>(ent);
|
||||||
plight->Radius = 2.f;
|
plight->Radius = 2.f;
|
||||||
@@ -176,6 +179,8 @@ public:
|
|||||||
|
|
||||||
std::shared_ptr<Components::Physics> physics = m_World->AddComponent<Components::Physics>(topWall);
|
std::shared_ptr<Components::Physics> physics = m_World->AddComponent<Components::Physics>(topWall);
|
||||||
physics->Static = true;
|
physics->Static = true;
|
||||||
|
physics->Category = CollisionLayer::Type::Wall;
|
||||||
|
physics->Mask = CollisionLayer::Type::Ball | CollisionLayer::Type::Brick;
|
||||||
|
|
||||||
m_World->CommitEntity(topWall);
|
m_World->CommitEntity(topWall);
|
||||||
}
|
}
|
||||||
@@ -192,6 +197,8 @@ public:
|
|||||||
|
|
||||||
std::shared_ptr<Components::Physics> physics = m_World->AddComponent<Components::Physics>(leftWall);
|
std::shared_ptr<Components::Physics> physics = m_World->AddComponent<Components::Physics>(leftWall);
|
||||||
physics->Static = true;
|
physics->Static = true;
|
||||||
|
physics->Category = CollisionLayer::Type::Wall;
|
||||||
|
physics->Mask = CollisionLayer::Type::Ball | CollisionLayer::Type::Brick;
|
||||||
|
|
||||||
m_World->CommitEntity(leftWall);
|
m_World->CommitEntity(leftWall);
|
||||||
}
|
}
|
||||||
@@ -208,6 +215,8 @@ public:
|
|||||||
|
|
||||||
std::shared_ptr<Components::Physics> physics = m_World->AddComponent<Components::Physics>(rightWall);
|
std::shared_ptr<Components::Physics> physics = m_World->AddComponent<Components::Physics>(rightWall);
|
||||||
physics->Static = true;
|
physics->Static = true;
|
||||||
|
physics->Category = CollisionLayer::Type::Wall;
|
||||||
|
physics->Mask = CollisionLayer::Type::Ball | CollisionLayer::Type::Brick;
|
||||||
|
|
||||||
m_World->CommitEntity(rightWall);
|
m_World->CommitEntity(rightWall);
|
||||||
}
|
}
|
||||||
@@ -221,8 +230,12 @@ public:
|
|||||||
auto rectangle = m_World->AddComponent<Components::RectangleShape>(ent);
|
auto rectangle = m_World->AddComponent<Components::RectangleShape>(ent);
|
||||||
auto physics = m_World->AddComponent<Components::Physics>(ent);
|
auto physics = m_World->AddComponent<Components::Physics>(ent);
|
||||||
physics->Static = false;
|
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);
|
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);
|
auto pad = m_World->AddComponent<Components::Pad>(ent);
|
||||||
m_World->CommitEntity(ent);
|
m_World->CommitEntity(ent);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,18 +4,40 @@
|
|||||||
|
|
||||||
namespace dd
|
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
|
namespace Components
|
||||||
{
|
{
|
||||||
|
|
||||||
struct Physics: public Component
|
struct Physics: public Component
|
||||||
{
|
{
|
||||||
//idk if anything should be here :)
|
|
||||||
bool Static = true;
|
bool Static = true;
|
||||||
|
bool Calculate = false;
|
||||||
float GravityScale = 0.f;
|
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
|
#endif //DAYDREAM_CPHYSICS_H
|
||||||
|
|||||||
@@ -15,6 +15,7 @@
|
|||||||
#include "Physics/CCircleShape.h"
|
#include "Physics/CCircleShape.h"
|
||||||
#include "Core/EventBroker.h"
|
#include "Core/EventBroker.h"
|
||||||
#include "Game/CPad.h"
|
#include "Game/CPad.h"
|
||||||
|
#include "Game/CBall.h"
|
||||||
|
|
||||||
|
|
||||||
namespace dd
|
namespace dd
|
||||||
@@ -102,16 +103,19 @@ private:
|
|||||||
void PreSolve(b2Contact* contact, const b2Manifold* oldManifold)
|
void PreSolve(b2Contact* contact, const b2Manifold* oldManifold)
|
||||||
{
|
{
|
||||||
EntityID entityA = m_PhysicsSystem->m_BodiesToEntities[contact->GetFixtureA()->GetBody()];
|
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 physicsComponentA = m_PhysicsSystem->m_World->GetComponent<Components::Physics>(entityA);
|
||||||
auto physicsComponentB = m_PhysicsSystem->m_World->GetComponent<Components::Physics>(entityB);
|
auto physicsComponentB = m_PhysicsSystem->m_World->GetComponent<Components::Physics>(entityB);
|
||||||
|
|
||||||
if (physicsComponentA != nullptr || physicsComponentB != nullptr) {
|
if (physicsComponentA != nullptr && physicsComponentB != nullptr) {
|
||||||
// Turn of collisions
|
if (physicsComponentA->Calculate || physicsComponentB->Calculate) {
|
||||||
contact->SetEnabled(false);
|
// Turn of collisions
|
||||||
|
contact->SetEnabled(false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
void PostSolve(b2Contact* contact, const b2ContactImpulse* impulse)
|
void PostSolve(b2Contact* contact, const b2ContactImpulse* impulse)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -60,9 +60,10 @@ bool dd::Systems::BallSystem::Contact(const Events::Contact &event)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Which is the ball?
|
|
||||||
EntityID ballEntity, otherEntitiy;
|
EntityID ballEntity, otherEntitiy;
|
||||||
|
|
||||||
|
//Which is the ball?
|
||||||
if (ballComponent1 != nullptr) {
|
if (ballComponent1 != nullptr) {
|
||||||
ballEntity = event.Entity1;
|
ballEntity = event.Entity1;
|
||||||
otherEntitiy = event.Entity2;
|
otherEntitiy = event.Entity2;
|
||||||
@@ -78,6 +79,8 @@ bool dd::Systems::BallSystem::Contact(const Events::Contact &event)
|
|||||||
//TODO: Add support for power-up collisions
|
//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);
|
auto ballTransform = m_World->GetComponent<Components::Transform>(ballEntity);
|
||||||
glm::vec2 ballVelocity = glm::vec2(ballTransform->Velocity.x, ballTransform->Velocity.y);
|
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);
|
auto padTransform = m_World->GetComponent<Components::Transform>(otherEntitiy);
|
||||||
float x = ballTransform->Position.x - padTransform->Position.x;
|
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;
|
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;
|
ballTransform->Velocity = glm::normalize(glm::vec3(x, y ,0.f)) * ballComponent->Speed;
|
||||||
|
|||||||
@@ -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);
|
std::shared_ptr<Components::Physics> cPhys = m_World->AddComponent<Components::Physics>(brick);
|
||||||
cPhys->Static = false;
|
cPhys->Static = false;
|
||||||
cPhys->GravityScale = 0.f;
|
cPhys->GravityScale = 0.f;
|
||||||
|
cPhys->Category = CollisionLayer::Type::Brick;
|
||||||
|
cPhys->Mask = CollisionLayer::Type::Ball;
|
||||||
|
|
||||||
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");
|
||||||
@@ -270,6 +273,20 @@ bool dd::Systems::LevelSystem::OnContact(const dd::Events::Contact &event)
|
|||||||
|
|
||||||
auto physicsComponent = m_World->GetComponent<Components::Physics>(entityBrick);
|
auto physicsComponent = m_World->GetComponent<Components::Physics>(entityBrick);
|
||||||
physicsComponent->GravityScale = 1.f;
|
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);
|
SetNumberOfBricks(NumberOfBricks() - 1);
|
||||||
if (NumberOfBricks() <= 0) {
|
if (NumberOfBricks() <= 0) {
|
||||||
@@ -279,7 +296,7 @@ bool dd::Systems::LevelSystem::OnContact(const dd::Events::Contact &event)
|
|||||||
es.Score = brick->Score;
|
es.Score = brick->Score;
|
||||||
EventBroker->Publish(es);
|
EventBroker->Publish(es);
|
||||||
|
|
||||||
std::cout << NumberOfBricks() << std::endl;
|
// std::cout << NumberOfBricks() << std::endl;
|
||||||
//std::cout << "Score: " << Score() << 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);
|
auto model = m_World->AddComponent<Components::Model>(powerUp);
|
||||||
std::shared_ptr<Components::CircleShape> cRec = m_World->AddComponent<Components::CircleShape>(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::Physics> cPhys = m_World->AddComponent<Components::Physics>(powerUp);
|
||||||
std::shared_ptr<Components::PowerUp> cPow = m_World->AddComponent<Components::PowerUp>(powerUp);
|
|
||||||
cPhys->Static = false;
|
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->Position = event.Position;
|
||||||
transform->Scale = glm::vec3(0.2, 0.2, 0.2);
|
transform->Scale = glm::vec3(0.2, 0.2, 0.2);
|
||||||
|
|||||||
@@ -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::Ball> cball = m_World->AddComponent<Components::Ball>(ent);
|
||||||
std::shared_ptr<Components::Physics> physics = m_World->AddComponent<Components::Physics>(ent);
|
std::shared_ptr<Components::Physics> physics = m_World->AddComponent<Components::Physics>(ent);
|
||||||
physics->Static = false;
|
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;
|
cball->Speed = 5.f;
|
||||||
|
|
||||||
m_World->CommitEntity(ent);
|
m_World->CommitEntity(ent);
|
||||||
@@ -234,9 +236,9 @@ bool dd::Systems::PadSystem::OnContactPowerUp(const dd::Events::Contact &event)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
m_World->RemoveComponent<Components::PowerUp>(entityPower);
|
// m_World->RemoveComponent<Components::PowerUp>(entityPower);
|
||||||
m_World->RemoveComponent<Components::CircleShape>(entityPower);
|
// m_World->RemoveComponent<Components::CircleShape>(entityPower);
|
||||||
m_World->RemoveComponent<Components::Physics>(entityPower);
|
// m_World->RemoveComponent<Components::Physics>(entityPower);
|
||||||
m_World->RemoveEntity(entityPower);
|
m_World->RemoveEntity(entityPower);
|
||||||
Events::PowerUpTaken ep;
|
Events::PowerUpTaken ep;
|
||||||
ep.Name = "Something";
|
ep.Name = "Something";
|
||||||
|
|||||||
@@ -85,6 +85,12 @@ void dd::Systems::PhysicsSystem::Update(double dt)
|
|||||||
body->SetLinearVelocity(b2Vec2(transformComponent->Velocity.x, transformComponent->Velocity.y));
|
body->SetLinearVelocity(b2Vec2(transformComponent->Velocity.x, transformComponent->Velocity.y));
|
||||||
|
|
||||||
body->SetGravityScale(physicsComponent->GravityScale);
|
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;
|
b2Body* body = i.second;
|
||||||
|
|
||||||
if (body == nullptr) {
|
if (body == nullptr) {
|
||||||
//LOG_ERROR("This body should not exist");
|
LOG_ERROR("This body should not exist");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -149,6 +155,11 @@ void dd::Systems::PhysicsSystem::OnEntityCommit(EntityID entity)
|
|||||||
|
|
||||||
void dd::Systems::PhysicsSystem::OnEntityRemoved(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];
|
b2Body* body = m_EntitiesToBodies[entity];
|
||||||
|
|
||||||
if (body != nullptr) {
|
if (body != nullptr) {
|
||||||
@@ -180,8 +191,6 @@ void dd::Systems::PhysicsSystem::CreateBody(EntityID entity)
|
|||||||
|
|
||||||
b2BodyDef bodyDef;
|
b2BodyDef bodyDef;
|
||||||
bodyDef.position.Set(absoluteTransform.Position.x, absoluteTransform.Position.y);
|
bodyDef.position.Set(absoluteTransform.Position.x, absoluteTransform.Position.y);
|
||||||
|
|
||||||
|
|
||||||
bodyDef.angle = -glm::eulerAngles(absoluteTransform.Orientation).z;
|
bodyDef.angle = -glm::eulerAngles(absoluteTransform.Orientation).z;
|
||||||
|
|
||||||
if (physicsComponent->Static) {
|
if (physicsComponent->Static) {
|
||||||
@@ -191,7 +200,6 @@ void dd::Systems::PhysicsSystem::CreateBody(EntityID entity)
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
b2Body* body = m_PhysicsWorld->CreateBody(&bodyDef);
|
b2Body* body = m_PhysicsWorld->CreateBody(&bodyDef);
|
||||||
|
|
||||||
b2Shape* pShape;
|
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) {
|
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 {
|
else {
|
||||||
//TODO: FIX THIS SHIT INTO COMPONENTS
|
|
||||||
b2FixtureDef fixtureDef;
|
|
||||||
fixtureDef.shape = pShape;
|
fixtureDef.shape = pShape;
|
||||||
fixtureDef.density = 1.f;
|
fixtureDef.density = 1.f;
|
||||||
fixtureDef.restitution = 1.0f;
|
fixtureDef.restitution = 1.0f;
|
||||||
@@ -232,6 +244,7 @@ void dd::Systems::PhysicsSystem::CreateBody(EntityID entity)
|
|||||||
|
|
||||||
delete pShape;
|
delete pShape;
|
||||||
|
|
||||||
|
|
||||||
body->SetGravityScale(physicsComponent->GravityScale);
|
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));
|
||||||
|
|||||||
Reference in New Issue
Block a user