Original Post
Hello guys, I am stuck on picking a certain point on a plane which is located anywhere on the z axis of my coordinate system. The coordinate system can be zoomed and rotated by using my chase camera. Here is what it looks like:
The problem is that my coordinates seem to be messed up and I cannot figure out why. The camera is rotated and zoomed by setting up the projection and view matrices in this way: Rotating the camera around my coordinate system works fine. I use this code to determine if a ray intersects a certain plane: Sometimes it seems that the plane is located flat in the upper right corner of the screen... Can please anybody have a look at this and tell me what is wrong? I also tried intersection using a BoundingBox: BoundingBox box = new BoundingBox(new Vector3(0), new Vector3(1)); It seems that the coordinates are not translated correctly. Any help is appreciated! Best Regards OppiXP
The problem is that my coordinates seem to be messed up and I cannot figure out why. The camera is rotated and zoomed by setting up the projection and view matrices in this way:
projectionMatrix = Matrix.PerspectiveFovLH(
(float)Math.PI / 4,
(float)ClientRect.Width / ClientRect.Height,
0.001f,
20f);
Matrix trans = Matrix.Translation(0.0f, 0.0f, camDistance);
Matrix rot = Matrix.Multiply(Matrix.RotationY(camAngleHorizontal), Matrix.Identity);
rot = Matrix.Multiply(Matrix.RotationX(camAngleVertical), rot);
viewMatrix = Matrix.Multiply(trans, rot);
viewMatrix = Matrix.Invert(viewMatrix);
protected override void MouseMove(System.Windows.Forms.MouseEventArgs e)
{
base.MouseMove(e);
Plane plane = new Plane(new Vector3(1f, 0, 0), 0);
DX.Instance.SetWorldMatrix(Matrix.Identity, Matrix.Identity);
Vector3 nearPlane = Vector3.Unproject(new Vector3((float)e.X, (float)e.Y, currentCamDistance), DX.Instance.ViewPort, DX.Instance.CurrentProjectionMatrix, DX.Instance.CurrentViewMatrix, DX.Instance.CurrentWorldMatrix);
Vector3 farPlane = Vector3.Unproject(new Vector3((float)e.X, (float)e.Y, 20f), DX.Instance.ViewPort, DX.Instance.CurrentProjectionMatrix, DX.Instance.CurrentViewMatrix, DX.Instance.CurrentWorldMatrix);
Vector3 direction = farPlane - nearPlane;
direction.Normalize();
Ray ray = new Ray(nearPlane, direction);
float distance;
Console.WriteLine(Ray.Intersects(ray, plane, out distance));
}