Sign in to follow this  

MDX 2D to 3D Points

Recommended Posts

I have been trying to convert from a 2D mouse coordinate to a 3D coordinate using the Vector3.Unproject method. I plan to eventually use this to do picking in the scene but I decided that before I tried picking, I wanted to make sure that the rays I was casting started and ended at the correct position. The program is designed so that when I click the mouse button, a ray is supposed to appear where I clicked the mouse in the 3D scene. The rays are defniately affected by a change in the mouse position and they appear relative to each other in the correct direction (if I click the mouse to the left, the rays appear more to the left). However, they only seem to work the best when the camera is at 0, 0, for X and Y (Z doesn't have any effect) and even then, they don't seem perfectly along with the mouse. As I move the camera left, the X coordinates of the rays move to the right and if I move the camera right, the X coordinates of the rays move to the left. The code I use to get the start and end points of the rays is:
Vector3 start, end;
Matrix viewMat = Matrix.LookAtLH(Center, Center + Direction, new Vector3(0, 1, 0));
Matrix projectionMat = Matrix.PerspectiveFovLH((float)Math.PI / 4, device.Viewport.Width / device.Viewport.Height, 0f, 100f);

start = Vector3.Unproject(new Vector3(x, y, 0), device.Viewport, projectionMat, viewMat, Matrix.Identity);
end = Vector3.Unproject(new Vector3(x, y, 1), device.Viewport, projectionMat, viewMat, Matrix.Identity);
Where 'Center' is a Vector3 indicating the position of the camera, 'Direction' is a C# property I wrote that returns a Vector3 going in the direction of the camera, 'device' is the Direct3D device, and 'x' and 'y' are variables containing the x and y position of the mouse when it was clicked. 'start' and 'end' are used to construct rays that appear on the screen so I can check to see if my function is working properly. I would greatly appreciate any help on this problem.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this