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!
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!