Implemented boundary and jumping collision detection

This commit is contained in:
Ash Reynolds 2016-02-09 16:37:43 +00:00
parent 9a31d7b078
commit 71f048e3ca
5 changed files with 112 additions and 68 deletions

View file

Before

Width:  |  Height:  |  Size: 6.4 KiB

After

Width:  |  Height:  |  Size: 6.4 KiB

View file

@ -29,24 +29,23 @@ public class Platform extends Base {
FixtureDef fd = new FixtureDef(); FixtureDef fd = new FixtureDef();
/*ChainShape chain = new ChainShape(); // Create the box (platform)
Vector2[] v = new Vector2[3];
v[0] = new Vector2(
-TILESIZE / 2 / PPM, -TILESIZE / 2 / PPM);
v[1] = new Vector2(
-TILESIZE / 2 / PPM, TILESIZE / 2 / PPM);
v[2] = new Vector2(
TILESIZE / 2 / PPM, TILESIZE / 2 / PPM);
chain.createChain(v);
fd.shape = chain;*/
PolygonShape polygon = new PolygonShape(); PolygonShape polygon = new PolygonShape();
polygon.setAsBox((TILESIZE / 2) / PPM, (TILESIZE / 2) / PPM); polygon.setAsBox(TILESIZE / 2 / PPM, TILESIZE / 2 / PPM);
fd.shape = polygon; fd.shape = polygon;
fd.density = 1f;
fd.restitution = 0f;
fd.friction = 1f;
body.createFixture(fd); body.createFixture(fd);
// Create a sensor used for jumping above each box
ChainShape chain = new ChainShape();
Vector2[] v = new Vector2[2];
v[0] = new Vector2(
(-TILESIZE / 2 + 5) / PPM, (TILESIZE / 2 + 5) / PPM);
v[1] = new Vector2(
(TILESIZE / 2 - 5) / PPM, (TILESIZE / 2 + 5) / PPM);
chain.createChain(v);
fd.shape = chain;
fd.isSensor = true;
body.createFixture(fd).setUserData("PLATFORM");
} }
} }

View file

@ -1,9 +1,10 @@
package com.game.Actor; package com.game.Actor;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.*; import com.badlogic.gdx.physics.box2d.*;
import com.game.Misc.Vars; import com.game.Misc.Vars;
import javafx.scene.shape.Circle;
import static com.game.Misc.Vars.PPM; import static com.game.Misc.Vars.PPM;
@ -12,6 +13,9 @@ import static com.game.Misc.Vars.PPM;
*/ */
public class Player extends Base { public class Player extends Base {
// TODO, remove
private Texture texture = new Texture("textures/player.png");
private Action curAction; private Action curAction;
public enum Action public enum Action
{ {
@ -29,35 +33,21 @@ public class Player extends Base {
@Override @Override
public void makeBody() { public void makeBody() {
bd = new BodyDef(); bd = new BodyDef();
bd.type = BodyDef.BodyType.DynamicBody;
if(bodyType.equals("STATIC")) { bd.type = BodyDef.BodyType.StaticBody; } // Doesn't move, isn't affected by forces
else if(bodyType.equals("KINEMATIC")) { bd.type = BodyDef.BodyType.KinematicBody; } // Can move, isn't affected by forces
else { bd.type = BodyDef.BodyType.DynamicBody; } // Can move, is affected by forces
bd.position.set(pos.x / PPM, pos.y / PPM); bd.position.set(pos.x / PPM, pos.y / PPM);
body = world.createBody(bd); body = world.createBody(bd);
//body.setFixedRotation(true);
FixtureDef fd = new FixtureDef(); FixtureDef fd = new FixtureDef();
//PolygonShape polygon = new PolygonShape();
//polygon.setAsBox((size.x / 2) / PPM, (size.y / 2) / PPM);
//fd.shape = polygon;
CircleShape circle = new CircleShape(); CircleShape circle = new CircleShape();
circle.setRadius((size.x / 2) / PPM); circle.setRadius((size.x / 2) / PPM);
fd.shape = circle; fd.shape = circle;
fd.density = 1f; fd.density = 1f;
fd.restitution = 0f;
fd.friction = 0.9f; fd.friction = 0.9f;
body.createFixture(fd).setUserData("player"); body.createFixture(fd).setUserData("PLAYER");
/*PolygonShape polygon = new PolygonShape();
polygon.setAsBox((size.x / 4) / PPM, (size.y / 4) / PPM, new Vector2(0, (size.y / -3) / PPM), 0);
fd.shape = polygon;
fd.isSensor = true;
body.createFixture(fd).setUserData("sensor");*/
} }
public void update(float dt) public void update(float dt)
@ -71,6 +61,16 @@ public class Player extends Base {
Vector2 curVel = body.getLinearVelocity(); Vector2 curVel = body.getLinearVelocity();
curVel.x = Vars.SCROLLSPEED.x * dt; curVel.x = Vars.SCROLLSPEED.x * dt;
body.setLinearVelocity(curVel); body.setLinearVelocity(curVel);
pos = body.getPosition();
}
public void render(SpriteBatch sb)
{
sb.draw(texture,
(pos.x * PPM) - size.x / 2,
(pos.y * PPM) - size.y / 2,
size.x,
size.y);
} }
public void jump() public void jump()
@ -81,20 +81,6 @@ public class Player extends Base {
} }
} }
public void moveLeft()
{
Vector2 vel = body.getLinearVelocity();
vel.x = -5f;
body.setLinearVelocity(vel);
}
public void moveRight()
{
Vector2 vel = body.getLinearVelocity();
vel.x = 5f;
body.setLinearVelocity(vel);
}
// Accessors // Accessors
public Action getCurAction() { return curAction; } public Action getCurAction() { return curAction; }

View file

@ -2,17 +2,11 @@ package com.game;
import com.badlogic.gdx.ApplicationAdapter; import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera; import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.game.Misc.Vars; import com.game.Misc.Vars;
import com.game.States.StateManager; import com.game.States.StateManager;
import javax.swing.*;
import java.awt.*;
public class Game extends ApplicationAdapter { public class Game extends ApplicationAdapter {
private float accum; private float accum;

View file

@ -6,11 +6,16 @@ import com.badlogic.gdx.Input;
import com.badlogic.gdx.audio.Sound; import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera; import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.maps.MapLayer;
import com.badlogic.gdx.maps.objects.PolylineMapObject;
import com.badlogic.gdx.maps.objects.TextureMapObject;
import com.badlogic.gdx.maps.tiled.TiledMap; import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TiledMapTileLayer; import com.badlogic.gdx.maps.tiled.TiledMapTileLayer;
import com.badlogic.gdx.maps.tiled.TmxMapLoader; import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer; import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
import com.badlogic.gdx.math.Polyline;
import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.physics.box2d.*; import com.badlogic.gdx.physics.box2d.*;
import com.game.Actor.Base; import com.game.Actor.Base;
import com.game.Actor.Platform; import com.game.Actor.Platform;
@ -25,7 +30,7 @@ import java.util.ArrayList;
public class Play extends State { public class Play extends State {
// TODO, remove // TODO, remove
public boolean isDebug = true; public boolean isDebug = false;
// Physics related // Physics related
private World world; private World world;
@ -50,7 +55,19 @@ public class Play extends State {
ContactListener cl = new ContactListener() { ContactListener cl = new ContactListener() {
@Override @Override
public void beginContact(Contact contact) { public void beginContact(Contact contact) {
player.setAction(Player.Action.IDLE); Fixture fa = contact.getFixtureA();
Fixture fb = contact.getFixtureB();
if(fa == null || fb == null) { return; }
if(fa.getUserData() == null || fb.getUserData() == null) { return; }
if(fa.getUserData().equals("PLAYER") && fb.getUserData().equals("PLATFORM") ||
fb.getUserData().equals("PLAYER") && fa.getUserData().equals("PLATFORM"))
{
if(player.getCurAction() != Player.Action.IDLE) {
player.setAction(Player.Action.IDLE);
}
}
} }
@Override @Override
@ -84,24 +101,28 @@ public class Play extends State {
@Override @Override
public void update(float dt) { public void update(float dt) {
world.step(dt, 6, 2); world.step(dt, 6, 2);
cam.position.x = player.getPos().x * PPM;
cam.update();
b2dCam.position.x = player.getPos().x; cameraUpdate(dt);
b2dCam.position.x = player.getPos().x / PPM;
b2dCam.update(); b2dCam.update();
player.update(dt); player.update(dt);
} }
@Override @Override
public void render() { public void render() {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
sb.setProjectionMatrix(cam.combined);
if(!isDebug) if(!isDebug)
{ {
tmr.setView(cam); tmr.setView(cam);
tmr.render(); tmr.render();
sb.begin();
player.render(sb);
sb.end();
} }
else else
{ {
@ -113,7 +134,7 @@ public class Play extends State {
public void handleInput() { public void handleInput() {
if(Gdx.input.isKeyPressed(Input.Keys.SPACE)) if(Gdx.input.isKeyPressed(Input.Keys.SPACE))
{ {
jumpSound.play(); //jumpSound.play(); TODO, fix sound?
player.jump(); player.jump();
} }
@ -125,21 +146,37 @@ public class Play extends State {
} }
private void cameraUpdate(float dt)
{
Vector3 camPos = cam.position;
camPos.x = cam.position.x + ((player.getPos().x * PPM) - cam.position.x) * .2f;
camPos.y = cam.position.y + ((player.getPos().y * PPM) - cam.position.y) * .2f;
cam.position.set(camPos);
cam.update();
}
private void setupLevel() private void setupLevel()
{ {
player = new Player(world, new Vector2(500, 500), new Vector2(60, 60), Base.Colours.NONE); tileMap = new TmxMapLoader().load("levels/level1.tmx");
tileMap = new TmxMapLoader().load("levels/level2.tmx");
tmr = new OrthogonalTiledMapRenderer(tileMap); tmr = new OrthogonalTiledMapRenderer(tileMap);
TiledMapTileLayer layer = (TiledMapTileLayer)tileMap.getLayers().get("PLATFORM"); TiledMapTileLayer platformLayer = (TiledMapTileLayer)tileMap.getLayers().get("PLATFORM");
tileSize = layer.getTileWidth(); tileSize = platformLayer.getTileWidth();
for(int row = 0; row < layer.getHeight(); row++) MapLayer boundaryLayer = tileMap.getLayers().get("BOUNDARY");
PolylineMapObject polylineObj = (PolylineMapObject)boundaryLayer.getObjects().get(0);
buildBoundary(polylineObj);
MapLayer playerLayer = tileMap.getLayers().get("PLAYER");
TextureMapObject playerObj = (TextureMapObject)playerLayer.getObjects().get(0);
player = new Player(world, new Vector2(playerObj.getX(), playerObj.getY()), new Vector2(60, 60), Base.Colours.NONE);
for(int row = 0; row < platformLayer.getHeight(); row++)
{ {
for(int col = 0; col < layer.getWidth(); col++) for(int col = 0; col < platformLayer.getWidth(); col++)
{ {
TiledMapTileLayer.Cell cell = layer.getCell(col, row); TiledMapTileLayer.Cell cell = platformLayer.getCell(col, row);
if(cell == null) { continue; } if(cell == null) { continue; }
if(cell.getTile() == null) { continue; } if(cell.getTile() == null) { continue; }
@ -151,6 +188,34 @@ public class Play extends State {
} }
} }
public void buildBoundary(PolylineMapObject polylineObj)
{
Polyline r = polylineObj.getPolyline();
BodyDef bd = new BodyDef();
bd.type = BodyDef.BodyType.StaticBody;
Body body = world.createBody(bd);
FixtureDef fd = new FixtureDef();
ChainShape chain = new ChainShape();
float[] v = r.getTransformedVertices();
Vector2[] finalV = new Vector2[v.length / 2];
for(int i = 0; i < v.length / 2; ++i)
{
finalV[i] = new Vector2();
finalV[i].x = v[i * 2] / PPM;
finalV[i].y = v[i * 2 + 1] / PPM;
}
chain.createChain(finalV);
fd.shape = chain;
body.createFixture(fd).setUserData("boundary");
}
// Accessors // Accessors
// Mutators // Mutators