Advertisement Jump to content
Sign in to follow this  
Bimble Bob

Local Tiles to Global and Tile Selection

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been working on a 2D Tile Based game and Map Editor for a little bit now. And I've come to the point where I need to be able to "Paint" tiles onto a map in my Map Editor. I have my Tile class and I have two arrays of Tiles. The first is the Tiles that can be displayed plus an extra offscreen border for tile scrolling and such. This one ranges form (0, 0) to (15, 15). Then I have my Map tile array which contains data on all the tiles on the map. This ranges from (0, 0) to (500, 500) or whatever the map size is. These tiles aren't drawn directly they are copied to the visible tiles array and those tiles are drawn. My problem is that I cannot seem to find a working way of converting a local on-screen tile (Say (4, 8)) to a global map tile (For example (45, 67)). I have tried this: for(int i = 0; i < 15; i++) { for(int y = 0; y < 15; y++) { int globalTileX = ((centreTileX - i) + centreTileX); int globalTileY = ((centreTileY - i) + centreTileY); tmpTile = LoAMap.Tiles[globalTileX][globalTileY)]; Tiles[y] = tmpTile; } } To display the correct tiles on screen. centreTileX/Y is a global map co-ordinate which is in the dead centre of the onscreen tiles. I'm probably going about it the wrong way but one I get this sorted I should be able to pick and manipulate tiles in the way I need. Sorry for the long post. Oh yeah this is written in Java although it shouldn't make too much difference. I just need a theory/idea/forumla. [Edited by - Dom_152 on September 18, 2006 1:21:40 PM]

Share this post

Link to post
Share on other sites
How do you keep track of the offset of your map?

It would be easy to just have a pixel-offset of the map and then convert it to tile coordinates to find out which tiles to draw. Using the same offset, you can convery any pixel coordinate to tile coordinates and find out which tiles you're clicking on.

Lets do a little example...

Say, your screen is 320x240.
Your tiles are 16x16.

Your visible screen area in tiles would be 20x15. (not important at this time)

Any point in pixels can be divided by the dimensions of the tiles and give you tile coords.

Point (56, 134) would be (3, 8) in tile coords.

Knowing this, if you translate the map, say scroll it left 467 pixels, you can still apply this rule. Just add your offset on and divide the total by the tile size.

hOffset = 467
Point screen = (56 + hOffset, 134)
Point tile = ( (int)(screen.x/16), ( (int)(screen.y/16) )

Does this answer your question?

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!