Discussion:
Mouse Screen Coordinates to World Space
(too old to reply)
H. Williams
2005-08-12 23:34:25 UTC
Permalink
How do you convert mouse XY coordinates to world space?

I tried the following code (passing the mouse coordinates to x & y and
setting z to 0):

Vector3 v = new Vector3(x,y,z);
device.Transform.World =Matrix.Translation(0f,0f,0f);
v.Unproject(device.Viewport, device.Transform.Projection,
device.Transform.View, device.Transform.World);

Unfortunately v.X & v.Y only correspond correctly at the screen center.

thanks.
Michel Walsh
2005-10-07 12:57:56 UTC
Permalink
Hi,


I am a little bit late, but try a Identity matrix as fourth argument. You
include the World matrix only to reach the (x, y,z) of the model space (or
the reference element space, the object by itself) outside the "world", as
"an eraser" by opposition to "the specific eraser on the desk". In other
words, you got "a typical eraser" (as a mesh, let say), then you "place it"
in the world through the World matrix transform (which can also scale and
shear the object, in addition to translation and rotation). And that is what
you now want. You don't want to 'undo' the world transform.

You would use the World transform to know, as example, which triangle of the
eraser model you got a point.

The mouse point defines a ray, not a single point. The other end of the ray
is with z=1.


Hoping it may help,
Vanderghast, Access MVP
Post by H. Williams
How do you convert mouse XY coordinates to world space?
I tried the following code (passing the mouse coordinates to x & y and
Vector3 v = new Vector3(x,y,z);
device.Transform.World =Matrix.Translation(0f,0f,0f);
v.Unproject(device.Viewport, device.Transform.Projection,
device.Transform.View, device.Transform.World);
Unfortunately v.X & v.Y only correspond correctly at the screen center.
thanks.
Loading...