Sign in to follow this  
Feadur

Mouse collision detection... how to?

Recommended Posts

Feadur    122
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 this post


Link to post
Share on other sites
Feadur    122
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:

[CODE]
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);
}
[/CODE]


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 this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this