Permalink
Browse files

Added Box2D

Box2D world init, added helper class to create collision bodies. Each tile around the island now has a collision box.
  • Loading branch information...
tyler6699 committed Aug 3, 2017
1 parent 9d4b05b commit a27df4d40420f5427f988ae85b344d9bd6d9e588
@@ -0,0 +1,34 @@
package uk.co.carelesslabs.box2d;
import com.badlogic.gdx.math.Vector3;
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;
public class Box2DHelper {
public static Body createBox(World world, float width, float height, Vector3 pos, BodyDef.BodyType type) {
Body body;
BodyDef bodyDef = new BodyDef();
bodyDef.position.set(pos.x + width/2, pos.y + height/2);
bodyDef.angle = 0;
bodyDef.fixedRotation = true;
bodyDef.type = type;
body = world.createBody(bodyDef);
FixtureDef fixtureDef = new FixtureDef();
PolygonShape boxShape = new PolygonShape();
boxShape.setAsBox(width / 2, height / 2);
fixtureDef.shape = boxShape;
fixtureDef.restitution = 0.4f;
body.createFixture(fixtureDef);
boxShape.dispose();
return body;
}
}
@@ -0,0 +1,26 @@
package uk.co.carelesslabs.box2d;
import uk.co.carelesslabs.Control;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
import com.badlogic.gdx.physics.box2d.World;
public class Box2DWorld {
public World world;
private Box2DDebugRenderer debugRenderer;
public Box2DWorld(){
world = new World(new Vector2(.0f, .0f), true);
debugRenderer = new Box2DDebugRenderer();
}
public void tick(OrthographicCamera camera, Control control){
if (control.debug) debugRenderer.render(world, camera.combined);
world.step(Gdx.app.getGraphics().getDeltaTime(), 6, 2);
world.clearForces();
}
}
@@ -1,8 +1,11 @@
package uk.co.carelesslabs;
import java.util.ArrayList;
import uk.co.carelesslabs.box2d.Box2DWorld;
import uk.co.carelesslabs.map.Tile;
import uk.co.carelesslabs.map.Island;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
@@ -13,6 +16,7 @@
OrthographicCamera camera;
Control control;
SpriteBatch batch;
Box2DWorld box2D;
// Display Size
private int displayW;
@@ -23,7 +27,7 @@
// Island
Island island;
@Override
public void create() {
Media.load_assets();
@@ -44,7 +48,11 @@ public void create() {
control = new Control(displayW, displayH, camera);
Gdx.input.setInputProcessor(control);
island = new Island();
// Box2D
box2D = new Box2DWorld();
// Island
island = new Island(box2D);
// Hero
hero = new Hero(island.centreTile.pos);
@@ -75,6 +83,8 @@ public void render () {
}
hero.draw(batch);
batch.end();
box2D.tick(camera, control);
}
@Override
@@ -41,7 +41,7 @@ public String getTileCode(int row, int col){
return tile.isGrass() ? "1" : "0";
}
}
return null;
return "0";
}
}
@@ -5,8 +5,11 @@
import uk.co.carelesslabs.Enums.TileType;
import uk.co.carelesslabs.Entity;
import uk.co.carelesslabs.Media;
import uk.co.carelesslabs.box2d.Box2DHelper;
import uk.co.carelesslabs.box2d.Box2DWorld;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
public class Island {
public Tile centreTile;
@@ -33,12 +36,25 @@
String[] aGrassTopRight = {"000000100"};
String[] aGrassTopLeft = {"000000001"};
public Island(){
public Island(Box2DWorld box2D){
setupTiles();
codeTiles();
generateHitboxes(box2D);
}
private void setupTiles(){
private void generateHitboxes(Box2DWorld box2D) {
for(ArrayList<Tile> row : chunk.tiles){
for(Tile tile : row){
if(tile.isNotPassable() && tile.notIsAllWater()){
System.out.println(tile.code);
Box2DHelper.createBox(box2D.world, chunk.tileSize, chunk.tileSize, tile.pos, BodyType.StaticBody);
}
}
}
}
private void setupTiles(){
chunk = new Chunk(33,33, 8);
int currentRow = 0;
@@ -48,4 +48,12 @@ public boolean isPassable() {
public boolean isNotPassable() {
return !isPassable();
}
public boolean isAllWater() {
return code.equals("000000000");
}
public boolean notIsAllWater() {
return !isAllWater();
}
}

0 comments on commit a27df4d

Please sign in to comment.