From 92201411b97f5ae01c52cdb6426ea573a29cfd6c Mon Sep 17 00:00:00 2001 From: Matt Low Date: Wed, 14 Nov 2018 22:26:02 +0400 Subject: [PATCH] Refactor entity.PhysicsBody to physics.PhysicsBody Create CollisionListener interface and Box2dContactListener Register the Box2dContactListener with the box2d world --- core/src/com/me/brickbuster/entity/Ball.java | 1 + core/src/com/me/brickbuster/entity/Brick.java | 1 + .../src/com/me/brickbuster/entity/Paddle.java | 1 + .../brickbuster/entity/powerup/PowerUp.java | 2 +- .../physics/Box2dContactListener.java | 53 +++++++++++++++++++ .../physics/CollisionListener.java | 11 ++++ .../{entity => physics}/PhysicsBody.java | 4 +- .../com/me/brickbuster/state/PlayState.java | 2 + 8 files changed, 71 insertions(+), 4 deletions(-) create mode 100644 core/src/com/me/brickbuster/physics/Box2dContactListener.java create mode 100644 core/src/com/me/brickbuster/physics/CollisionListener.java rename core/src/com/me/brickbuster/{entity => physics}/PhysicsBody.java (54%) diff --git a/core/src/com/me/brickbuster/entity/Ball.java b/core/src/com/me/brickbuster/entity/Ball.java index abf8041..0a9939d 100644 --- a/core/src/com/me/brickbuster/entity/Ball.java +++ b/core/src/com/me/brickbuster/entity/Ball.java @@ -10,6 +10,7 @@ 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.me.brickbuster.physics.PhysicsBody; import com.me.brickbuster.state.PlayState; public class Ball extends Entity implements PhysicsBody { diff --git a/core/src/com/me/brickbuster/entity/Brick.java b/core/src/com/me/brickbuster/entity/Brick.java index 98fb682..bf79f92 100644 --- a/core/src/com/me/brickbuster/entity/Brick.java +++ b/core/src/com/me/brickbuster/entity/Brick.java @@ -9,6 +9,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.PhysicsBody; import com.me.brickbuster.state.PlayState; public class Brick extends Entity implements PhysicsBody { diff --git a/core/src/com/me/brickbuster/entity/Paddle.java b/core/src/com/me/brickbuster/entity/Paddle.java index 05e8bf1..57f05a7 100644 --- a/core/src/com/me/brickbuster/entity/Paddle.java +++ b/core/src/com/me/brickbuster/entity/Paddle.java @@ -10,6 +10,7 @@ import com.badlogic.gdx.physics.box2d.Body; 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.PhysicsBody; import com.me.brickbuster.state.PlayState; import net.dermetfan.utils.Pair; diff --git a/core/src/com/me/brickbuster/entity/powerup/PowerUp.java b/core/src/com/me/brickbuster/entity/powerup/PowerUp.java index b98d672..78dba27 100644 --- a/core/src/com/me/brickbuster/entity/powerup/PowerUp.java +++ b/core/src/com/me/brickbuster/entity/powerup/PowerUp.java @@ -10,7 +10,7 @@ import com.badlogic.gdx.physics.box2d.CircleShape; import com.badlogic.gdx.physics.box2d.FixtureDef; import com.me.brickbuster.Utils; import com.me.brickbuster.entity.Entity; -import com.me.brickbuster.entity.PhysicsBody; +import com.me.brickbuster.physics.PhysicsBody; import com.me.brickbuster.state.PlayState; import net.dermetfan.utils.Pair; diff --git a/core/src/com/me/brickbuster/physics/Box2dContactListener.java b/core/src/com/me/brickbuster/physics/Box2dContactListener.java new file mode 100644 index 0000000..a169540 --- /dev/null +++ b/core/src/com/me/brickbuster/physics/Box2dContactListener.java @@ -0,0 +1,53 @@ +package com.me.brickbuster.physics; + +import com.badlogic.gdx.physics.box2d.Contact; +import com.badlogic.gdx.physics.box2d.ContactImpulse; +import com.badlogic.gdx.physics.box2d.ContactListener; +import com.badlogic.gdx.physics.box2d.Manifold; +import com.me.brickbuster.entity.Entity; + +public class Box2dContactListener implements ContactListener { + + @Override + public void beginContact(Contact contact) { + Object userDataA = contact.getFixtureA().getBody().getUserData(); + Object userDataB = contact.getFixtureB().getBody().getUserData(); + + if (userDataA == null || userDataB == null || + !(userDataA instanceof Entity) || !(userDataB instanceof Entity)) { + return; + } + + if (userDataA instanceof CollisionListener) { + ((CollisionListener) userDataA).beginContact((Entity) userDataB); + } + if (userDataB instanceof CollisionListener) { + ((CollisionListener) userDataB).beginContact((Entity) userDataA); + } + } + + @Override + public void endContact(Contact contact) { + Object userDataA = contact.getFixtureA().getBody().getUserData(); + Object userDataB = contact.getFixtureB().getBody().getUserData(); + + if (userDataA == null || userDataB == null || + !(userDataA instanceof Entity) || !(userDataB instanceof Entity)) { + return; + } + + if (userDataA instanceof CollisionListener) { + ((CollisionListener) userDataA).endContact((Entity) userDataB); + } + if (userDataB instanceof CollisionListener) { + ((CollisionListener) userDataB).endContact((Entity) userDataA); + } + } + + @Override + public void preSolve(Contact contact, Manifold oldManifold) {} + + @Override + public void postSolve(Contact contact, ContactImpulse impulse) {} + +} diff --git a/core/src/com/me/brickbuster/physics/CollisionListener.java b/core/src/com/me/brickbuster/physics/CollisionListener.java new file mode 100644 index 0000000..a63d2b4 --- /dev/null +++ b/core/src/com/me/brickbuster/physics/CollisionListener.java @@ -0,0 +1,11 @@ +package com.me.brickbuster.physics; + +import com.me.brickbuster.entity.Entity; + +public interface CollisionListener { + + void beginContact(Entity contacted); + + void endContact(Entity contacted); + +} diff --git a/core/src/com/me/brickbuster/entity/PhysicsBody.java b/core/src/com/me/brickbuster/physics/PhysicsBody.java similarity index 54% rename from core/src/com/me/brickbuster/entity/PhysicsBody.java rename to core/src/com/me/brickbuster/physics/PhysicsBody.java index bab3c0a..c1059f7 100644 --- a/core/src/com/me/brickbuster/entity/PhysicsBody.java +++ b/core/src/com/me/brickbuster/physics/PhysicsBody.java @@ -1,4 +1,4 @@ -package com.me.brickbuster.entity; +package com.me.brickbuster.physics; import com.badlogic.gdx.physics.box2d.Body; @@ -6,6 +6,4 @@ public interface PhysicsBody { Body getBody(); - //public void handleCollission(Entity other); - } diff --git a/core/src/com/me/brickbuster/state/PlayState.java b/core/src/com/me/brickbuster/state/PlayState.java index af2d2e9..57c7fc9 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.Ball; import com.me.brickbuster.entity.Brick; import com.me.brickbuster.entity.Paddle; import com.me.brickbuster.entity.powerup.*; +import com.me.brickbuster.physics.Box2dContactListener; import java.util.*; @@ -59,6 +60,7 @@ public class PlayState extends State { public void setup() { // Initialize a world with no gravity world = new World(new Vector2(), false); + world.setContactListener(new Box2dContactListener()); // define a playArea body with position set to 0 BodyDef playAreaDef = new BodyDef();