Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


Mouse click on isometric map


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 JakubW   Members   -  Reputation: 102

Like
0Likes
Like

Posted 13 May 2012 - 07:42 AM

Hi everybody! I'm new on this forum and i'm come from poland, so sorry for any mistakes Posted Image
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 Posted Image

Edited by JakubW, 13 May 2012 - 07:44 AM.


Sponsor:

#2 codeToad   Members   -  Reputation: 142

Like
0Likes
Like

Posted 13 May 2012 - 05:03 PM

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

#3 JakubW   Members   -  Reputation: 102

Like
0Likes
Like

Posted 14 May 2012 - 09:41 AM

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

#4 Álvaro   Crossbones+   -  Reputation: 15596

Like
0Likes
Like

Posted 14 May 2012 - 09:59 AM

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, 14 May 2012 - 10:04 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS