# OpenGL Mouse Coords to Unit Coords math problem

This topic is 5419 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Here is my algorithm for converting cursor coordinates Windows sends with WM_MOUSEMOVE, etc., into actual unit coordinates in the 3D world. window_x, window_y are my window sizes mouse_x, mouse_y are my incoming Windows cursor coordinates mx, my are those coordinates converted into "(0,0) in center" camera_distance is the distance between the camera and the 2D plane on which the unit coordinates will be mapped ("board") camera_x_offset, camera_y_offset are the relevant positions of the camera Here is my code: float mx = mouse_x - (window_x / 2); float my = mouse_y - (window_y / 2); mouse_ux = ((mx / window_x) * camera_distance) / ((window_y/window_x) * sin(DEGtoRAD(TGO_FIELDANGLE))) - camera_x_offset; mouse_uy = ((my / window_y) * camera_distance) / sin(DEGtoRAD(TGO_FIELDANGLE)) - camera_y_offset; Everything works great UNTIL you tilt the board a little bit (change the camera's angle around the X axis). Normally, when the camera's looking at the board straight-on, my cursor points to the center of the circle model that I'm moving around on the board. When I change the X angle of the board slightly, however, things get slightly messed up. On the top half, near the corners, the cursor gets a little farther out than the center of the circle. On the bottom half, near the corners, the cursor gets a little closer in than the center of the circle. This is clearly due to perspective, because units appear closer together up top, and father apart on bottom (but Windows cursor coordinates remain constant). So here's my variable: camera_x_angle Could anyone tell me how to insert that variable in the above code to make the Cursor-to-Unit conversion flawless? Here is a diagram of my dilemma: EDIT: Oops!! This isn't OpenGL specific. Sorry!

##### Share on other sites
>>Here is my algorithm for converting cursor coordinates Windows sends with WM_MOUSEMOVE, etc., into actual unit coordinates in the 3D world.<<

SEE GLUUNPROJECT(..)

• 18
• 29
• 11
• 21
• 16
×

## Important Information

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!