Tile Maps and Snapping player coordinates

Started by
1 comment, last by BytePtr 12 years, 5 months ago
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.
Advertisement
int MapCoordinateX = RealCoordinateX / TileWidth;
int MapCoordinateY = RealCoordinateY / TileHeight;

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

Seems to work fine enough.

Thank you.

This topic is closed to new replies.

Advertisement