Fixed it.
Hi JGO,
I’m having an issue with my collision system in terms of my player colliding with the tiles properly. Almost everything works except when the player moves up or down against a wall that’s not on the very perimeter of the level.
The player is the Minotaur walking up onto the wall which he should have collided against.
This bug only occurs when the player faces directly head on to the wall tile that it SHOULD collide with, and walks into it. The collision works in everything but these instances.
The code below is the code I use to check for collisions with the TileMap Tile. All of the tiles that are walls have been given the property “blocked” which is what the code checks for.
Essentially, this method is called with the player’s current x and y position as the parameters. Then it checks to see if it coincides with a blocked tile’s x & y position.
public boolean isBlocked(float x, float y) {
TiledMapTileLayer.Cell cell = collisionLayer.getCell((int) (x / collisionLayer.getTileWidth()), (int) (y / collisionLayer.getTileHeight()));
return cell != null && cell.getTile() != null && cell.getTile().getProperties().containsKey(blockedKey);
}
This code is the context in which this method is used to check for collision when the player moves.
// Rest of the movement controls omitted for brevity. These are the only statements that seem to be faulty in checking collision
else if(Gdx.input.isKeyPressed(Input.Keys.DOWN)) {
currentSprite = playerSprites[0];
float step = 0;
if(!isBlocked(x, y + step)) {
this.y -= speed * Gdx.graphics.getDeltaTime();
} else {
this.y -= 0 * Gdx.graphics.getDeltaTime();
}
}
else if(Gdx.input.isKeyPressed(Input.Keys.UP)) {
currentSprite = playerSprites[1];
float step = 32;
if(!isBlocked(x, y + step)) {
this.y += speed * Gdx.graphics.getDeltaTime();
} else {
this.y+= 0 * Gdx.graphics.getDeltaTime();
}
}
}
I’ll try to upload a demo executable jar ASAP if needed.
Thanks!