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

#10 tank sprite now follows box2d body

parent 0da0ce8d
No related branches found
No related tags found
1 merge request!20Resolve "Implement projectile logic"
Pipeline #205142 passed
......@@ -3,36 +3,95 @@ package com.game.tankwars;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Interpolation;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.physics.box2d.Body;
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.ScreenUtils;
import com.game.tankwars.model.Box2dWorld;
import com.game.tankwars.model.Bullet;
import com.game.tankwars.model.Tank;
public class TankWarsGame extends ApplicationAdapter {
public static int VIEWPORT_WIDTH = 100;
public static int VIEWPORT_HEIGHT = 50;
int horizontalScaling;
int verticalScaling;
SpriteBatch batch;
Tank tank;
Box2dWorld model;
World world;
OrthographicCamera cam;
Box2DDebugRenderer debugRenderer;
Bullet bullet;
@Override
public void create () {
batch = new SpriteBatch();
tank = new Tank(new Vector2(50, 50), new Texture("tank-khaki.png"));
model = new Box2dWorld();
world = Box2dWorld.getWorld();
cam = new OrthographicCamera(VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
cam.position.set(VIEWPORT_WIDTH/2, VIEWPORT_HEIGHT/2, 0);
cam.update();
debugRenderer = new Box2DDebugRenderer(true, true, true, true, true, true);
Vector2 tankPos = new Vector2(0, cam.position.y/2);
tank = new Tank(tankPos, new Texture("tank-khaki.png"));
horizontalScaling = Gdx.graphics.getWidth() / VIEWPORT_WIDTH;
verticalScaling = Gdx.graphics.getHeight() / VIEWPORT_HEIGHT;
System.out.println(horizontalScaling);
System.out.println(verticalScaling);
}
@Override
public void render () {
model.logicStep(Gdx.graphics.getDeltaTime());
Gdx.gl.glClearColor(0f, 0f, 0f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
debugRenderer.render(world, cam.combined);
if(Gdx.input.isKeyPressed(Input.Keys.D) && !tank.detectCollisionRight()) {
tank.moveRight();
}
if(Gdx.input.isKeyPressed(Input.Keys.A) && !tank.detectCollisionLeft()) {
else if(Gdx.input.isKeyPressed(Input.Keys.A) && !tank.detectCollisionLeft()) {
tank.moveLeft();
} else {
tank.stop();
}
if(Gdx.input.justTouched()) {
bullet = new Bullet(tank);
System.out.println(tank.getPosition());
bullet.shoot();
}
Array<Body> bodies = new Array<Body>();
world.getBodies(bodies);
for (Body b : bodies) {
Sprite s = (Sprite) b.getUserData();
if (s != null) {
s.setPosition(b.getPosition().x * (float) horizontalScaling, b.getPosition().y * (float) verticalScaling);
}
}
ScreenUtils.clear(0, 0, 0, 1);
//ScreenUtils.clear(0, 0, 0, 1);
batch.begin();
batch.draw(tank.getTexture(), tank.getPosition().x, tank.getPosition().y, Tank.TANK_WIDTH, Tank.TANK_HEIGHT);
tank.getSprite().draw(batch);
batch.end();
}
......
package com.game.tankwars.model;public class Box2dWorld {
package com.game.tankwars.model;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.World;
public class Box2dWorld {
private static final World world = new World(new Vector2(0, -10), true);
public void logicStep(float dt) {
world.step(dt, 3, 3);
}
public static World getWorld() {
return world;
}
}
package com.game.tankwars.model;public class Bullet {
package com.game.tankwars.model;
import com.badlogic.gdx.Gdx;
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.World;
public class Bullet {
BodyDef bodyDef = new BodyDef();
FixtureDef fixtureDef = new FixtureDef();
World world;
Body body;
Tank tank;
Vector2 position;
public Bullet(Tank tank) {
this.position = tank.getPosition();
this.tank = tank;
world = Box2dWorld.getWorld();
bodyDef.type = BodyDef.BodyType.DynamicBody;
bodyDef.position.set(this.position);
body = world.createBody(bodyDef);
CircleShape shape = new CircleShape();
shape.setRadius(0.1f);
fixtureDef.shape = shape;
fixtureDef.density = 0.5f;
fixtureDef.restitution = 0.6f;
fixtureDef.friction = 0.4f;
body.createFixture(fixtureDef);
shape.dispose();
}
public void shoot() {
body.applyLinearImpulse(0.2f, 0.1f, position.x, position.y, true);
System.out.println(this.position);
}
}
......@@ -2,25 +2,52 @@ package com.game.tankwars.model;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.math.Rectangle;
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.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.utils.Array;
import com.game.tankwars.TankWarsGame;
public class Tank {
public static final int TANK_MOVESPEED = 2;
public static final int TANK_WIDTH = 50;
public static final int TANK_HEIGHT = 50;
public static final int TANK_MOVESPEED = 10;
public static final int TANK_WIDTH = 2;
public static final int TANK_HEIGHT = 1;
private Vector2 position;
private Rectangle bounds;
private Texture texture;
private Sprite sprite;
World world;
Body body;
BodyDef bodyDef = new BodyDef();
FixtureDef fixtureDef = new FixtureDef();
public Tank(Vector2 position, Texture texture) {
this.position = position;
this.bounds = new Rectangle(position.x, position.y, TANK_WIDTH, TANK_HEIGHT);
this.texture = texture;
sprite = new Sprite(texture);
sprite.setPosition(TankWarsGame.VIEWPORT_WIDTH/2, TankWarsGame.VIEWPORT_HEIGHT/2 + TANK_HEIGHT);
world = Box2dWorld.getWorld();
bodyDef.type = BodyDef.BodyType.KinematicBody;
bodyDef.position.set(sprite.getX(), sprite.getY());
body = world.createBody(bodyDef);
body.setUserData(sprite);
PolygonShape shape = new PolygonShape();
shape.setAsBox(TANK_WIDTH, TANK_HEIGHT);
fixtureDef.shape = shape;
body.createFixture(fixtureDef);
shape.dispose();
}
public Vector2 getPosition() {
......@@ -35,6 +62,8 @@ public class Tank {
return texture;
}
public Sprite getSprite() {return sprite;}
public void setPosition(Vector2 position) {
this.position = position;
}
......@@ -49,13 +78,15 @@ public class Tank {
public void moveRight() {
position.x += TANK_MOVESPEED;
body.setLinearVelocity(TANK_MOVESPEED, 0);
}
public void moveLeft() {
position.x -= TANK_MOVESPEED;
body.setLinearVelocity(- TANK_MOVESPEED, 0);
}
public void stop() {body.setLinearVelocity(0, 0);}
public boolean detectCollisionLeft() {
return position.x < 0;
}
......@@ -64,4 +95,5 @@ public class Tank {
return Gdx.graphics.getWidth() / 2.0f - bounds.getWidth() / 2.0f - 50 < position.x;
}
}
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