Implemented boundary and jumping collision detection
This commit is contained in:
parent
9a31d7b078
commit
71f048e3ca
5 changed files with 112 additions and 68 deletions
Before Width: | Height: | Size: 6.4 KiB After Width: | Height: | Size: 6.4 KiB |
|
@ -29,24 +29,23 @@ public class Platform extends Base {
|
|||
|
||||
FixtureDef fd = new FixtureDef();
|
||||
|
||||
/*ChainShape chain = new ChainShape();
|
||||
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;*/
|
||||
// Create the box (platform)
|
||||
PolygonShape polygon = new PolygonShape();
|
||||
polygon.setAsBox((TILESIZE / 2) / PPM, (TILESIZE / 2) / PPM);
|
||||
polygon.setAsBox(TILESIZE / 2 / PPM, TILESIZE / 2 / PPM);
|
||||
fd.shape = polygon;
|
||||
|
||||
fd.density = 1f;
|
||||
fd.restitution = 0f;
|
||||
fd.friction = 1f;
|
||||
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,9 +1,10 @@
|
|||
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.physics.box2d.*;
|
||||
import com.game.Misc.Vars;
|
||||
import javafx.scene.shape.Circle;
|
||||
|
||||
import static com.game.Misc.Vars.PPM;
|
||||
|
||||
|
@ -12,6 +13,9 @@ import static com.game.Misc.Vars.PPM;
|
|||
*/
|
||||
public class Player extends Base {
|
||||
|
||||
// TODO, remove
|
||||
private Texture texture = new Texture("textures/player.png");
|
||||
|
||||
private Action curAction;
|
||||
public enum Action
|
||||
{
|
||||
|
@ -29,35 +33,21 @@ public class Player extends Base {
|
|||
@Override
|
||||
public void makeBody() {
|
||||
bd = new BodyDef();
|
||||
|
||||
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.type = BodyDef.BodyType.DynamicBody;
|
||||
bd.position.set(pos.x / PPM, pos.y / PPM);
|
||||
|
||||
body = world.createBody(bd);
|
||||
//body.setFixedRotation(true);
|
||||
|
||||
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();
|
||||
circle.setRadius((size.x / 2) / PPM);
|
||||
fd.shape = circle;
|
||||
|
||||
fd.density = 1f;
|
||||
fd.restitution = 0f;
|
||||
fd.friction = 0.9f;
|
||||
|
||||
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");*/
|
||||
body.createFixture(fd).setUserData("PLAYER");
|
||||
}
|
||||
|
||||
public void update(float dt)
|
||||
|
@ -71,6 +61,16 @@ public class Player extends Base {
|
|||
Vector2 curVel = body.getLinearVelocity();
|
||||
curVel.x = Vars.SCROLLSPEED.x * dt;
|
||||
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()
|
||||
|
@ -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
|
||||
public Action getCurAction() { return curAction; }
|
||||
|
||||
|
|
|
@ -2,17 +2,11 @@ package com.game;
|
|||
|
||||
import com.badlogic.gdx.ApplicationAdapter;
|
||||
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.Texture;
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||
import com.game.Misc.Vars;
|
||||
import com.game.States.StateManager;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
public class Game extends ApplicationAdapter {
|
||||
|
||||
private float accum;
|
||||
|
|
|
@ -6,11 +6,16 @@ import com.badlogic.gdx.Input;
|
|||
import com.badlogic.gdx.audio.Sound;
|
||||
import com.badlogic.gdx.graphics.GL20;
|
||||
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.TiledMapTileLayer;
|
||||
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
|
||||
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.Vector3;
|
||||
import com.badlogic.gdx.physics.box2d.*;
|
||||
import com.game.Actor.Base;
|
||||
import com.game.Actor.Platform;
|
||||
|
@ -25,7 +30,7 @@ import java.util.ArrayList;
|
|||
public class Play extends State {
|
||||
|
||||
// TODO, remove
|
||||
public boolean isDebug = true;
|
||||
public boolean isDebug = false;
|
||||
|
||||
// Physics related
|
||||
private World world;
|
||||
|
@ -50,7 +55,19 @@ public class Play extends State {
|
|||
ContactListener cl = new ContactListener() {
|
||||
@Override
|
||||
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
|
||||
|
@ -84,24 +101,28 @@ public class Play extends State {
|
|||
@Override
|
||||
public void update(float dt) {
|
||||
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();
|
||||
|
||||
|
||||
player.update(dt);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render() {
|
||||
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
|
||||
sb.setProjectionMatrix(cam.combined);
|
||||
|
||||
if(!isDebug)
|
||||
{
|
||||
tmr.setView(cam);
|
||||
tmr.render();
|
||||
|
||||
sb.begin();
|
||||
player.render(sb);
|
||||
sb.end();
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -113,7 +134,7 @@ public class Play extends State {
|
|||
public void handleInput() {
|
||||
if(Gdx.input.isKeyPressed(Input.Keys.SPACE))
|
||||
{
|
||||
jumpSound.play();
|
||||
//jumpSound.play(); TODO, fix sound?
|
||||
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()
|
||||
{
|
||||
player = new Player(world, new Vector2(500, 500), new Vector2(60, 60), Base.Colours.NONE);
|
||||
|
||||
tileMap = new TmxMapLoader().load("levels/level2.tmx");
|
||||
tileMap = new TmxMapLoader().load("levels/level1.tmx");
|
||||
tmr = new OrthogonalTiledMapRenderer(tileMap);
|
||||
|
||||
TiledMapTileLayer layer = (TiledMapTileLayer)tileMap.getLayers().get("PLATFORM");
|
||||
tileSize = layer.getTileWidth();
|
||||
TiledMapTileLayer platformLayer = (TiledMapTileLayer)tileMap.getLayers().get("PLATFORM");
|
||||
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.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
|
||||
|
||||
// Mutators
|
||||
|
|
Reference in a new issue