Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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

share|improve this question
1  
What do you mean by "more precise"? Coordinates within a tile? How would such coordinates look like? –  Liosan Dec 11 '12 at 10:00
    
You already have the solution. Just drop the Math.round call and use floats for tileX and tileY. –  ccxvii Dec 11 '12 at 10:02
    
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 –  Rawrz Dec 11 '12 at 10:25
2  
@Rawrz Remember to post an answer when you find your solution! –  Laurent Couvidou Dec 11 '12 at 10:41
add comment

1 Answer

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).

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.