Add Box2d collision filtering, allow collecting power ups with balls

This commit is contained in:
Matt Low 2018-11-15 14:22:42 +04:00
parent 74aa10de04
commit adebc42c16
7 changed files with 46 additions and 10 deletions

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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;
}
}

View File

@ -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;
}

View File

@ -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<PowerUp>();