PDA

View Full Version : Vector3.Unproject does not work


vi1
06-18-2007, 10:18 AM
I am using DirectX 9.0 and c# 2.0.

I want to be able to to click on the screen and convert the point of mouse click to World coordinates. I've tried using Vector3.Unproject but I do not get correct results.

I tried to figure out if something is wrong with the matrices I use but I cannot find anything wrong with them. More over the Vector3.Project works perfectly.

So, this is how I set up my matrices:

device.Transform.Projection = Matrix.PerspectiveFovLH((float)(Math.PI / 4), this.areaWidth / this.areaHeight, 0f, 5000f);
device.Transform.View = Matrix.LookAtLH(new Vector3(0, 0, -25), new Vector3(), new Vector3(0, 1, 0));
Matrix m = Matrix.Identity;
m.M41 = 170;
m.M42 = -65;
m.M43 = 0;
this.device.Transform.World = m;


Then, I try to project a point like that:

Vector3 t = new Vector3(-170, 65, 0);
t.Project(device.Viewport, device.Transform.Projection, device.Transform.View, device.Transform.World);

This works perfectly fine - I get the screen coordinates point that is exactly in the center of my form, which is correct.

However, if I try to unproject a point I get a strange result:

Vector3 t2 = new Vector3(200, 300, 0);
t2.Unproject(device.Viewport,device.Transform.Proj ection, device.Transform.View, device.Transform.World);

I get x = 16,36 and y = -6.4 where I expect to get something like (-171, 64)

Also, I noticed that these values vary slightly depending on the height of my view matrix (in this case -25).

Any suggestions for what I coulld be doing wrong will be greatly appreaciated.

Thanks!

Reedbeta
06-18-2007, 01:23 PM
You don't seem to understand how unproject works. There is no way to recover a 3D position from a 2D point in screen space because many different 3D points map to the same screen space location. Rather you should think of it as firing a ray from the eye through the screen pixel and intersecting it with the scene. The unproject function takes your screen space location and gives you a point in world space that you can use to construct this ray, along with the eye location. It doesn't determine the ray's intersection with the geometry; you have to do that yourself.

vi1
06-26-2007, 06:02 AM
Thanks a lot for the help.

Your comments pointed into the right direction and was able to get it working.