Mesh.intersect problem
hello friends
I'm trying to find XYZ point of mouse picking on a sphere, using Visual Studio 2008 (C#) & MDX. I use Mesh.intersect but it doesn't return the correct intersection point, what is the problem? how can I implement this intersection?
I think I should solve the equation for a ray intersection with a sphere, but how can I do that? how can I make ray pos (which should be the mouse pos on screen) and how can I make my vectors?
any sample or hints?
thanks
This is how i create my ray object. There are some variables in this method which you should already have. Some assembly required :) The ray is a simple "public Ray ray" object. It's updated every frame and belongs to my Camera class.
Your can find your mouse position (mousePos below) by using "PointToClient(MousePosition)"
edit: forgot to mention that i use SlimDX.
To check if the ray actually intersects anything in your Mesh:
The variable 'newposition' will be the exact spot where the ray intersects your model. intersectInformation contains some more info aswell, and you can use faceIndex to track down which polygon it intersects. Sorry about messy sources - i just did a cut'n'paste :)
Your can find your mouse position (mousePos below) by using "PointToClient(MousePosition)"
edit: forgot to mention that i use SlimDX.
public void UpdateRay(Point mousePos) { mousePosition = mousePos; mousePosition.X -= clientBorder.X; mousePosition.Y -= clientBorder.Y; mouseScreenSpace.X = (((2.0f * mousePosition.X) / screenWidth) - 1) / matProj.M11; mouseScreenSpace.Y = -(((2.0f * mousePosition.Y) / screenHeight) - 1) / matProj.M22; mouseScreenSpace.Z = 1.0f; // Get the inverse view matrix Matrix matWorldView = matWorld * matView; Matrix m; Matrix.Invert(ref matWorldView, out m); // Transform the screen space pick ray into 3D space ray.Direction.X = mouseScreenSpace.X * m.M11 + mouseScreenSpace.Y * m.M21 + mouseScreenSpace.Z * m.M31; ray.Direction.Y = mouseScreenSpace.X * m.M12 + mouseScreenSpace.Y * m.M22 + mouseScreenSpace.Z * m.M32; ray.Direction.Z = mouseScreenSpace.X * m.M13 + mouseScreenSpace.Y * m.M23 + mouseScreenSpace.Z * m.M33; ray.Position.X = m.M41; ray.Position.Y = m.M42; ray.Position.Z = m.M43; }
To check if the ray actually intersects anything in your Mesh:
Vector3 newposition;if (myMesh.Intersects(Camera.ray, out rayDistance, out faceIndex, out intersectInformation)){ isIntercepted = true; newposition = Camera.ray.Position + (Camera.ray.Direction * intersectInformation[0].Distance);}
The variable 'newposition' will be the exact spot where the ray intersects your model. intersectInformation contains some more info aswell, and you can use faceIndex to track down which polygon it intersects. Sorry about messy sources - i just did a cut'n'paste :)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement