How to convert screen coordinates to 3D?

Started by
4 comments, last by ding 20 years, 9 months ago
Hi all, I am trying to convert a position of a mouse click over a DirectX 9.0 Direct3D canvas into a position (ray) in 3D. Basically, I would like to figure out what objects are below the mouse in 3D. Normally, this is done by casting a 3D ray from the mouse and checking what intersects the ray, but has anyone done this with the new, managed DirectX 9.0 in .NET? I am having trouble converting the matrices. The view transformation in DirectX is Vs = Pm * Mworld * Mview * Mproj * Mclip * Mvs / W where Vs are the screen coordinates. I am trying to invert this process, but without luck. Any help would be much appreciated. Since this is a very common thing to do, there must be some built-in function that takes care of it. -- Ding
Advertisement
Check this... It''s in VB.NET but i''m sure that it wouldn''t be a problem.

http://www.chez.com/scribe/en/directx/direct3d_07.htm
<-Sweenie->
Hi again,

thanks, but I found the unproject function in the Vector3 structure which is supposed to be able to do it:

public Vector3 Unproject(object viewport, Matrix projection, Matrix View, Matrix World);

I run it like this:

Vector3 vs = new Vector3(e.X, e.Y, 0.0f);
Vector3 v = Vector3.Unproject(vs, device.Viewport, device.Transform.Projection, device.Transform.View, device.Transform.World);

where e.X and e.Y are my mouse coordinates. However, it doesn''t work and I am not sure that I am giving it the right arguments. Why is the DirectX 9.0 documentation so crappy! There''s absolutely no information in there.

D

In VB.NET

Dim vNear As Vector3
Dim vFar As Vector3

vNear = New Vector3(x, y, 0)
vNear.Unproject(Device.Viewport, Device.Transform.Projection, Device.Transform.View, Device.Transform.World)

vFar = New Vector3(x, y, 1)
vFar.Unproject(Device.Viewport, Device.Transform.Projection, Device.Transform.View, Device.Transform.World)
vFar.Subtract(vNear)

VNear = Ray position
VFar = Ray direction

In C# i suppose it would be...

Vector3 RayPos = New Vector3(e.X,e.Y,0.0f);
RayPos = Vector3.Unproject(RayPos, device.Viewport, device.Transform.Projection, device.Transform.View, device.Transform.World);

Vector3 RayDir = New Vector3(e.X,e.Y,1.0f);
RayDir = Vector3.Unproject(RayDir, device.Viewport, device.Transform.Projection, device.Transform.View, device.Transform.World);
RayDir = Vector3.Subtract(RayDir,RayPos)

(Not sure about the syntax though, I have never coded in C# before, neither in C or C++)
But I think you get the idea.





[edited by - Sweenie on April 30, 2003 7:21:37 AM]
<-Sweenie->
Make sure you''re giving it coordinates relative to the client area of your window.
FYI, I have been doing some experimenting with this, and spurious results occur if you use the RayPos and RayDir with the mesh intersect function without first normalizing the RayDir vector.

Regards

Rael

This topic is closed to new replies.

Advertisement