Public Group

# Mouse collision detection... how to?

This topic is 4081 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi there, I'm pretty shure this question has been asked in another thread, but after 20 minutes i haven't found anything. The thing is, how do I know the intersecction between the ray created by the mouse and a plane? I've this code from riemers.net website: int mouseX = mouseState.X; int mouseY = mouseState.Y; Vector3 nearsource = new Vector3((float)mouseX, 0f,(float)mouseY); Vector3 farsource = new Vector3((float)mouseX, 1f, (float)mouseY); Matrix world = Matrix.CreateTranslation(0, 0, 0); Vector3 nearPoint = graphics.GraphicsDevice.Viewport.Unproject( nearsource, Camara1.MatrizProyeccion , Camara1.MatrizVista , world); Vector3 farPoint = graphics.GraphicsDevice.Viewport.Unproject(farsource, Camara1.MatrizProyeccion, Camara1.MatrizVista, world); // Create a ray from the near clip plane to the far clip plane. Vector3 direction = farPoint - nearPoint; So I undertand that Vector3 direction is a line between near and far clipping plane... Assuming this code es correct enought. How do I calculate the intersection with my plane ?(which actually is (0,1,0))

##### Share on other sites
Ok I've found something that could be useful. A method to find the ray , and one to find the distance between the ray's origin anf the plane:

Ray GetPickRay(MouseState mouseState) { int mouseX = mouseState.X; int mouseY = mouseState.Y; Vector3 nearsource = new Vector3((float)mouseX, (float)mouseY, 0f); Vector3 farsource = new Vector3((float)mouseX, (float)mouseY, 1f); Matrix world = Matrix.CreateTranslation(0, 0, 0); Vector3 nearPoint = graphics.GraphicsDevice.Viewport.Unproject(nearsource, Camara1.MatrizProyeccion , Camara1.MatrizVista , world); Vector3 farPoint = graphics.GraphicsDevice.Viewport.Unproject(farsource, Camara1.MatrizProyeccion , Camara1.MatrizVista , world); // Create a ray from the near clip plane to the far clip plane. Vector3 direction = farPoint - nearPoint; direction.Normalize(); Ray pickRay = new Ray(nearPoint, direction); return pickRay; }float DistanceRayPlane(Vector3 vRayOrigin, Vector3 vnRayVector, Vector3 vnPlaneNormal,float planeD) { float cosAlpha; float deltaD; cosAlpha =Vector3.Dot( vnRayVector, vnPlaneNormal); // parallel to the plane (alpha=90) if (cosAlpha == 0) return -1.0f; deltaD = planeD - Vector3.Dot(vRayOrigin, vnPlaneNormal); return (deltaD / cosAlpha); }

So it seems the only thing I need is to add this distance to ray's origin, in ray's direction.

intersection = (distance * ray.direction) + ray.position;

Is it correct?

And so, I have lots of MouseChecks, with a single mouse click. Is there any way to solve it? Because each time I click, the model is moving and re-checking the collision with undesired results.

##### Share on other sites
Well I finally got it. If somenone's is interested I can paste the code here.

• 10
• 19
• 14
• 19
• 15