6
\$\begingroup\$

I'm trying to translate mouse coords to precise isometric coords (I can already find the tile the mouse is over, but I want it to be more precise). I've tried several different methods but I seem to keep falling short.

For drawing I use:

batch.draw(
    texture,
    (y * tileWidth / 2) + (x  * tileWidth / 2),
    (x  * tileHeight / 2) - (y * tileHeight / 2))

This is what I currently use for figuring out a tile position:

 float xt = x +  camPosition.x - (ScreenWidth/2) ;
 float yt = (ScreenHeight) - y + camPosition.y - (ScreenHeight/2);

 int tileY = Math.round((((xt) / tileWidth) - ((yt) / tileHeight)));
 int tileX = Math.round((((xt) / tileWidth) + ((yt) / tileHeight))- 1);

I'm just wondering how I could update these to allow for more precise coordinates, instead of tile only. EDIT: Following what ccxvii said below, and removing the -1 from tileX, the object follows my mouse just like I had wanted. Just going to re-examine the math and figure out if that change will result in other messes =o

\$\endgroup\$
4
  • 1
    \$\begingroup\$ What do you mean by "more precise"? Coordinates within a tile? How would such coordinates look like? \$\endgroup\$
    – Liosan
    Commented Dec 11, 2012 at 10:00
  • \$\begingroup\$ You already have the solution. Just drop the Math.round call and use floats for tileX and tileY. \$\endgroup\$
    – ccxvii
    Commented Dec 11, 2012 at 10:02
  • \$\begingroup\$ Ah, I had though that might be the right way, It follows the mouse more closely than my other attempts, but its still off a bit. Guessing a bug lies elsewhere in my code, I'll just keep looking, thanks =o \$\endgroup\$
    – Rawrz
    Commented Dec 11, 2012 at 10:25
  • 2
    \$\begingroup\$ @Rawrz Remember to post an answer when you find your solution! \$\endgroup\$ Commented Dec 11, 2012 at 10:41

1 Answer 1

1
\$\begingroup\$

I have worked with isometric games before, there is usually 'two' approaches (there are many but I've only personally worked with two). If you truly want to stick to 2D and want to work with tilemaps, which from your question appears to be the case, then I would highly suggest following a wonderful tutorial which talks about handling various aspects of isometric maps at XNA Resources. The tutorial is in XNA, but the concepts talked about in the article really helped me when I first played with it.

The second method, one I decided to go with, was to render the game in 3D (using decals for 2D sprites). This method allows you to easily cope with vertical displacement as you will simply be projecting/unprojecting your screen coordinates to 3D and using ray casting to determine where things intersect. The reason I picked this method was so that I could rotate the map easily and simulate isometric by using an orthographic camera. Finding the intersection I followed this guide on libgdx-users. (Its a bit dated but again the concepts are the same).

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .