Ejemplo n.º 1
0
        /// <summary>
        /// This tells if a sphere is BEHIND, in FRONT, or INTERSECTS a plane, also it's distance
        /// </summary>
        public static MySpherePlaneIntersectionEnum GetSpherePlaneIntersection(ref BoundingSphereD sphere, ref MyPlane plane, out float distanceFromPlaneToSphere)
        {
            //  First we need to find the distance our polygon plane is from the origin.
            float planeDistance = plane.GetPlaneDistance();

            //  Here we use the famous distance formula to find the distance the center point
            //  of the sphere is from the polygon's plane.
            distanceFromPlaneToSphere = (float)(plane.Normal.X * sphere.Center.X + plane.Normal.Y * sphere.Center.Y + plane.Normal.Z * sphere.Center.Z + planeDistance);

            //  If the absolute value of the distance we just found is less than the radius,
            //  the sphere intersected the plane.
            if (Math.Abs(distanceFromPlaneToSphere) < sphere.Radius)
            {
                return(MySpherePlaneIntersectionEnum.INTERSECTS);
            }
            else if (distanceFromPlaneToSphere >= sphere.Radius)
            {
                //  Else, if the distance is greater than or equal to the radius, the sphere is
                //  completely in FRONT of the plane.
                return(MySpherePlaneIntersectionEnum.FRONT);
            }

            //  If the sphere isn't intersecting or in FRONT of the plane, it must be BEHIND
            return(MySpherePlaneIntersectionEnum.BEHIND);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Method returns intersection point between sphere and triangle (which is defined by vertexes and plane).
        /// If no intersection found, method returns null.
        /// See below how intersection point can be calculated, because it's not so easy - for example sphere vs. triangle will 
        /// hardly generate just intersection point... more like intersection area or something.
        /// </summary>
        public static Vector3? GetSphereTriangleIntersection(ref BoundingSphereD sphere, ref MyPlane trianglePlane, ref MyTriangle_Vertices triangle)
        {
            //	Vzdialenost gule od roviny trojuholnika
            float distance;

            //	Zistim, ci sa gula nachadza pred alebo za rovinou trojuholnika, alebo ju presekava
            MySpherePlaneIntersectionEnum spherePlaneIntersection = GetSpherePlaneIntersection(ref sphere, ref trianglePlane, out distance);

            //	Ak gula presekava rovinu, tak hladam pseudo-priesecnik
            if (spherePlaneIntersection == MySpherePlaneIntersectionEnum.INTERSECTS)
            {
                //	Offset ktory pomoze vypocitat suradnicu stredu gule premietaneho na rovinu trojuholnika
                Vector3 offset = trianglePlane.Normal * distance;

                //	Priesecnik na rovine trojuholnika, je to premietnuty stred gule na rovine trojuholnika
                Vector3 intersectionPoint;
                intersectionPoint.X = (float)(sphere.Center.X - offset.X);
                intersectionPoint.Y = (float)(sphere.Center.Y - offset.Y);
                intersectionPoint.Z = (float)(sphere.Center.Z - offset.Z);

                if (GetInsidePolygonForSphereCollision(ref intersectionPoint, ref triangle))		//	Ak priesecnik nachadza v trojuholniku
                {
                    //	Toto je pripad, ked sa podarilo premietnut stred gule na rovinu trojuholnika a tento priesecnik sa
                    //	nachadza vnutri trojuholnika (tzn. sedia uhly)
                    return intersectionPoint;
                }
                else													//	Ak sa priesecnik nenachadza v trojuholniku, este stale sa moze nachadzat na hrane trojuholnika
                {
                    Vector3? edgeIntersection = GetEdgeSphereCollision(ref sphere.Center, (float)sphere.Radius / 1.0f, ref triangle);
                    if (edgeIntersection != null)
                    {
                        //	Toto je pripad, ked sa priemietnuty stred gule nachadza mimo trojuholnika, ale intersection gule a trojuholnika tam
                        //	je, pretoze gula presekava jednu z hran trojuholnika. Takze vratim suradnice priesecnika na jednej z hran.
                        return edgeIntersection.Value;
                    }
                }
            }

            //	Sphere doesn't collide with any triangle
            return null;
        }