Tiled Map Collision Detection Help

Hello forum, recently I began to use a Tiled map for my game. I was thinking about using a more traditional AABB collision method but then found out I can get tile ID’s and so I would imagine I could figure out collision that way. I was trolling through past posts about Tiled and collision detection and came upon this:

With this code in particular

 TiledMapTileLayer.Cell cell = layer.getCell(x, y);

You can get the ID from a specific cell. Now for the questions:

  1. What would the cell ID turn up as ? A number or some string - how would this be relevant to what is exactly placed?
  2. After I figure out when my player is next to a certain tile, how could i prevent the player from moving onto that tile? Would there be a simple way to determine if the tile is to the left of the character, and making a variable like isAbleToMoveLeft set to false if its near a tile where it can not move left.Maybe something like this?
if(collisionTile.x < player.x){
     isAbleToMoveLeft = false;
}
  1. Final question is this: I have items drawn in 4 layers in Tiled. Is this something that will effect the way collision detection works? In the method written by matheus23 the getCollisionTilesFrom method requires a layer. So I am curious if 4 layers will create problems.

Thanks for the help! 8)

  1. What I did is I had a for-loop that would add 1 (or subtract 1) from the player’s x coordinate for each value in the xspeed. It would keep adding until it hit something, and I did something similar for the y coordinate. That is one simple method of solving it, although I’m sure there are much better ways. (You should only check for collision with tiles in the immediate vicinity, however. You don’t want to have to check too many tiles too often.)

I do not really understand what you mean by your usage of the foor loop ???

Let’s say that xspeed is an integer. If it is negative, this means x gets smaller and the character moves to the left. Otherwise, the character moves to the left.

Here is the pseudo code:


if(xspeed != 0){
			for(int i = 0; i < Math.abs(xSpeed); i++){
				if(the character will not collide with anything){
					x += Math.signum(xspeed);//the Math.signnum() method will return -1 if xspeed < 0, or 1 if it is not
				}
			}
		}

I am still not understanding the usage of the for loop… The “i” variable is not even being used.

The “i” is unimportant, it’s just force of habit. All this does is make sure that you keep moving in the direction of the speed until you hit something.

Perhaps I misunderstood your questions. I thought you were talking about moving your player until it hit something, not just standing still.

However, my method would still work.

But yes, you could have a “canMoveLeft” boolean that would not let the player move left if there was a block right to the left of the player.

Here is what I was thinking to try out: Put all of the cell ID’s that are passable into an array list. then do something like this:


for(loop through above array list){
     if(tile to the left of character is passable)
          isAbleToMoveLeft = true;
     else
          isAbleToMoveLeft = false;
}
//repeat for each direction in for loop

i think something like this should work well

I just got the actual tiled map working (before i just saved the tile map as an image and moved that aorund) but now when I move the camera everything is very grainy when moving. Is there a reason for this? Just to test things out I tried this:

camera.update();
		renderer.setView(camera);
		renderer.render();
		camera.translate(.01f,.01f);

I would not suggest looping through the tiles, looking for collisions. In all honesty, I am unfamiliar with this library. However, I would imagine that there are ways to check for tiles at a specific point in space. There is no reason to check for EVERY tile, only the near ones.

I don’t think I’ll be of very much help from here on out, I have never used that library. Good Luck :slight_smile:

I found out you can make properties for tiles and I am going to use those to help with collision. The last thing I am having great trouble with is why “.getCell(x, y)” returns a unique number even when trying it on the same tiles… This becomes a problem when trying to match it up with tile properties.

Are you looping through all your tiles to check for collisions? If so, that’s a bad idea. You could directly query tiles based on player position.

Say if each tile is 32x32…


int tx = playerx/32;
int ty = playery/32;

int tile = map[tx][ty];

oh no of course im not doing that.

Take a look at this: