• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Tile Maps and Snapping player coordinates

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

2 replies to this topic

### #1BytePtr  Members

Posted 24 October 2011 - 05:30 PM

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.

### #2ApochPiQ  Moderators

Posted 24 October 2011 - 05:44 PM

int MapCoordinateX = RealCoordinateX / TileWidth;
int MapCoordinateY = RealCoordinateY / TileHeight;
Wielder of the Sacred Wands

### #3BytePtr  Members

Posted 27 October 2011 - 02:34 PM

Seems to work fine enough.

Thank you.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.