Mouse click on isometric map

Started by
2 comments, last by alvaro 11 years, 11 months ago
Hi everybody! I'm new on this forum and i'm come from poland, so sorry for any mistakes smile.png
Uh.. i wrote isometric map display:
screen: http://scr.hu/0ug/176jj

(Language: C++)



for(int y = 0; y < map_height; y++)
{
for(int x = 0; x < map_width; x++)
{
xx = (x-y)*(TILE_SIZE_W/2);
yy = ((x-1)+(y-1))*(TILE_SIZE_H/2);

// render tiles
}
}


The variable "xx" and "yy" are tiles position.

But now, how can i read the clicked tile?
I want to do something like that:

map[..][..] = ID;

tile width = 120 and tile height = 63.

I'm looking and looking for solutions, but i can't find it sad.png
Advertisement
First, let me make sure I understand your question. You want to have the user click the mouse, and be able to tell which tile they clicked on. The problem is, you can only get the mouse coordinates in screen space, and your tile data is stored in world space. Is that right?

I've read about how to do this before, and the problem can be solved with ray casting. I have never actually implemented it, but I can give you a high-level idea of how i think it might work.

Somewhere in your code, you must be storing the camera's position and the direction it is looking. If the player were to click in the exact center of the screen, you could imagine a ray coming out of the camera's position, travelling in the direction the camera is looking. If the ray were long enough, you could compute which tile that ray intersected (if any).

Now let's say the player does not click in the exact center of the screen. Imagine a plane centered on the camera's position, perpendicular to the direction the camera is looking. Wherever the player clicked would correspond to some point on that plane. If there were a ray coming out of that point, in a direction parallel to the camera's direction, you could compute which tile that ray intersects (if any).
Yes, that's right, that's my problem.

Hmm thanks for idea. One day I tried to do this ray-casting but in 3D - i did something like that, but not really ray-casting.

I must try do this in 2D, thanks :)
The mapping from (x,y) to (xx,yy) is a linear mapping that can be expressed by the matrix operation
(TILE_SIZE_W/2 -TILE_SIZE_W/2 0 ) (x) (xx)
(TILE_SIZE_H/2 -TILE_SIZE_H/2 -TILE_SIZE_H) * (y) = (yy)
( 0 0 1 ) (1) ( 1)


You can use the inverse of that matrix to convert back from (xx,yy) to (x,y). Is that what you are looking for?

EDIT: I give up on the formatting. Just rearrange what I wrote above until it makes sense.

This topic is closed to new replies.

Advertisement