Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


How to convert screen coordinates to 3D?

This topic is 5422 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post

Link to post
Share on other sites
Check this... It''s in VB.NET but i''m sure that it wouldn''t be a problem.


Share this post

Link to post
Share on other sites
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.


Share this post

Link to post
Share on other sites

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)

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]

Share this post

Link to post
Share on other sites
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.



Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!