[Solved] Transform mouse coordinates to world coordinates

Started by nickbrickmaster, October 07, 2013, 16:51:30

Previous topic - Next topic

nickbrickmaster

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.

RiverC

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.

quew8

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.

nickbrickmaster

Quote from: RiverC 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.

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?

RiverC



RiverC

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?

nickbrickmaster

I suppose the world is 3D, and the view is fixed.

RiverC

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.

nickbrickmaster

Quote from: quew8 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.

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?

quew8

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.

nickbrickmaster

Thanks for the help. I decided to go with color picking.