Finding X and Y positions of tile in a grid?(Solved)


(Sparrow) #1

Hi there fellow FPers.

First of all thanks to Zachary for making this beautiful place for all FPers!

Okay, on to the question: In a collision between an Entity and the Grid (the level), is there a way to find the specific coordinates of the tile the entity collided with?

Thanks for your time.

Sparrow


(Jason Pickering) #2

you could take the characters X and Y, then divide it by the grid tile size and then round. that would give you their current gird location, then go from there. What exactly are you trying to do?


(Sparrow) #3

I’m working on a platformer and am trying to figure out a simple way for finding out if the player can grab a ledge and other player to surface related actions like wall walking/running.


(Abel Toy) #4

To get the tile index of a tilemap based on the x and y coordinates of an entity, you’ll have to get the entity’s coordinates and then divide them by the size of the tiles, like this:

tilemap.getTile(x / tileSize, y / tileSize);

You’d place this exactly where you check if the entity’s colliding with the grid. Then you can just check if the tile ID is the one corresponding to your ledges.


(Sparrow) #5

Yes that’s what I’ve been looking for, thanks Abel and Jason!