Esempio n. 1
0
        public Option <RayHitpoint> TryHit(PositionableSphere positionableSphere, Ray ray, RayParameter bottomBoundary,
                                           RayParameter topBoundary)
        {
            var(positionable, sphere) = positionableSphere;
            var originToCenter = ray.Origin - positionable.Translation;
            var a = Vector3.Dot(ray.Direction, ray.Direction);
            var b = 2.0f * Vector3.Dot(originToCenter, ray.Direction);
            var c = Vector3.Dot(originToCenter, originToCenter) -
                    sphere.Radius * sphere.Radius;
            var discriminant = b * b - 4 * a * c;

            if (discriminant < 0)
            {
                return(Option <RayHitpoint> .Empty);
            }

            var temp = new RayParameter((-b - (float)Math.Sqrt(discriminant)) / (2.0f * a));

            if (temp < topBoundary.Value && temp > bottomBoundary.Value)
            {
                var hitPoint = ray.PointAt(temp);
                var normal   = GetNormalAtPoint(hitPoint, positionableSphere);
                return(new Option <RayHitpoint>(new RayHitpoint(hitPoint, normal, temp)));
            }

            temp = new RayParameter((-b + (float)Math.Sqrt(discriminant)) / (2.0f * a));
            if (temp < topBoundary.Value && temp > bottomBoundary.Value)
            {
                var hitPoint = ray.PointAt(temp);
                var normal   = GetNormalAtPoint(hitPoint, positionableSphere);
                return(new Option <RayHitpoint>(new RayHitpoint(hitPoint, normal, temp)));
            }

            return(Option <RayHitpoint> .Empty);
        }
Esempio n. 2
0
 private static Vector3 GetNormalAtPoint(Vector3 point, PositionableSphere sphereEntity)
 {
     var(positionable, sphere) = sphereEntity;
     return((point - positionable.Translation) / sphere.Radius);
 }
Esempio n. 3
0
 public Bounds Compute(PositionableSphere positionableSphere)
 {
     var(positionable, sphere1) = positionableSphere;
     return(new Bounds(positionable.Translation - new Vector3(sphere1.Radius),
                       positionable.Translation + new Vector3(sphere1.Radius)));
 }
Esempio n. 4
0
 public HitableSphere(PositionableSphere sphere, IHitableSphereComputer computer)
 {
     _sphere   = sphere;
     _computer = computer;
 }