Lennart Ericson

asked on

# Calculating the intersection coordinates of a Ray and a Plane

Hi all, this is my first post here Smile

I'm coding a game for my bachelors thesis. It's a simple idea that will play something like Diablo, where you have a camera centered on the character and you click on the screen to make him move. That's just the problem, the game is 3d and we are not using a grid system.

I'm having a hard time translating the coordinates of the mouse click to a point in the 3d space on the Y=0 plane.

So far, i've done this: <code>

I've used a tutorial for selecting objects on screen for this, and tried to modify it so that I will get the point with the coordinates I can move my character to.

What that gets me are two points in the 3d space, one representing the mouse, I think, and the other the direction it's pointing. Then I draw a ray that goes from the mouse through that point and continues on forever. One could imagine it beeing a line drawn from my eyes, passing through the mouse cursor into the 3d space.

Then I create a plane with the coordinates 0 0 0 0, and check if the line interesects with it. It always will unless it's pointing upward, which is not possible. If they intersect, I think i get the distance between the nearpoint from which i draw the ray, to the point where it colides with the plane. Can anyone confirm this? I'm unsure of this point.

The problem is that I now need to find the coordinates for that point. I tried calculating it with triangels and what not, but it's just too advanced math for me.

Is there anyone with a similar problem but a different solution? Or some method that can help me? Perhaps someone very good at math willing to help?

I'm coding a game for my bachelors thesis. It's a simple idea that will play something like Diablo, where you have a camera centered on the character and you click on the screen to make him move. That's just the problem, the game is 3d and we are not using a grid system.

I'm having a hard time translating the coordinates of the mouse click to a point in the 3d space on the Y=0 plane.

So far, i've done this: <code>

I've used a tutorial for selecting objects on screen for this, and tried to modify it so that I will get the point with the coordinates I can move my character to.

What that gets me are two points in the 3d space, one representing the mouse, I think, and the other the direction it's pointing. Then I draw a ray that goes from the mouse through that point and continues on forever. One could imagine it beeing a line drawn from my eyes, passing through the mouse cursor into the 3d space.

Then I create a plane with the coordinates 0 0 0 0, and check if the line interesects with it. It always will unless it's pointing upward, which is not possible. If they intersect, I think i get the distance between the nearpoint from which i draw the ray, to the point where it colides with the plane. Can anyone confirm this? I'm unsure of this point.

The problem is that I now need to find the coordinates for that point. I tried calculating it with triangels and what not, but it's just too advanced math for me.

Is there anyone with a similar problem but a different solution? Or some method that can help me? Perhaps someone very good at math willing to help?

```
MouseState mouseState = Mouse.GetState();
if (mouseState.LeftButton == ButtonState.Pressed)
{ // checks if someone presses the left mous button
int mouseX = mouseState.X;
int mouseY = mouseState.Y;
// creates a nearsoure and farsource and...
Vector3 nearsource = new Vector3((float)mouseX, (float)mouseY, 0f);
Vector3 farsource = new Vector3((float)mouseX, (float)mouseY, 1f);
// ...translates them into 3d coordinates
Matrix world = Matrix.CreateTranslation(0, 0, 0);
Vector3 nearPoint = graphics.GraphicsDevice.Viewport.Unproject(
nearsource, cameraProjectionMatrix, cameraViewMatrix, world);
Vector3 farPoint = graphics.GraphicsDevice.Viewport.Unproject(
farsource, cameraProjectionMatrix, cameraViewMatrix, world);
// Calculates a vector that is in the direction where the mouse
// click would be in 3d coordinates
Vector3 direction = farPoint - nearPoint;
direction.Normalize();
// Draws a ray from the fictional mouse cursor in 3d space thru
// the direction point in space
Ray pickRay = new Ray(nearPoint, direction);
// Checks where the ray intersects with a plane at Y=0.0F
// and returns the distance between the fictional mouse,
// and the point on that plane it intersects with. right?
float length = (float)pickRay.Intersects(new Plane(new Vector4(0f,0f,0f,0f)));
/**
*
* Just some code i started working on to calculate the coordinates
* of the intersection point
*
int XCoordinate = nearPoint.X + (
Math.Sqrt(
Math.Pow(length, 2) + Math.Pow(length, 2)));
int XCoordinate = nearPoint.Z + (
Math.Sqrt(
Math.Pow(length, 2) + Math.Pow(length, 2)));
**/
}
```

ASKER CERTIFIED SOLUTION

membership

Create an account to see this answer

Signing up is free. No credit card required.