From adebc42c16a431f5966be1953d45e3db788429fe Mon Sep 17 00:00:00 2001 From: Matt Low Date: Thu, 15 Nov 2018 14:22:42 +0400 Subject: [PATCH] Add Box2d collision filtering, allow collecting power ups with balls --- core/src/com/me/brickbuster/entity/Ball.java | 9 +++++---- core/src/com/me/brickbuster/entity/Brick.java | 4 ++++ core/src/com/me/brickbuster/entity/Paddle.java | 5 ++++- core/src/com/me/brickbuster/entity/Shield.java | 4 ++++ .../me/brickbuster/entity/powerup/PowerUp.java | 7 ++++++- .../com/me/brickbuster/physics/EntityType.java | 12 ++++++++++++ core/src/com/me/brickbuster/state/PlayState.java | 15 +++++++++++---- 7 files changed, 46 insertions(+), 10 deletions(-) create mode 100644 core/src/com/me/brickbuster/physics/EntityType.java diff --git a/core/src/com/me/brickbuster/entity/Ball.java b/core/src/com/me/brickbuster/entity/Ball.java index a46170e..79cc6a1 100644 --- a/core/src/com/me/brickbuster/entity/Ball.java +++ b/core/src/com/me/brickbuster/entity/Ball.java @@ -6,11 +6,9 @@ import com.badlogic.gdx.graphics.glutils.ShapeRenderer; import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType; import com.badlogic.gdx.math.MathUtils; import com.badlogic.gdx.math.Vector2; -import com.badlogic.gdx.physics.box2d.Body; -import com.badlogic.gdx.physics.box2d.BodyDef; -import com.badlogic.gdx.physics.box2d.CircleShape; -import com.badlogic.gdx.physics.box2d.FixtureDef; +import com.badlogic.gdx.physics.box2d.*; import com.me.brickbuster.physics.CollisionListener; +import com.me.brickbuster.physics.EntityType; import com.me.brickbuster.physics.PhysicsBody; import com.me.brickbuster.state.PlayState; @@ -85,6 +83,9 @@ public class Ball extends Entity implements PhysicsBody, CollisionListener { ballFixture.friction = 0f; ballFixture.density = 1f; + ballFixture.filter.categoryBits = EntityType.BALL; + ballFixture.filter.maskBits = EntityType.BOUNDARY | EntityType.BRICK | EntityType.PADDLE | EntityType.POWER_UP; + body = state.world.createBody(ballBody); body.createFixture(ballFixture); body.setUserData(this); diff --git a/core/src/com/me/brickbuster/entity/Brick.java b/core/src/com/me/brickbuster/entity/Brick.java index 4dc18c4..a385d02 100644 --- a/core/src/com/me/brickbuster/entity/Brick.java +++ b/core/src/com/me/brickbuster/entity/Brick.java @@ -12,6 +12,7 @@ import com.badlogic.gdx.physics.box2d.FixtureDef; import com.badlogic.gdx.physics.box2d.PolygonShape; import com.me.brickbuster.entity.powerup.PowerUpType; import com.me.brickbuster.physics.CollisionListener; +import com.me.brickbuster.physics.EntityType; import com.me.brickbuster.physics.PhysicsBody; import com.me.brickbuster.state.PlayState; @@ -81,6 +82,9 @@ public class Brick extends Entity implements PhysicsBody, CollisionListener { brickFixture.shape = brickShape; brickFixture.friction = 0f; + brickFixture.filter.categoryBits = EntityType.BRICK; + brickFixture.filter.maskBits = EntityType.BALL; + body = state.world.createBody(brickBody); body.createFixture(brickFixture); body.setUserData(this); diff --git a/core/src/com/me/brickbuster/entity/Paddle.java b/core/src/com/me/brickbuster/entity/Paddle.java index 7373491..8c6aa48 100644 --- a/core/src/com/me/brickbuster/entity/Paddle.java +++ b/core/src/com/me/brickbuster/entity/Paddle.java @@ -12,6 +12,7 @@ import com.badlogic.gdx.physics.box2d.BodyDef; import com.badlogic.gdx.physics.box2d.EdgeShape; import com.badlogic.gdx.physics.box2d.FixtureDef; import com.me.brickbuster.physics.CollisionListener; +import com.me.brickbuster.physics.EntityType; import com.me.brickbuster.physics.PhysicsBody; import com.me.brickbuster.state.PlayState; import net.dermetfan.utils.Pair; @@ -87,7 +88,9 @@ public class Paddle extends Entity implements PhysicsBody { FixtureDef paddleFixture = new FixtureDef(); paddleFixture.shape = paddleShape; - //paddleFixture.isSensor = true; + + paddleFixture.filter.categoryBits = EntityType.PADDLE; + paddleFixture.filter.maskBits = EntityType.BALL | EntityType.POWER_UP; body = state.world.createBody(paddleBody); body.createFixture(paddleFixture); diff --git a/core/src/com/me/brickbuster/entity/Shield.java b/core/src/com/me/brickbuster/entity/Shield.java index 53de221..c78cf22 100644 --- a/core/src/com/me/brickbuster/entity/Shield.java +++ b/core/src/com/me/brickbuster/entity/Shield.java @@ -8,6 +8,7 @@ import com.badlogic.gdx.physics.box2d.BodyDef; import com.badlogic.gdx.physics.box2d.FixtureDef; import com.badlogic.gdx.physics.box2d.PolygonShape; import com.me.brickbuster.entity.powerup.PowerUpType; +import com.me.brickbuster.physics.EntityType; import com.me.brickbuster.physics.PhysicsBody; import com.me.brickbuster.state.PlayState; @@ -53,6 +54,9 @@ public class Shield extends Entity implements PhysicsBody { brickFixture.shape = brickShape; brickFixture.friction = 0f; + brickFixture.filter.categoryBits = EntityType.SHIELD; + brickFixture.filter.maskBits = EntityType.BALL; + body = state.world.createBody(brickBody); body.createFixture(brickFixture); body.setUserData(this); diff --git a/core/src/com/me/brickbuster/entity/powerup/PowerUp.java b/core/src/com/me/brickbuster/entity/powerup/PowerUp.java index 8afbfe4..45f4c88 100644 --- a/core/src/com/me/brickbuster/entity/powerup/PowerUp.java +++ b/core/src/com/me/brickbuster/entity/powerup/PowerUp.java @@ -9,9 +9,11 @@ import com.badlogic.gdx.physics.box2d.BodyDef; import com.badlogic.gdx.physics.box2d.CircleShape; import com.badlogic.gdx.physics.box2d.FixtureDef; import com.me.brickbuster.Utils; +import com.me.brickbuster.entity.Ball; import com.me.brickbuster.entity.Entity; import com.me.brickbuster.entity.Paddle; import com.me.brickbuster.physics.CollisionListener; +import com.me.brickbuster.physics.EntityType; import com.me.brickbuster.physics.PhysicsBody; import com.me.brickbuster.state.PlayState; import net.dermetfan.utils.Pair; @@ -72,6 +74,9 @@ public abstract class PowerUp extends Entity implements PhysicsBody, CollisionLi ballFixture.shape = ballShape; ballFixture.isSensor = true; + ballFixture.filter.categoryBits = EntityType.POWER_UP; + ballFixture.filter.maskBits = EntityType.PADDLE | EntityType.BALL; + body = state.world.createBody(ballBody); body.createFixture(ballFixture); body.setUserData(this); @@ -83,7 +88,7 @@ public abstract class PowerUp extends Entity implements PhysicsBody, CollisionLi @Override public void beginContact(Entity contacted) { - if (contacted instanceof Paddle) { + if (contacted instanceof Paddle || contacted instanceof Ball) { isCaught = true; } } diff --git a/core/src/com/me/brickbuster/physics/EntityType.java b/core/src/com/me/brickbuster/physics/EntityType.java new file mode 100644 index 0000000..298b4ca --- /dev/null +++ b/core/src/com/me/brickbuster/physics/EntityType.java @@ -0,0 +1,12 @@ +package com.me.brickbuster.physics; + +public final class EntityType { + + public static final short BOUNDARY = 0x1; + public static final short BALL = 0x1 << 1; + public static final short BRICK = 0x1 << 2; + public static final short PADDLE = 0x1 << 3; + public static final short POWER_UP = 0x1 << 4; + public static final short SHIELD = 0x1 << 5; + +} diff --git a/core/src/com/me/brickbuster/state/PlayState.java b/core/src/com/me/brickbuster/state/PlayState.java index 9fff855..f0e3dcc 100644 --- a/core/src/com/me/brickbuster/state/PlayState.java +++ b/core/src/com/me/brickbuster/state/PlayState.java @@ -11,6 +11,7 @@ import com.me.brickbuster.entity.*; import com.me.brickbuster.entity.powerup.PowerUp; import com.me.brickbuster.entity.powerup.PowerUpType; import com.me.brickbuster.physics.Box2dContactListener; +import com.me.brickbuster.physics.EntityType; import java.util.Iterator; @@ -68,19 +69,25 @@ public class PlayState extends State { EdgeShape screenEdge = new EdgeShape(); + FixtureDef playAreaFixture = new FixtureDef(); + playAreaFixture.shape = screenEdge; + + playAreaFixture.filter.categoryBits = EntityType.BOUNDARY; + playAreaFixture.filter.maskBits = EntityType.BALL | EntityType.BRICK; + playArea = world.createBody(playAreaDef); // Right edge screenEdge.set(lowerRightCorner, upperRightCorner); - playArea.createFixture(screenEdge, 0f); + playArea.createFixture(playAreaFixture); // Top edge screenEdge.set(upperRightCorner, upperLeftCorner); - playArea.createFixture(screenEdge, 0f); + playArea.createFixture(playAreaFixture); // Left edge screenEdge.set(upperLeftCorner, lowerLeftCorner); - playArea.createFixture(screenEdge, 0f); + playArea.createFixture(playAreaFixture); // Bottom edge //screenEdge.set(lowerLeftCorner, lowerRightCorner); - //playArea.createFixture(screenEdge, 0f); + //playArea.createFixture(playAreaFixture); screenEdge.dispose(); powerUps = new Array();