Hello Guest

[Solved] Transform mouse coordinates to world coordinates

  • 11 Replies
  • 11200 Views
[Solved] Transform mouse coordinates to world coordinates
« on: October 07, 2013, 16:51:30 »
I need to find out if a sprite has been clicked on in 2d that is also dependent on the cameraPos vector, which I'm using to move around. If possible, I would like to check for rotation as well. I have translated my window mouse coords to (-1, 1) coordinates for configurable display sizes. Thanks for the help.
« Last Edit: October 10, 2013, 20:35:36 by nickbrickmaster »

Re: Transform mouse coordinates to world coordinates
« Reply #1 on: October 07, 2013, 16:58:13 »
This is a matter of subtracting the camera's position from the sprite's position and seeing if the mouse is within its bounds.

So for instance if the camera is now at 200,100 in the world coordinates, that means the center of the scene (0,0) is at (200,100). So if the sprite is at 201,101, the sprite is now at 1,1. The last step would be to figure out the size of a world coordinate unit in screen units; this depends on whether you chose a fixed-pixel size for each unit, or to stretch each unit so there are always visible a certain number of them either horizontally or vertically.

For the first, you would simply figure out the conversion ratio; if the screen is 800x600 and the units come out to 32px, then 201,101 starts at 432,332 (for example.) therefore, the sprite's bounds start at (432-400)/400 , (332-300)/300. Its upper bounds would probably be (464-400)/400,(364-300)/300. Those two pairs of decimals could then be checked against the mouse-coords to find if the mouse was over the sprite.

For the second, the process would be the same except you'd already know the percentage of the view a single unit was, like say there is currently 32 units in view, then each unit would be 1/16 of x making 201,101 come out as 1/16,1/16 and its upper bounds 2/16,2/16 (they'd be decimals.) This could be directly tested against the mouse coords.
« Last Edit: October 07, 2013, 17:09:34 by RiverC »

*

Offline quew8

  • *****
  • 569
  • Because Square Eyes Look More Real
Re: Transform mouse coordinates to world coordinates
« Reply #2 on: October 07, 2013, 17:01:16 »
http://www.opengl.org/sdk/docs/man2/xhtml/gluUnProject.xml If you wan't a more dynamic method that takes into account projection and modelview matrices as well as viewport transform etc.

Re: Transform mouse coordinates to world coordinates
« Reply #3 on: October 07, 2013, 17:14:21 »
This is a matter of subtracting the camera's position from the sprite's position and seeing if the mouse is within its bounds.

So for instance if the camera is now at 200,100 in the world coordinates, that means the center of the scene (0,0) is at (200,100). So if the sprite is at 201,101, the sprite is now at 1,1. The last step would be to figure out the size of a world coordinate unit in screen units; this depends on whether you chose a fixed-pixel size for each unit, or to stretch each unit so there are always visible a certain number of them either horizontally or vertically.

For the first, you would simply figure out the conversion ratio; if the screen is 800x600 and the units come out to 32px, then 201,101 starts at 432,332 (for example.) therefore, the sprite's bounds start at (432-400)/400 , (332-300)/300. Its upper bounds would probably be (464-400)/400,(364-300)/300. Those two pairs of decimals could then be checked against the mouse-coords to find if the mouse was over the sprite.

For the second, the process would be the same except you'd already know the percentage of the view a single unit was, like say there is currently 32 units in view, then each unit would be 1/16 of x making 201,101 come out as 1/16,1/16 and its upper bounds 2/16,2/16 (they'd be decimals.) This could be directly tested against the mouse coords.

That's great, thanks. One thing I forgot to mention is that the camera also moves vertically, that is, along the z-axis. How could I take this into account as well?

Re: Transform mouse coordinates to world coordinates
« Reply #4 on: October 07, 2013, 17:21:04 »
So is this in 2d or 3d?

Re: Transform mouse coordinates to world coordinates
« Reply #5 on: October 07, 2013, 17:28:38 »

Re: Transform mouse coordinates to world coordinates
« Reply #6 on: October 07, 2013, 17:30:00 »
Then how is the camera moving in the z direction? Is it zooming, or is the world itself 3d and the view is just fixed 2d?

Re: Transform mouse coordinates to world coordinates
« Reply #7 on: October 07, 2013, 17:50:50 »
I suppose the world is 3D, and the view is fixed.

Re: Transform mouse coordinates to world coordinates
« Reply #8 on: October 07, 2013, 17:57:22 »
Then you may wish to look into the method our other interlocutor described above and ask him more on how to use it. Whether a sprite is under the cursor would then depend on if it is 1. in front of the plane of the field of view, 2. if the 'depth' it is in the view, which would effect its visible size.

Furthermore, if you're using perspective, that complicates things even more.

Re: Transform mouse coordinates to world coordinates
« Reply #9 on: October 08, 2013, 02:10:14 »
http://www.opengl.org/sdk/docs/man2/xhtml/gluUnProject.xml If you wan't a more dynamic method that takes into account projection and modelview matrices as well as viewport transform etc.

I found a good modern method for this, but I have a couple questions. Do I need to test it for each of my model matrices?

*

Offline quew8

  • *****
  • 569
  • Because Square Eyes Look More Real
Re: Transform mouse coordinates to world coordinates
« Reply #10 on: October 08, 2013, 18:13:53 »
What do you mean by "each?" If you render each sprite with a different model matrix then yes you would have to do each separately.

Re: [Solved] Transform mouse coordinates to world coordinates
« Reply #11 on: October 10, 2013, 20:36:01 »
Thanks for the help. I decided to go with color picking.