public AntiIntersection AntiIntersect(Ray ray, SphereBase parentSphere)
        {
            Vector3 l             = GetPosition() - ray.GetStart();     //Vector to sphere center (and direction)
            float   tc            = Vector3.Dot(l, ray.GetDirection()); //Length to ray hit-center
            float   els           = Vector3.Dot(l, l);
            float   radiusSquared = _radius * _radius;

            if (tc < 0 && els > radiusSquared)
            {
                return(AntiIntersection.CreateNullAntiIntersection());
            }
            float dSquared = els - tc * tc; //Squared perpendicular distance from spherecenter to ray (vinkelrätt)

            if (dSquared > radiusSquared)
            {
                return(AntiIntersection.CreateNullAntiIntersection());
            }
            float t1c = (float)Math.Sqrt(radiusSquared - dSquared);
            float tNear, tFar;

            tNear = tc - t1c;
            tFar  = tc + t1c;
            Vector3 pNear           = ray.GetStart() + Vector3.Multiply(ray.GetDirection(), tNear);
            Vector3 pFar            = ray.GetStart() + Vector3.Multiply(ray.GetDirection(), tFar);
            var     normNearTexture = Vector3.Normalize(Vector3.Divide(GetPosition() - pNear, _radius));
            var     normFarTexture  = Vector3.Normalize(Vector3.Divide(GetPosition() - pFar, _radius));

            return(new AntiIntersection(pNear, pFar, normNearTexture, normFarTexture, tNear, tFar, this));
        }
Exemplo n.º 2
0
        private AntiIntersection getFarthestAntiIntersection(float tMin, float tMax, AntiIntersection[] antiSphereIntersectionList)
        {
            AntiIntersection farthestAntiIntersection = AntiIntersection.CreateNullAntiIntersection();

            tMin = (tMin < 0) ? 0 : tMin;
            float tCurrent = tMin;

            //compare the hits in the antilist
            foreach (AntiIntersection antiIntersection in antiSphereIntersectionList)
            {
                //Break here, because the sorting places all the missed intersections in the end
                if (!antiIntersection.IsHit())
                {
                    break;
                }
                if (antiIntersection.isDistanceInside(tCurrent))
                {
                    //Move away the nearest hit to the other side of the AntiSphere
                    tCurrent = antiIntersection.GetTFar();
                    farthestAntiIntersection = antiIntersection;
                }
                if (tCurrent >= tMax)
                {
                    //It went through
                    farthestAntiIntersection = AntiIntersection.CreateInfiniteAntiIntersection();
                    break;
                }
            }
            return(farthestAntiIntersection);
        }
Exemplo n.º 3
0
 public AntiSphereAntiIntersection(AntiSphere antiSphere)
 {
     _antiSphere       = antiSphere;
     _antiIntersection = AntiIntersection.CreateNullAntiIntersection();
 }