Sign in to follow this  

Mouse click on isometric map

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

Hi everybody! I'm new on this forum and i'm come from poland, so sorry for any mistakes [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]
Uh.. i wrote isometric map display:
screen: [url="http://scr.hu/0ug/176jj"]http://scr.hu/0ug/176jj[/url]

(Language: C++)


[CODE]
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
}
}[/CODE]

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

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

[CODE]map[..][..] = ID;[/CODE]

tile width = 120 and tile height = 63.

I'm looking and looking for solutions, but i can't find it [img]http://public.gamedev.net//public/style_emoticons/default/sad.png[/img] Edited by JakubW

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
The mapping from (x,y) to (xx,yy) is a linear mapping that can be expressed by the matrix operation
[code](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)[/code]

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. Edited by alvaro

Share this post


Link to post
Share on other sites

This topic is 2044 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this