Ejemplo n.º 1
0
        // Returns false is there is no intersection
        private bool GetMousePositionOnXZPlane(Point mousePosition, out Point3D intersectionPoint)
        {
            Point3D  rayOrigin;
            Vector3D rayDirection;

            // Calculate the 3D ray that goes from the mouse position into the 3D scene
            bool success = Camera1.CreateMouseRay3D(mousePosition, out rayOrigin, out rayDirection);

            bool hasIntersection;

            intersectionPoint = new Point3D();

            if (success && rayDirection.Y != 0)
            {
                // Get intersection of ray and xz plane - defined by N=(0,1,0), P=(0,0,0)
                // Because we use the xz plane the intersection formula is very simple:
                double t = (-rayOrigin.Y) / rayDirection.Y;

                if (t > 0) // we have an intersection
                {
                    // Get the 3d point of intersection
                    intersectionPoint = rayOrigin + t * rayDirection;

                    // Skip intersections that are far away
                    if (intersectionPoint.Z > HALF_MOUSE_LINE_LENGTH || intersectionPoint.Z < -HALF_MOUSE_LINE_LENGTH || intersectionPoint.X > HALF_MOUSE_LINE_LENGTH || intersectionPoint.X < -HALF_MOUSE_LINE_LENGTH)
                    {
                        hasIntersection = false;
                    }
                    else
                    {
                        hasIntersection = true;
                    }
                }
                else
                {
                    hasIntersection = false;
                }
            }
            else
            {
                hasIntersection = false;
            }

            return(hasIntersection);
        }