• Advertisement
Sign in to follow this  

Why won't this work?

This topic is 4679 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

This calculation is "supposed" to map my mouse click to the proper x,y tile on a isometric map but it's not very accurate, It's off by about 1 tile on the Y side although the X tile seems to be closer on target.. 128 = graphic width 64 = graphic height e.X/e.Y = Mouse X,Y on screen Dim X As Int32 = CType((128 * e.Y + 64 * e.X) / (128 * 64), Int32) Dim Y As Int32 = CType((128 * e.Y - 64 * e.X) / (128 * 64), Int32) Console.WriteLine(X.ToString & "/" & Y.ToString) I am trying to implement the calcuation as it show on this article... http://www.bookofhook.com/Article/GameDevelopment/IsometricEngineDevelopmen.html

Share this post


Link to post
Share on other sites
Advertisement
Why not just:

X = e.x / 128;
Y = e.y / 64;

If you were in the first tile for instance (the mouse coordinates 0 to 63 in the Y direction) and you divide by 64 you get 0. Similary 0 to 127 in the X direction would be tile 0 and dividing by 128 gives you 0.

Share this post


Link to post
Share on other sites
I would agree with deepcompfish. I can't see the purpose of going through all of those calculations you have. I read the article and I'm not the least bit convinced that the formulae given for mouse->screen/screen->mouse make any sense at all.

Share this post


Link to post
Share on other sites
I just briefly looked at the website, and from the example game screenshots it looks like it is talking about diamond-shaped tiles. This means that Map X/Y and Screen X/Y are not the same axes (and thus a simple formula such as the one given by deepcompfish doesn't work).

I can't say anything about the formula given, but it does look somewhat like I remeber the 'diamond tile 'projection'' algorithm to be.

Share this post


Link to post
Share on other sites
I didn't take the diamond-shaped tiles into consideration when I examined the formulae. That was pretty silly of me. The calculations make a bit more sense in that case.

Is the Y-value always off by 1 and, if so, is it always off in the same direction (greater or smaller)?

Share this post


Link to post
Share on other sites
Well when I click on the very Top Left tile of the screen, which should be 0, 0, it registers as 1, 0 - It's pretty accurate other than that.. So I'm thinking maybe the calculation is off by a bit.. A screen shot is here http://www.2and2.net/Uploads/Images/ss2.JPG As you see the debug shows 1, 0 when I click on the first tile located in 0, 0

Wuzzbent

Share this post


Link to post
Share on other sites
Edit - I'm an idiot. I thought I saw 128/64 at the end of the equation. Nevermind.


[Edited by - Dave Hunt on March 31, 2005 6:47:06 PM]

Share this post


Link to post
Share on other sites
These are the equations that I worked out ages ago. Not sure if it helps.



And I also wrote an article ages ago.

Edit: Sheesh, I've just read it again, I sound so lame. Well it's there for all who want to read it.

Share this post


Link to post
Share on other sites
The map to screen formula given there looks good, assuming you set up your map the same way they did:

Sx = Mx * (width / 2) - My * (width/2)
Sy = Mx * (height / 2) + My * (height/2)

Which simplifies to:

Sx = (Mx - My) * (width / 2)
Sy = (Mx + My) * (height / 2)

If Mx = My then Sx = 0 and Sy = 2(My) * (height) * 1/2 = My * height. So the line Mx = My maps to the y axis in screen space. Likewise Mx = -My means Sx = Mx * width and Sy = 0, mapping to the x axis in screen space.

0,0 in map space = 0,0 in screen space.

If Mx = 0,

Sx = -My * Width / 2,

Sy = My * height / 2
My = (2 * Sy) / height

Sx = -2 * (Sy / height) * Width * 1/2
Sx = -Sy * Width / Height

So the map space x axis in screen space runs through the screen space origin and along the Sx = -Sy * Width / Height diagonal.

If you graph all this out and use standard windows screen space where the origin is in the upper left corner of the screen and y increases towards the bottom of the screen you get a diamond map where the map origin (the top of the diamond) is in the upper left corner. The top of the map (y = 0) is a diagonal running down towards the bottom right corner of the screen. The left edge of the map (x = 0) is off screen for all positive non-zero values of y and the left edge of the screen is the x=y diagonal in map space.

There is one issue left here though. Lets say I'm drawing tile (0,0) in map space. After the transform I get (0,0) in screen space back out, but what do you do with it? You could use this as the upper left corner of the quad containing the diamond tile. However in that case the position (0,0) in screen space actually won't belong to the (0,0) map tile. It would belong to (-1,0).

mx = (w * sy + h * sx) / (w * h)
mx = 0 + 0 / (wh) = 0

my = (w * sy - h * sx) / (w * h)
my = 0 - 0 / (wh) = 0

So we can't do that.

Instead lets put the center of the tile there. Now we'll get the correct result for the center. Let's consider the next tile down and to the right. It's center will be at (width / 2, height / 2):

mx = (w * sy + h * sx) / (w * h)
mx = (w * (h/2) + h * (w/2)) / (w * h)
mx = (wh/2 + wh/2) / wh
mx = 1

my = (w * sy - h * sx) / (w * h)
my = (w * (h/2) - h * (w/2)) / (w * h)
my = (wh/2 - wh/2) / wh
my = 0

That works. Now let's consider a point on the edge between the two:

mx = (w * sy + h * sx) / (w * h)
mx = (w * (h/4) + h * (w/4)) / (w * h)
mx = (wh/4 + wh/4) / wh
mx = (wh * 1/2) / wh
ms = 1/2

my = (w * sy - h * sx) / (w * h)
my = (w * (h/4) - h * (w/4)) / (w * h)
my = (wh/4 - wh/4) / wh
my = 0

As long as fractions above 1/2 round up this should work. If your float to int truncates then you'll have problems. You probably should also convert e.X and e.Y to floats and use floating point constants as well. Finally make sure that you are centering the tiles on the screen coords given by the transform.

Edit: Ok looking at your screenshot it looks like that is your problem. The 0,0 map tile needs to be centered at 0,0 in screen space. So all your tiles are off by half a tile.

Share this post


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

  • Advertisement