View Full Version : Another Mouse Coordinate Question

Baron

03-12-2000, 08:29 PM

Okay, I'm familiar with detecting which object falls under the mouse cursor, but I need to figure out the actual coordinates of the intersection.

I want an object to follow the mouse cursor around the screen, so I need to translate screen coordinates into world coordinates, using the first object hit under the cursor as the depth.

Any suggestions/tutorials out there?

I came up with those equations through trigonometry. I haven't tested them, but there is a high probability they work because we can find the special case(camera is on the xy plane with no rotation whatsoever)(wich I have tested http://www.opengl.org/discussion_boards/ubb/smile.gif) throught them.It will make your object move a the same distance from the camera at whatever position your camera is:

World_Xd = (Xmouse * factor_H *(90 - rotx) * (90 - rotz) / 8100) + (Ymouse * factor_Y *rotz / 90).

World_Yd = (Ymouse * factor_Y *(90 - roty) * (90 - rotz) / 8100) + (Xmouse * factor_X *rotz / 90).

World_Zd = (Xmouse * factor_H * rotx / 90) + (Ymouse * factor_Y * roty / 90).

factor_H = Dist_OC * (fright - fleft) /Window_width

factor_Y = Dist_OC * (ftop - fbottom) / Window_height.

World_Id = the displacement in world unit on the I axis.

Imouse = Mouse displacement in the I coordinates.

rotI = rotation of the camera from the I axis

Dist_OC = distance from the camera to the object.

fright, fleft, fbottom, ftop = values from the frustum.

if using glProjection :

ftop = tan(angle) * znear

fbottom = -top

fright = top * Window_width / Window_height

As you can see they are pretty messy equations. There are a bunch of optimizations you can do like changing the division to multiplication.

the biggest optimisation you can do is to forbid the camera to move. In that case most of the variables have a value.

And one more thing, if you use an orthographic projecton,

factor_H = (fright - fleft) /Window_width

factor_Y = (ftop - fbottom) / Window_height

It's pretty long to explain where I got the equations so I won't write it.

[This message has been edited by Gorg (edited 03-13-2000).]

[This message has been edited by Gorg (edited 03-13-2000).]

Powered by vBulletin® Version 4.2.2 Copyright © 2016 vBulletin Solutions, Inc. All rights reserved.