[SLIMDX] Transform screen coordinate to world [SOLVED]

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

Recommended Posts

Hi, could someone please explain to me how to transform a screen coordinate to a world coordinate using SlimDX? I am using a 3D camera with a 2D world, and would like to get an AABB representing the area of world that is seen by the camera. The easiest I way I thought was to project the top-left and bottom-right corners of the screen to world space. The world runs along the X axis, with a Z position of 0. I found some examples using Vector3.Project / UnProject, but SlimDX uses a different version of those functions that I don't understand. I've also read that you can do it using just the View/Projection Matrixes? Thanks, and I appreciate any help I get! [Edited by - BoxyCraft on May 20, 2010 11:21:51 PM]

Share on other sites
Hi!

Here is some SlimDX C# code accomplishing exactly that:

Vector3 projectMouse(Matrix world, Point mouseCoordinates, Vector3 worldPosition, Plane plane)		{						Ray mouseRay=GetTransformedMouseRay(mouseCoordinates, world);			Vector3 intersection;			Plane.Intersects(plane, mouseRay.Position, mouseRay.Position+mouseRay.Direction*10000, out intersection);			return intersection;		}Ray GetTransformedMouseRay(Point screenCoordinates, Matrix worldMatrix)		{			Vector3 near = new Vector3(screenCoordinates.X, screenCoordinates.Y, 0); // possible improve			Vector3 far = new Vector3(screenCoordinates.X, screenCoordinates.Y, 1);			Viewport viewport = Renderer.Device.Viewport;			Vector3 position = Vector3.Unproject(near, viewport.X, viewport.Y, viewport.Width, viewport.Height, viewport.MinZ, viewport.MaxZ, worldMatrix * Renderer.View * Renderer.Projection);			Vector3 direction = Vector3.Unproject(far, viewport.X, viewport.Y, viewport.Width, viewport.Height, viewport.MinZ, viewport.MaxZ, worldMatrix * Renderer.View * Renderer.Projection) - position;			direction.Normalize();			Ray transformedRay = new Ray(position, direction);			return transformedRay;		}

This code is not 100% bullet-proof, but it works.

In your case, if your 2D world runs along the X and Y axis (and z==0 everywhere), you would use that code like this:
Vector3 worldPosition = projectMouse(WorldMatrix, MouseCoordinates, new Plane(Vector3.Zero, new Vector3(0, 0, 1));

Share on other sites
Thanks a lot for your help!

The reason this wasn't working for me before is because my projection matrix had a mindepth of 0! So if anyone else is getting NaN's after calling Vector3.Subtract, you should check your matrix.

Your code works great by the way, but you forgot one little thing:

    direction = direction - position;    direction.Normalize();

And here's a similar thread for anyone who's looking: SlimDX - Picking

I have never heard that you shouldn't have a minimum depth of 0 in your projection matrix before, so could someone please confirm whether this is a general thing?

Thanks!

1. 1
2. 2
Rutin
20
3. 3
4. 4
A4L
15
5. 5
khawk
14

• 13
• 26
• 10
• 11
• 9
• Forum Statistics

• Total Topics
633737
• Total Posts
3013614
×