Archived

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

ahbonk

DirectX 9 Screen to Object Space

Recommended Posts

I have this application where I have a mouse and when I click, I want to know the intersection point of where the mouse is pointing and a plane in which (y = 0, more like a plane on the ground). So I typed in the following script: Public Sub Move(ByVal x As Single, ByVal y As Single, ByVal XD3DDev As Device) Dim vNear As Vector3 Dim vFar As Vector3 Dim Norm As Vector3 Dim RayDir As Vector3 Dim Closest As IntersectInformation Dim t As Single Dim IntersectPoint As Vector3 vNear = New Vector3(x, y, 0) vNear.Unproject(XD3DDev.Viewport, XD3DDev.Transform.Projection, XD3DDev.Transform.View, XD3DDev.Transform.World) vFar = New Vector3(x, y, 1) vFar.Unproject(XD3DDev.Viewport, XD3DDev.Transform.Projection, XD3DDev.Transform.View, XD3DDev.Transform.World) vFar.Subtract(vNear) Norm = New Vector3(0, 1, 0) t = (0 - Vector3.Dot(Norm, vNear)) / (Vector3.Dot(Norm, vFar)) IntersectPoint = Vector3.Add(vNear, Vector3.Multiply(vFar, t)) posX = IntersectPoint.X posZ = IntersectPoint.Z End Sub The problem is, vNear always comes out with two different values, although I have clicked at the same place. Thus, the resuling posX and posZ is always changing although the mouse cursor is at the same place. What do I do? BoNKing the nation!

Share this post


Link to post
Share on other sites