Skip to main content
gamedev.net
🔒 Locked 🎮 Unity

Mesh.Intersect problem

Started by spiffycrony 3 replies 1.2k views
Original Post
spiffycrony
spiffycrony
I am creating a cube and a cylinder using Mesh.Box and Mesh.Cylinder respectivly(Directx 9). I am trying to pick the closest vertex of the mesh. I am using Mesh.Intersect to do this. The code is something as below sub function Intersect() 'x, y are mouse coordinates 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, meshmatworld) 'compute point on far clip plane at cursor vIn.Z = 1 vFar = Microsoft.DirectX.Vector3.Unproject(vIn, viewport, proj, view, meshmatworld) 'Comput direction vector vDir = Microsoft.DirectX.Vector3.Subtract(vFar, vNear) If Mesh.Intersect(vNear, vDir, ClosestHit) = True Then 'Find the closest vertex from the ClosestHit.FaceIndex(hit triangle) endif end sub Since I am using the Mesh.Intersect, I assume i dont have to do any transformation of the mesh vertices before I call the Mesh.Interset function. Is there anything obvious i am missing. Because this code works for the cube(most of the times and rarely it draws a picked point out of the cube) and for the cylinder it doesn't work at all(always the picked vertex is somewhere out of the cylinder).
remigius
remigius
Well, the code you posted only seems to check a single mesh, so it's hard to tell if you are doing your picking/intersecting correctly. When you say you assume you're not using any transformations, this would mean that both the box and the cilinder are displayed at (0,0,0), which I doubt is the case. When picking, you should loop over all meshes (or at least all visible meshes), unproject the mouse ray for each mesh with its world matrix and only then perform the intersection test.

Maybe this here post clears things up a bit. Hope this answers your question :)
spiffycrony
spiffycrony
The link was useful. I found out where the problem is. The problem is that before rendering the cylinder is rotate say for 90 along yaxis. The cube isnt rotated. I think this is why it worked for the cube and not for the cylinder(I confirmed this by removing the rotation and the picking works fine for both cube and cylinder). Its only part of the code so i do loop over all the meshes and try to get the ray in the local object space(without the rotation). Although i found the problem, I am not sure of how to fix this? Should I also rotate the ray by the rotation angle for the cylinder? If i do this the hittest doesnt find the correct triangle?
remigius
remigius
You should use the complete world matrix for each of the meshes when unprojecting, there's nothing special about rotation in this regard. If your cylinder is only rotated, just use the rotation matrix as the world matrix for the unproject. Similarly, if your world matrix should consist of a scale, rotation and translation matrix, you also use this entire matrix to do your unprojection.
spiffycrony
spiffycrony
Hi remigius

Thanks for the explanations. I got your point. There was problem while drawing the picked point as well.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.