Refactor entity.PhysicsBody to physics.PhysicsBody

Create CollisionListener interface and Box2dContactListener
Register the Box2dContactListener with the box2d world
This commit is contained in:
Matt Low 2018-11-14 22:26:02 +04:00
parent 0994289c7e
commit 92201411b9
8 changed files with 71 additions and 4 deletions

View File

@ -10,6 +10,7 @@ import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef; 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.physics.PhysicsBody;
import com.me.brickbuster.state.PlayState; import com.me.brickbuster.state.PlayState;
public class Ball extends Entity implements PhysicsBody { public class Ball extends Entity implements PhysicsBody {

View File

@ -9,6 +9,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.PhysicsBody;
import com.me.brickbuster.state.PlayState; import com.me.brickbuster.state.PlayState;
public class Brick extends Entity implements PhysicsBody { public class Brick extends Entity implements PhysicsBody {

View File

@ -10,6 +10,7 @@ import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef; 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.PhysicsBody;
import com.me.brickbuster.state.PlayState; import com.me.brickbuster.state.PlayState;
import net.dermetfan.utils.Pair; import net.dermetfan.utils.Pair;

View File

@ -10,7 +10,7 @@ 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.Entity; 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 com.me.brickbuster.state.PlayState;
import net.dermetfan.utils.Pair; import net.dermetfan.utils.Pair;

View File

@ -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) {}
}

View File

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

View File

@ -1,4 +1,4 @@
package com.me.brickbuster.entity; package com.me.brickbuster.physics;
import com.badlogic.gdx.physics.box2d.Body; import com.badlogic.gdx.physics.box2d.Body;
@ -6,6 +6,4 @@ public interface PhysicsBody {
Body getBody(); Body getBody();
//public void handleCollission(Entity other);
} }

View File

@ -11,6 +11,7 @@ import com.me.brickbuster.entity.Ball;
import com.me.brickbuster.entity.Brick; import com.me.brickbuster.entity.Brick;
import com.me.brickbuster.entity.Paddle; import com.me.brickbuster.entity.Paddle;
import com.me.brickbuster.entity.powerup.*; import com.me.brickbuster.entity.powerup.*;
import com.me.brickbuster.physics.Box2dContactListener;
import java.util.*; import java.util.*;
@ -59,6 +60,7 @@ public class PlayState extends State {
public void setup() { public void setup() {
// Initialize a world with no gravity // Initialize a world with no gravity
world = new World(new Vector2(), false); world = new World(new Vector2(), false);
world.setContactListener(new Box2dContactListener());
// define a playArea body with position set to 0 // define a playArea body with position set to 0
BodyDef playAreaDef = new BodyDef(); BodyDef playAreaDef = new BodyDef();