Tile Collision Assistance

Hi there,

I have been stumped with tiled + libgdx based collision lately. I tried using dermitfans method of doing things, however have experienced issues and would like a simpler method. Asking around, I have heard of using an array[col][row] for finding tiles in a tile map, though I am not sure how to implement this.

Col = X, Row = Y. (The forum formatting was being weird… )

My assumptions would be:
• Set Array[col][row] for tiles in tile map.
• Find all with property “blocked”.
• If player collides with tile that is “blocked”, check side and move back one tile in opposite direction.

This, I have heard, provides a very smooth and easy collision system and appearance, so I am anxious to learn how to do this. Please help if you can. :slight_smile:
I would really love for some pastebin examples on setting this up. Let us code together. :slight_smile:
Thanks,

  • A

Check if tile at [player.pos + player.speed] is not blocked, and if it is not blocked, player.pos += player.speed.

If it is blocked, don’t do anything.

That works if you can only go up down left right, but not two of them at once. (Simpler said: Walking diagonally)

Usually you’d like the player to slide along walls.
So you need to do that for both the x and y coordinate:


if (!isBlocked(player.pos.x + player.speed.x)) player.pos.x += player.speed.x;
if (!isBlocked(player.pos.y + player.speed.y)) player.pos.y += player.speed.y;

but that only works for AABBs. Because it doesn’t allow sliding in arbitrary directions (only slide along x and slide along y).

Also, if the speed is higher than 1, for example 5, then it’s likely that your character will stop 3 or 4 pixels in front of the object it’s colliding with.

It makes sense to make the game concept a little bit simpler, though, for example use a Tiled map like you do (and only AABBs) :slight_smile:

(yeah, [``x] is a list point here)