Advertisement Jump to content
Sign in to follow this  

Mouse coords to world space

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Short answer:

Depending on your setep, the first step may be to invert the mouse y coodinate to get window coordinates.

The second step is to assign a w value of 1 and an appropriate z value. The z value should be within the depth range. Note that a single mouse coordinate maps to a line in 3d, so you need to specify the z value to restrict the solution to a single point.

Multiply by the inverse viewport matrix to get NDC.

Multiply by the inverse projection matrix.

Multiply by the inverse view matrix.

Divide by w.

This is how I do it, and I can attest that it works in an OpenGL environment. I think it should work the same in D3D, but I can't say for sure.

Share this post

Link to post
Share on other sites
I have never tried this, but I have heard of ray casting in C++ to determine coordinates. If you look at the DX sample PICK(its in the direct3d\meshes i believe) they use the mouse to determine which triangle is hit in a mesh. I'm not sure how they do it, but it might be another place to start.


Share this post

Link to post
Share on other sites
Hi there ktuluorion,
How are you doing?

The Problem
Transforming mouse coordinates to world space.

The Solution
Nice topic and I see this pops up alot.
Well the theory is simple

1) Get your screen space mouse coordinates
2) Multiply them with the inverse view projection matrix.
3) Wallah, they should be in world space.

Now the implementation I have in C# and MDX code. It should be relatively easy to read.

private void ValidateMouseCoordinates(Form window)
//Computes the screen space coordinates to client coordinates
cursor = window.PointToClient(Cursor.Position);
if(cursor.X < 0) //if the x position is less than 0. set it to the window width
cursor.X = window.Width;
if(cursor.Y < 0) //if the y position is less than 0. set it to the window height
cursor.Y = window.Height;

This is .NET btw :) ehehe.
window.PointToClient(Cursor.Position); this method is a helper method in .NET of a form.
So Like I said before. (in a little more detail)
You have to remember that your mouse is in screen space coordinates. You want it in world space coordinates.

Object -> World -> View -> Clip -> Screen space
so to go from screen space -> world space you will have to multiply it by the inverse of the matrices inbetween the spaces :)
Inverse of (View and Clip) :)

1) Get the mouse coordinates in screen space
2) Transform the mouse coordinate by the inverse view projection matrix (Depends on some factors like the angle of view ... if it's isometric it might be different since the directional ray might be at an angle etc...)

I hope this helps a bit buddy.
Take care.

[Edited by - Armadon on August 31, 2005 3:56:42 PM]

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. 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!