A way of telling if your mouse is over a mesh?

Phikwe

Member
Joined
Sep 18, 2004
Messages
6
Hey People! New to this forum... I want to know if anyone here knows how to know when for example your mouse if over a particula mesh on the screen... I know how to use Mesh.Intersect to find out if a ray intersect a mesh and at what distace but I really want to know if my mouse if pointing to an object in the a 3D scene. I suppose I could figure out a way to put a ray where the camera is and point it in the direction the camera is pointing with an offset of where the mouse is. It will have to do with the projectiong matrix also.. But I was tryiing to find an easyer way of doing it... Maybe there is a function that is simular to Mesh.Intersect but tells if a point on the screen intersect or touches a mesh...
haha I really hope you guys understood that!
This is a cool forume and I am glad to know people are having simular problems to mine!!
PLEASE ANSWER if you know a way I could do this!
Thanks ALOT PEOPLE!
PhikweG
 
Found the Answer!

This is what I wrote in the other tread! Just thought I should put it here too in case people were look! :)

Hey Guys! I found it for real now! It was not as hard as I thought it would be...
I made a little function that does it.. Works like a charm! What you do is give it the X and Y coordinates and the mesh and it tells you if the (x,y) is over your mesh... If you want you could call ClosestHit.Dist to see how far your mesh is from the screen.
Here is the function

Determines if an (x,y) coordinate [could be of your mouse] in screen space is over a mesh
Function DoesMouseHitMesh(ByVal meshAs mesh, ByVal x As Single, ByVal y As Single) As Boolean
Dim viewport As Viewport
Dim world As Matrix
Dim proj As Matrix
Dim view As Matrix

Dim vIn As Vector3, vNear As Vector3, vFar As Vector3, vDir As Vector3
Dim ClosestHit As IntersectInformation

viewport = device.Viewport
world = device.Transform.World Now here you have to remember to get the world matrix that will be used on your mesh...
proj = device.Transform.Projection
view = device.Transform.View

vIn.X = x : vIn.Y = y

Compute point on Near Clip plane at cursor
vIn.Z = 0
vNear = Microsoft.DirectX.Vector3.Unproject(vIn, viewport, proj, view, world)

compute point on far clip plane at cursor
vIn.Z = 1
vFar = Microsoft.DirectX.Vector3.Unproject(vIn, viewport, proj, view, world)

Comput direction vector
vDir = Microsoft.DirectX.Vector3.Subtract(vFar, vNear)

If mesh.Intersect(vNear, vDir, ClosestHit) = True Then
Return True
End If
End Function

It works GRATE men!
Later! Tell me if you need help in using it or dont understnad somthing...
 
Back
Top