Jump to content

  • Log In with Google      Sign In   
  • Create Account

Isometric Tile Picking Solved!

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.

  • You cannot reply to this topic
8 replies to this topic

#1   Members   -  Reputation: 485


Posted 03 December 2008 - 07:35 AM

Alright, I'm at my wits end, maybe someone out here can lend some insight. I'm currently working on an Isometric Tile-Based project in XNA. I'm using the Java app Tiled for map editing and all that. Everything has been going swimmingly so far, however I've reached a bit of a road block. I can't figure out the math I need to do tile picking (screen coords to tile coords). a lot of the things I've seen have had the maps drawn to the screen starting at the top left tile as 0,0 and shifting accordingly. However the maps produced using Tiled are to be drawn with the top center tile as 0,0 with X going down-right and Y going down-left. (Screenshot to show what I mean) Screenie If anyone out there could point me in the right direction on this, I would much appreciate it, as I think I'm about to go insane. [Edited by - FalconDragoon on January 3, 2009 12:08:23 PM]

#2   Crossbones+   -  Reputation: 13990


Posted 03 December 2008 - 08:18 AM

Without being able to give specifics, because you haven't told us your tile sizes, all we can really tell you is that the math for translating from screen-space coordinates (the mouse click) to tile-space coordinates is such that both the x and y results are both dependent upon the x and y inputs due to the diagonal nature of the tile indexing -- as opposed to rectangular tile grids, in which x an y are completely independent.

The formula can be coded as a 3x2 matrix, accounting for the transformation and the x/y offset (viewport centering).

Now, this only covers the simple case where the isometric grid doesn't contain any tiles which have "height" or any objects which extend beyond the diamond of a single tile. If you have tiles with height or large objects, you'll need to do additional work.

#3   Members   -  Reputation: 485


Posted 03 December 2008 - 08:35 AM

That does help some, I should have thought to add the tile dimensions.
Currently I'm using tiles that are effectively 64x32.
I understand the concept (To a degree) of how it's supposed to work, but I just can't wrap my head around how to implement it...

Thankfully I don't have any large objects or height to worry about.

Oh, and thanks for the response.

EDIT: would it be possible for you to elaborate a bit on the 3x2 matrix bit?

The first part of your reply is what I had deduced, the second part, I'm sure, could be the missing link with a little bit of detail.

[Edited by - FalconDragoon on December 3, 2008 4:35:08 PM]

#4   Members   -  Reputation: 413


Posted 04 December 2008 - 08:08 AM

its simple really, I'll start from tile--> screen and then see how it goes from screen to tile. Of course you should replace the numbers 64 and 32 by constants for easy change.

tile --> screen
for every tile you go in the X direction you go 64 pixels right and 32 pixels down,
for every tile you go in the Y direction you go 64 pixels left and 32 pixels down,

on screen right=positive x, left=negative x, up=negative y, down = positive y

so we get:
pixel_x = X_LOCATION_OF_TOP_MIDDLE_TILE + 64*tile_x - 64*tile_y
pixel_y = Y_LOCATION_OF_TOP_MIDDLE_TILE + 32*tile_x + 32*tile_y

now to do the screen --> tile
for ease of writing, pixel_x = px, tile_x = tx, X_LOCA... = X0

equation1: px = X0 + 64*(tx-ty)
equation2: py = Y0 + 32*(tx+ty)
we want to find equations for tx and ty when given px and py, so we do some algebra:

eq1+2*eq2 = px+2py = X0+2Y0 + 128*tx
---> tx = (px+2*py -X0-2Y0)/128

eq1-2eq2 = px-2py = X0-2*Y0 -128ty
---> ty = (px-2py - X0+2*Y0) /(-128)

I hope that was clear, and maybe you learned some math from it :)

the matrices are just compressed way to do the algebra.

[Edited by - Iftah on December 5, 2008 6:08:28 AM]

#5   Members   -  Reputation: 485


Posted 04 December 2008 - 10:09 AM

Nice, this could indeed be just what I needed. I plan on studying this quite a bit. I've been successfull in converting from tile->screen coords, but the other way has been weighing heavily on my algebra challenged mind.

Thanks a ton for the assist!

Admittedly I am still curious about the matrix solution.

#6   Members   -  Reputation: 485


Posted 04 December 2008 - 02:24 PM

WooT! Awesome man that did the trick! well after a couple of tweaks.

Firstly, I couldn't figure out where the 128 was comming from (It was the one thing that had me confused) changing it to 64, though, got the whole thing right on.

Thanks a ton!

[Edited by - FalconDragoon on December 22, 2008 12:24:49 PM]

#7   Members   -  Reputation: 122


Posted 22 December 2008 - 07:56 AM

So this doesn't have to do with mouse coords / tile picking, but I have a question about clipping.

I'm working on an Isometric (64x32) tile game in Java, and I'm having an issue with the placement of my graphics on the JFrame after clipping (it was pretty funny the first time i saw what it was doing)

So I won't include code unless anyone wants it, to keep the post shorter but here's what I have now:

A JFrame,
A Subclass of JPanel that I created

All of the drawing I do is in an overridden version of the JPanel's paint() method. I create an "offscreen" Graphics instance to draw to and at the end, draw it to the JPanel's Graphics object and paint. Well I have clipping set up to move the clip window as you move the player, to keep your view of yourself centered (with respect to what's displayed on your screen).

The specified rectangle is clipped as expected/desired, however on the JFrame it appears as if the whole map is drawn and you are just viewing the area that is clipped. The clipping window moves around the JFrame.. lol.

I'm using setBounds() on the JPanel because I plan to add other things to the JFrame, and this JPanel will only be the Viewable Map portion of the Game GUI. So I think modifying where I set the bounds could fix this problem but I just thought I'd ask here and see if anyone has experience with this before I start trying a bunch of different things :P

#8   Members   -  Reputation: 485


Posted 22 December 2008 - 11:38 AM

Could just be me, but that's tough to picture. Screenshot?

EDIT: I think I see what your saying (After re-reading) I'm assuming that the window you have is as big as the entire map and your just moving around a square to view a small piece (Like a lense or fog of war type thing)?

#9   Members   -  Reputation: 122


Posted 23 December 2008 - 01:35 AM

Heh sorry, I was able to solve my problem today.

Rather than moving my JPanel so that the clipped region -always- shows up at the same place on the JFrame, I just change the coordinates used to draw the graphics (current map) to the JPanel, and keep the clip region in the same place.. and that did it :)

[Edited by - pete2705 on December 23, 2008 9:35:54 AM]

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.