Skip to content
Snippets Groups Projects
Commit 1ffac2c5 authored by Eli Fjellbirkeland Johannesen's avatar Eli Fjellbirkeland Johannesen
Browse files

Resolve "implement collision detection"

parent 605babd3
No related branches found
No related tags found
1 merge request!55Resolve "implement collision detection"
package com.game.tankwars;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
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.Fixture;
import com.badlogic.gdx.physics.box2d.Manifold;
import com.badlogic.gdx.physics.box2d.World;
import com.game.tankwars.model.Box2dWorld;
import com.game.tankwars.model.FixtureData;
public class CollisionDetection implements ContactListener {
World world;
public CollisionDetection() {
this.world = Box2dWorld.getWorld();
}
@Override
public void beginContact(Contact contact) {
Fixture fixA = contact.getFixtureA();
Fixture fixB = contact.getFixtureB();
FixtureData dataA = (FixtureData) fixA.getUserData();
FixtureData dataB = (FixtureData) fixB.getUserData();
if (dataA.getId().equals("userTank") && dataB.getId().equals("bullet")) {
System.out.println("The bullet has hit the player's tank");
dataB.hasCollided();
}
else if (dataA.getId().equals("opponentTank") && dataB.getId().equals("bullet")) {
System.out.println("The bullet has hit the opponent's tank");
dataB.hasCollided();
}
else if (dataA.getId().equals("terrain") && dataB.getId().equals("bullet")) {
System.out.println("The bullet has hit the ground!");
dataB.hasCollided();
}
}
@Override
public void endContact(Contact contact) {
}
@Override
public void preSolve(Contact contact, Manifold oldManifold) {
}
@Override
public void postSolve(Contact contact, ContactImpulse impulse) {
}
}
package com.game.tankwars.model;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.utils.Array;
public class Box2dWorld {
private Array<Body> deadBodies = new Array<>();
private static final World world = new World(new Vector2(0, -10), true);
public void logicStep(float dt) {
......@@ -15,5 +19,18 @@ public class Box2dWorld {
return world;
}
public void addDeadBody(Body body) {
deadBodies.add(body);
}
public void destroyDeadBodies() {
if (deadBodies.size > 0) {
for (Body b : deadBodies) {
world.destroyBody(b);
}
deadBodies.clear();
}
}
}
......@@ -24,6 +24,7 @@ public class Bullet {
private Sprite bulletSprite;
private float angle;
private boolean collision = false;
public Bullet(Tank tank) {
this.tank = tank;
......@@ -40,6 +41,7 @@ public class Bullet {
fixtureDef.restitution = 0.0f;
fixtureDef.friction = 1f;
body.createFixture(fixtureDef);
body.getFixtureList().get(0).setUserData(new FixtureData("bullet"));
shape.dispose();
......@@ -82,6 +84,12 @@ public class Bullet {
public Body getBody() { return body; }
public boolean collision() {
return collision;
}
public void setCollision() {
collision = true;
}
}
package com.game.tankwars.model;
public class FixtureData {
String id;
Boolean collision;
public FixtureData(String id) {
this.id = id;
this.collision = false;
}
public void hasCollided() {
this.collision = true;
}
public boolean isHit() {
return collision ? true : false;
}
public String getId() {
return id;
}
}
......@@ -45,7 +45,7 @@ public class Tank {
int fuel = 150;
int health = 100;
public Tank(int posInVertArr, Texture chassisTexture, Texture cannonTexture, Terrain terrain, TankWarsGame tankWarsGame, boolean directionLeft, float cannonAngle) {
public Tank(int posInVertArr, Texture chassisTexture, Texture cannonTexture, Terrain terrain, TankWarsGame tankWarsGame, boolean directionLeft, float cannonAngle, String id) {
VIEWPORT_HEIGHT = tankWarsGame.getViewportHeight();
VIEWPORT_WIDTH = tankWarsGame.getViewportWidth();
......@@ -80,6 +80,7 @@ public class Tank {
chassis = world.createBody(bodyDef);
chassis.setUserData(chassisSprite);
chassis.createFixture(fixtureDef);
chassis.getFixtureList().get(0).setUserData(new FixtureData(id));
chassisSprite.scale(0.2f);
//Cannon
......
......@@ -43,6 +43,7 @@ public class Terrain {
body = world.createBody(bodyDef);
body.createFixture(fixtureDef);
body.getFixtureList().get(0).setUserData(new FixtureData("terrain"));
chainShape.dispose();
}
......
......@@ -19,10 +19,12 @@ import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.game.tankwars.CollisionDetection;
import com.game.tankwars.TankWarsGame;
import com.game.tankwars.controller.GameController;
import com.game.tankwars.model.Box2dWorld;
import com.game.tankwars.model.Bullet;
import com.game.tankwars.model.FixtureData;
import com.game.tankwars.model.Tank;
import com.game.tankwars.model.Terrain;
......@@ -46,8 +48,10 @@ public class GameScreen implements Screen {
OrthographicCamera hudCam;
Box2DDebugRenderer debugRenderer;
GameController controller;
CollisionDetection collisionDetection;
private Bullet bullet;
private boolean bulletToDestroy = false;
public GameScreen(final TankWarsGame tankWarsGame){
this.tankWarsGame = tankWarsGame;
......@@ -61,6 +65,9 @@ public class GameScreen implements Screen {
model = new Box2dWorld();
world = Box2dWorld.getWorld();
collisionDetection = new CollisionDetection();
world.setContactListener(collisionDetection);
worldCam = new OrthographicCamera(scale(TankWarsGame.GAMEPORT_WIDTH), scale(TankWarsGame.GAMEPORT_HEIGHT));
worldCam.position.set(scale(TankWarsGame.GAMEPORT_WIDTH)/2, scale(TankWarsGame.GAMEPORT_HEIGHT)/2, 0);
......@@ -82,14 +89,16 @@ public class GameScreen implements Screen {
new Texture("camo-tank-barrel.png"),
terrain,
tankWarsGame, true,
120);
120,
"userTank");
opponentTank = new Tank(opponentPos,
new Texture("camo-tank-1.png"),
new Texture("camo-tank-barrel.png"),
terrain,
tankWarsGame,
false,
225);
225,
"opponentTank");
horizontalScaling = Gdx.graphics.getWidth() / TankWarsGame.GAMEPORT_WIDTH;
verticalScaling = Gdx.graphics.getHeight() / TankWarsGame.GAMEPORT_HEIGHT;
......@@ -104,6 +113,7 @@ public class GameScreen implements Screen {
@Override
public void render(float delta) {
model.logicStep(Gdx.graphics.getDeltaTime());
model.destroyDeadBodies();
Gdx.gl.glClearColor(0, 0, 100, 100);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
debugRenderer.render(world, worldCam.combined);
......@@ -146,10 +156,17 @@ public class GameScreen implements Screen {
terrain.draw(shapeRender);
shapeRender.end();
updateBullet();
if (checkBulletCollision()) {
Body body = this.bullet.getBody();
bullet.getBulletSprite().getTexture().dispose();
this.bullet = null;
model.addDeadBody(body);
System.out.println("Destroy body!");
}
batch.begin();
if (bullet != (null)) {
bullet.getBulletSprite().draw(batch);
if (this.bullet != (null)) {
this.bullet.getBulletSprite().draw(batch);
}
myTank.getChassisSprite().draw(batch);
myTank.getCannonSprite().draw(batch);
......@@ -204,4 +221,17 @@ public class GameScreen implements Screen {
this.bullet = bullet;
}
}
private boolean checkBulletCollision() {
if (this.bullet == null) {
return false;
}
try {
FixtureData data = (FixtureData) bullet.getBody().getFixtureList().get(0).getUserData();
return data.isHit();
} catch (IndexOutOfBoundsException e){
return false;
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment