Ejemplo n.º 1
0
        public override bool RayIntersects(Ray ray, out float distance)
        {
            if (CollisionType == CollisionType.Box)
            {
                return(RayIntersectsBoundingBox(ray, out distance));
            }

            var pos    = Vector3.TransformPosition(Vector3.Zero, Transform);
            var sphere = new BSphere(pos, Scale.X / 2f);

            return(Ray.IntersectsSphere(ray, sphere, out distance));
        }
Ejemplo n.º 2
0
        public static bool IntersectsSphere(Ray ray, BSphere sphere, out float distance)
        {
            distance = float.NaN;

            var a = Vector3.Dot(ray.Direction, ray.Origin - sphere.Center);
            var b = (ray.Origin - sphere.Center).Length;
            var c = (a * a) - (b * b) + (sphere.Radius * sphere.Radius);

            if (c < 0f)
            {
                return(false);
            }

            if (NearZero(c))
            {
                distance = -a;
                return(true);
            }

            var d = (float)Math.Sqrt(c);

            distance = Math.Min(-a + d, -a - d);//+|-
            return(true);
        }