Jump to content
  • Advertisement
Sign in to follow this  

Mesh.intersect problem

This topic is 3348 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

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

Share this post

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

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 :)

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!