Skip to main content
gamedev.net
🔒 Locked

Returning current tile

Started by Lord_Jabbercat Dec 28, 2004 at 10:09 AM 5 replies 1.4k views
Original Post
Lord_Jabbercat
Lord_Jabbercat
Hello, I've been working on getting a tile engine working and have wondered how I would get the current tile x,y. I'm working with 64x64 square tiles, but tile size doesn't really matter. Thanks for your time, Lord Jabbercat~
Sam Gamgee
Sam Gamgee
If I understand your question correctly, then you simply need to do this:

x = TileX * TILE_SIZE;
y = TileY * TILE_SIZE;

Where "TileX" and "TileY" is the tile you need to get (what column and row is it in?), and TILE_SIZE is 64 or whatever. You may have to change a few things around depending on how you draw your map though.

-Walter
Lord_Jabbercat
Lord_Jabbercat
Sorry, I just relised I didn't phrase my question very well. (I had a tad too much to drink over Christmas I think!) What I'd like to know is how to return an npc's tile x or y. Sorry again.
akOOma
akOOma
x = npc.x * npc.width / tile.width
y = npc.y * npc.height / tile.height

I think that should work
QB 4 EVER
Lord_Jabbercat
Lord_Jabbercat
I was more interested in getting a npc's x,y position in tiles rather than pixels, but I suppose I'll nead to be able to check what tile an npc is standing on aswell at some point!

I can't really give alot of imformation about my engine, as I don't really have alot of imformation about it myself. I'm a novice programmer under training with my game company at the moment, so I'm not really able to give alot of answers at the moment!

Thanks.
Sam Gamgee
Sam Gamgee
So you have the x and y pos of the unit in pixels, and you need the tile position then? The following should do the trick:

TileX = x / TILE_SIZE;
TileY = y / TILE_SIZE;

That will convert your pixel position to Tile Position. You may have to modify it somewhat to fit your project though.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.