Public Group

# Tile Maps and Snapping player coordinates

This topic is 2606 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi.

For example i create simple tile map and add player to it.
When i press left, right, up or down key i will increase or decrease X and Y pos, like this for example (C++):

xpos += 1;
ypos += 1;

Map is define by simple numbers 0 and 1 etc.
0 is grass
1 is rock etc...

Depending on number i draw specific tile. Now let's say my tiles are all 64x64 pixels.
I would like to get "full" coordinates of player.
I mean, let's say map width is 256 and height also 256.
So if player is at X: 2 Y: 2 (at which the map tile ID is 1 for ex) then i must get his coordinates also like they are: X 2 and Y 2.
If he moves away from tile ID 1 then coordinate must also increase by one. Not by 1.42343 or something...

I don't know how to explain it better. It just must increase coordinate value only if he moves away from one tile to another.
If player is in one tile and moving within that tile, then coordinate values should stay same.

But i don't know the math behind it. Should i use % operator to do what i need or something else?

Thanks for any help.

##### Share on other sites
int MapCoordinateX = RealCoordinateX / TileWidth;
int MapCoordinateY = RealCoordinateY / TileHeight;

##### Share on other sites
Seems to work fine enough.

Thank you.

1. 1
2. 2
Rutin
17
3. 3
4. 4
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633735
• Total Posts
3013594
×