Jump to content
  • Advertisement
Sign in to follow this  
JakubW

Mouse click on isometric map

This topic is 2259 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 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 Edited by JakubW

Share this post


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

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

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!