I’ve spent the past week trying to sort this out, but using my knowledge from videos and tutorials I still can’t seem to figure this out. I finally got collision working but my player always gets stuck when it’s colliding and moving at the same time. For some reason, the top and right side of the player works just fine, but the other sides get’s stuck and glitchy.
Variables
//Sprites
SpriteBatch batch;
ShapeRenderer sr;
Sprite jandarUp;
Sprite jandarDown;
Sprite jandarLeft;
Sprite jandarRight;
//Tiled
TmxMapLoader tmap;
TiledMap map;
OrthogonalTiledMapRenderer mapRenderer;
OrthographicCamera camera;
String mapName = "maps/Woodward Village.tmx";
TiledMapTileLayer colLayer;
//Rendering Mech.
float delta = 1/60f;
boolean isPaused = false;
//Character
boolean down = true;
boolean up = false;
boolean left = false;
boolean right = false;
//
float velX = 0;
float velY = 0;
private float speed = 60 * 2;
COLLISION METHODS
public void checkCol() {
float oldX = camera.position.x;
float oldY = camera.position.y;
boolean colX = false;
boolean colY = false;
if(velX < 0) // going left
colX = collidesLeft();
else if(velX > 0) // going right
colX = collidesRight();
// react to x collision
if(colX) {
camera.position.x = oldX;
if(velX > 0) {
camera.position.x-= 0.1f;
velX = 0;
}
if(velX < 0) {
camera.position.x+= 0.1f;
velX = 0;
}
}
if(velY < 0) // going down
colY = collidesBottom();
else if(velY > 0) // going up
colY = collidesTop();
// react to y collision
if(colY) {
camera.position.y = oldY;
if(velY > 0) {
camera.position.y-= 0.1f;
velY = 0;
}
if(velY < 0) {
camera.position.y+= 0.1f;
velY = 0;
}
}
System.out.println("COLY" + colY + "COLX" + colX);
}
public boolean collidesRight() {
for(float step = 0; step < getHeight(); step += colLayer.getTileHeight() / 2)
if(isCellBlocked(getX() + getWidth(), getY() + step))
return true;
return false;
}
public boolean collidesLeft() {
for(float step = 0; step < getHeight(); step += colLayer.getTileHeight() )
if(isCellBlocked(getX(), getY() + step))
return true;
return false;
}
public boolean collidesTop() {
for(float step = 0; step < getWidth(); step += colLayer.getTileWidth() / 2)
if(isCellBlocked(getX() + step, getY() + getHeight()))
return true;
return false;
}
public boolean collidesBottom() {
for(float step = 0; step < getWidth(); step += colLayer.getTileWidth() / 2)
if(isCellBlocked(getX() + step, getY()))
return true;
return false;
}
private boolean isCellBlocked(float x, float y) {
Cell cell = colLayer.getCell((int) (x / colLayer.getTileWidth()), (int) (y / colLayer.getTileHeight()));
return cell != null && cell.getTile() != null && cell.getTile().getProperties().containsKey("blocked");
}
public float getHeight() {
return jandarUp.getHeight();
}
public float getWidth() {
return jandarUp.getWidth();
}
public float getX() {return camera.position.x;}
public float getY() {return camera.position.y;}
INIT METHODS
void initMusic() {
}
void initImages() {
camera = new OrthographicCamera();
camera.setToOrtho(false, Gdx.graphics.getWidth()/2, Gdx.graphics.getHeight()/2);
batch = new SpriteBatch();
map = new TmxMapLoader().load(mapName);
mapRenderer = new OrthogonalTiledMapRenderer(map);
mapRenderer.setView(camera);
batch = new SpriteBatch();
camera.position.set(16, 16*20, 0);
colLayer = new TiledMapTileLayer(0, 0, 0, 0);
colLayer = (TiledMapTileLayer) map.getLayers().get(0);
jandarUp = new Sprite(new Texture(Gdx.files.internal("characters/player/JandarUp.png")));
jandarDown = new Sprite(new Texture(Gdx.files.internal("characters/player/JandarDown.png")));
jandarLeft = new Sprite(new Texture(Gdx.files.internal("characters/player/JandarLeft.png")));
jandarRight = new Sprite(new Texture(Gdx.files.internal("characters/player/JandarRight.png")));
}
SHOW
public void show() {
initMusic();
initImages();
}
UPDATE
public void update(float delta) {
if(!isPaused){
System.out.println(camera.position.x + " " + camera.position.y);
if(velY > speed)
velY = speed;
else if(velY < -speed)
velY = -speed;
moveCam();
checkCol();
}
}
Render method
public void render(float delta) {
if(!isPaused) {
Gdx.gl.glClearColor(0F, 0F, 0F, 0F);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
update(delta);
camera.update();
mapRenderer.setView(camera);
batch.setProjectionMatrix(camera.combined);
mapRenderer.render();
jandarUp.setX(jandarUp.getX() + velX * delta);
jandarUp.setY(jandarUp.getY() + velY * delta);
camera.position.set(jandarUp.getX() + jandarUp.getWidth() / 2, jandarUp.getY() + jandarUp.getHeight() / 2, 0);
batch.begin();
//Player
if(up)
batch.draw(jandarUp, camera.position.x, camera.position.y);
if(down)
batch.draw(jandarDown, camera.position.x, camera.position.y);
if(left)
batch.draw(jandarLeft, camera.position.x, camera.position.y);
if(right)
batch.draw(jandarRight, camera.position.x, camera.position.y);
batch.end();
}
}
MOVEMENT
public void moveCam() {
//UP - W
if(Gdx.input.isKeyPressed(Keys.W)) {
velY = speed;
//camera.position.set(camera.position.x, camera.position.y + 1, camera.position.z);
up = true;
down = false;
left = false;
right = false;
}
//LEFT - A
if(Gdx.input.isKeyPressed(Keys.A)) {
velX = -speed;
//camera.position.set(camera.position.x - 1, camera.position.y, camera.position.z);
up = false;
down = false;
left = true;
right = false;
}
if(Gdx.input.isKeyPressed(Keys.W)) {
velY = speed;
//camera.position.set(camera.position.x, camera.position.y + 1, camera.position.z);
up = true;
down = false;
left = false;
right = false;
}
//DOWN - S
if(Gdx.input.isKeyPressed(Keys.S)) {
velY = -speed;
// camera.position.set(camera.position.x, camera.position.y - 1, camera.position.z);
up = false;
down = true;
left = false;
right = false;
}
//RIGHT - D
if(Gdx.input.isKeyPressed(Keys.D)) {
velX = speed;
//camera.position.set(camera.position.x + 1, camera.position.y, camera.position.z);
up = false;
down = false;
left = false;
right = true;
}
if(!Gdx.input.isKeyPressed(Keys.D) && !Gdx.input.isKeyPressed(Keys.A))
velX = 0;
if(!Gdx.input.isKeyPressed(Keys.W) && !Gdx.input.isKeyPressed(Keys.S))
velY = 0;
}
Collision problem sample
http://i.minus.com/iDBb8576ul0gm.gif
EDIT: Would you say my code looks any organized? XD