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:
You can get the ID from a specific cell. Now for the questions:
What would the cell ID turn up as ? A number or some string - how would this be relevant to what is exactly placed?
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?
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.
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.)
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
}
}
}
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.
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 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:
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
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.