Add Box2d collision filtering, allow collecting power ups with balls
This commit is contained in:
parent
74aa10de04
commit
adebc42c16
@ -6,11 +6,9 @@ import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
|
|||||||
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
|
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
|
||||||
import com.badlogic.gdx.math.MathUtils;
|
import com.badlogic.gdx.math.MathUtils;
|
||||||
import com.badlogic.gdx.math.Vector2;
|
import com.badlogic.gdx.math.Vector2;
|
||||||
import com.badlogic.gdx.physics.box2d.Body;
|
import com.badlogic.gdx.physics.box2d.*;
|
||||||
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.physics.CollisionListener;
|
import com.me.brickbuster.physics.CollisionListener;
|
||||||
|
import com.me.brickbuster.physics.EntityType;
|
||||||
import com.me.brickbuster.physics.PhysicsBody;
|
import com.me.brickbuster.physics.PhysicsBody;
|
||||||
import com.me.brickbuster.state.PlayState;
|
import com.me.brickbuster.state.PlayState;
|
||||||
|
|
||||||
@ -85,6 +83,9 @@ public class Ball extends Entity implements PhysicsBody, CollisionListener {
|
|||||||
ballFixture.friction = 0f;
|
ballFixture.friction = 0f;
|
||||||
ballFixture.density = 1f;
|
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 = state.world.createBody(ballBody);
|
||||||
body.createFixture(ballFixture);
|
body.createFixture(ballFixture);
|
||||||
body.setUserData(this);
|
body.setUserData(this);
|
||||||
|
@ -12,6 +12,7 @@ import com.badlogic.gdx.physics.box2d.FixtureDef;
|
|||||||
import com.badlogic.gdx.physics.box2d.PolygonShape;
|
import com.badlogic.gdx.physics.box2d.PolygonShape;
|
||||||
import com.me.brickbuster.entity.powerup.PowerUpType;
|
import com.me.brickbuster.entity.powerup.PowerUpType;
|
||||||
import com.me.brickbuster.physics.CollisionListener;
|
import com.me.brickbuster.physics.CollisionListener;
|
||||||
|
import com.me.brickbuster.physics.EntityType;
|
||||||
import com.me.brickbuster.physics.PhysicsBody;
|
import com.me.brickbuster.physics.PhysicsBody;
|
||||||
import com.me.brickbuster.state.PlayState;
|
import com.me.brickbuster.state.PlayState;
|
||||||
|
|
||||||
@ -81,6 +82,9 @@ public class Brick extends Entity implements PhysicsBody, CollisionListener {
|
|||||||
brickFixture.shape = brickShape;
|
brickFixture.shape = brickShape;
|
||||||
brickFixture.friction = 0f;
|
brickFixture.friction = 0f;
|
||||||
|
|
||||||
|
brickFixture.filter.categoryBits = EntityType.BRICK;
|
||||||
|
brickFixture.filter.maskBits = EntityType.BALL;
|
||||||
|
|
||||||
body = state.world.createBody(brickBody);
|
body = state.world.createBody(brickBody);
|
||||||
body.createFixture(brickFixture);
|
body.createFixture(brickFixture);
|
||||||
body.setUserData(this);
|
body.setUserData(this);
|
||||||
|
@ -12,6 +12,7 @@ import com.badlogic.gdx.physics.box2d.BodyDef;
|
|||||||
import com.badlogic.gdx.physics.box2d.EdgeShape;
|
import com.badlogic.gdx.physics.box2d.EdgeShape;
|
||||||
import com.badlogic.gdx.physics.box2d.FixtureDef;
|
import com.badlogic.gdx.physics.box2d.FixtureDef;
|
||||||
import com.me.brickbuster.physics.CollisionListener;
|
import com.me.brickbuster.physics.CollisionListener;
|
||||||
|
import com.me.brickbuster.physics.EntityType;
|
||||||
import com.me.brickbuster.physics.PhysicsBody;
|
import com.me.brickbuster.physics.PhysicsBody;
|
||||||
import com.me.brickbuster.state.PlayState;
|
import com.me.brickbuster.state.PlayState;
|
||||||
import net.dermetfan.utils.Pair;
|
import net.dermetfan.utils.Pair;
|
||||||
@ -87,7 +88,9 @@ public class Paddle extends Entity implements PhysicsBody {
|
|||||||
|
|
||||||
FixtureDef paddleFixture = new FixtureDef();
|
FixtureDef paddleFixture = new FixtureDef();
|
||||||
paddleFixture.shape = paddleShape;
|
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 = state.world.createBody(paddleBody);
|
||||||
body.createFixture(paddleFixture);
|
body.createFixture(paddleFixture);
|
||||||
|
@ -8,6 +8,7 @@ import com.badlogic.gdx.physics.box2d.BodyDef;
|
|||||||
import com.badlogic.gdx.physics.box2d.FixtureDef;
|
import com.badlogic.gdx.physics.box2d.FixtureDef;
|
||||||
import com.badlogic.gdx.physics.box2d.PolygonShape;
|
import com.badlogic.gdx.physics.box2d.PolygonShape;
|
||||||
import com.me.brickbuster.entity.powerup.PowerUpType;
|
import com.me.brickbuster.entity.powerup.PowerUpType;
|
||||||
|
import com.me.brickbuster.physics.EntityType;
|
||||||
import com.me.brickbuster.physics.PhysicsBody;
|
import com.me.brickbuster.physics.PhysicsBody;
|
||||||
import com.me.brickbuster.state.PlayState;
|
import com.me.brickbuster.state.PlayState;
|
||||||
|
|
||||||
@ -53,6 +54,9 @@ public class Shield extends Entity implements PhysicsBody {
|
|||||||
brickFixture.shape = brickShape;
|
brickFixture.shape = brickShape;
|
||||||
brickFixture.friction = 0f;
|
brickFixture.friction = 0f;
|
||||||
|
|
||||||
|
brickFixture.filter.categoryBits = EntityType.SHIELD;
|
||||||
|
brickFixture.filter.maskBits = EntityType.BALL;
|
||||||
|
|
||||||
body = state.world.createBody(brickBody);
|
body = state.world.createBody(brickBody);
|
||||||
body.createFixture(brickFixture);
|
body.createFixture(brickFixture);
|
||||||
body.setUserData(this);
|
body.setUserData(this);
|
||||||
|
@ -9,9 +9,11 @@ import com.badlogic.gdx.physics.box2d.BodyDef;
|
|||||||
import com.badlogic.gdx.physics.box2d.CircleShape;
|
import com.badlogic.gdx.physics.box2d.CircleShape;
|
||||||
import com.badlogic.gdx.physics.box2d.FixtureDef;
|
import com.badlogic.gdx.physics.box2d.FixtureDef;
|
||||||
import com.me.brickbuster.Utils;
|
import com.me.brickbuster.Utils;
|
||||||
|
import com.me.brickbuster.entity.Ball;
|
||||||
import com.me.brickbuster.entity.Entity;
|
import com.me.brickbuster.entity.Entity;
|
||||||
import com.me.brickbuster.entity.Paddle;
|
import com.me.brickbuster.entity.Paddle;
|
||||||
import com.me.brickbuster.physics.CollisionListener;
|
import com.me.brickbuster.physics.CollisionListener;
|
||||||
|
import com.me.brickbuster.physics.EntityType;
|
||||||
import com.me.brickbuster.physics.PhysicsBody;
|
import com.me.brickbuster.physics.PhysicsBody;
|
||||||
import com.me.brickbuster.state.PlayState;
|
import com.me.brickbuster.state.PlayState;
|
||||||
import net.dermetfan.utils.Pair;
|
import net.dermetfan.utils.Pair;
|
||||||
@ -72,6 +74,9 @@ public abstract class PowerUp extends Entity implements PhysicsBody, CollisionLi
|
|||||||
ballFixture.shape = ballShape;
|
ballFixture.shape = ballShape;
|
||||||
ballFixture.isSensor = true;
|
ballFixture.isSensor = true;
|
||||||
|
|
||||||
|
ballFixture.filter.categoryBits = EntityType.POWER_UP;
|
||||||
|
ballFixture.filter.maskBits = EntityType.PADDLE | EntityType.BALL;
|
||||||
|
|
||||||
body = state.world.createBody(ballBody);
|
body = state.world.createBody(ballBody);
|
||||||
body.createFixture(ballFixture);
|
body.createFixture(ballFixture);
|
||||||
body.setUserData(this);
|
body.setUserData(this);
|
||||||
@ -83,7 +88,7 @@ public abstract class PowerUp extends Entity implements PhysicsBody, CollisionLi
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void beginContact(Entity contacted) {
|
public void beginContact(Entity contacted) {
|
||||||
if (contacted instanceof Paddle) {
|
if (contacted instanceof Paddle || contacted instanceof Ball) {
|
||||||
isCaught = true;
|
isCaught = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
12
core/src/com/me/brickbuster/physics/EntityType.java
Normal file
12
core/src/com/me/brickbuster/physics/EntityType.java
Normal 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;
|
||||||
|
|
||||||
|
}
|
@ -11,6 +11,7 @@ import com.me.brickbuster.entity.*;
|
|||||||
import com.me.brickbuster.entity.powerup.PowerUp;
|
import com.me.brickbuster.entity.powerup.PowerUp;
|
||||||
import com.me.brickbuster.entity.powerup.PowerUpType;
|
import com.me.brickbuster.entity.powerup.PowerUpType;
|
||||||
import com.me.brickbuster.physics.Box2dContactListener;
|
import com.me.brickbuster.physics.Box2dContactListener;
|
||||||
|
import com.me.brickbuster.physics.EntityType;
|
||||||
|
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
|
||||||
@ -68,19 +69,25 @@ public class PlayState extends State {
|
|||||||
|
|
||||||
EdgeShape screenEdge = new EdgeShape();
|
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);
|
playArea = world.createBody(playAreaDef);
|
||||||
// Right edge
|
// Right edge
|
||||||
screenEdge.set(lowerRightCorner, upperRightCorner);
|
screenEdge.set(lowerRightCorner, upperRightCorner);
|
||||||
playArea.createFixture(screenEdge, 0f);
|
playArea.createFixture(playAreaFixture);
|
||||||
// Top edge
|
// Top edge
|
||||||
screenEdge.set(upperRightCorner, upperLeftCorner);
|
screenEdge.set(upperRightCorner, upperLeftCorner);
|
||||||
playArea.createFixture(screenEdge, 0f);
|
playArea.createFixture(playAreaFixture);
|
||||||
// Left edge
|
// Left edge
|
||||||
screenEdge.set(upperLeftCorner, lowerLeftCorner);
|
screenEdge.set(upperLeftCorner, lowerLeftCorner);
|
||||||
playArea.createFixture(screenEdge, 0f);
|
playArea.createFixture(playAreaFixture);
|
||||||
// Bottom edge
|
// Bottom edge
|
||||||
//screenEdge.set(lowerLeftCorner, lowerRightCorner);
|
//screenEdge.set(lowerLeftCorner, lowerRightCorner);
|
||||||
//playArea.createFixture(screenEdge, 0f);
|
//playArea.createFixture(playAreaFixture);
|
||||||
screenEdge.dispose();
|
screenEdge.dispose();
|
||||||
|
|
||||||
powerUps = new Array<PowerUp>();
|
powerUps = new Array<PowerUp>();
|
||||||
|
Loading…
Reference in New Issue
Block a user