示例#1
0
        /// <summary>
        /// return the closest point on the disc from K, if the GravityInfo permit it
        /// </summary>
        /// <param name="k"></param>
        /// <param name="canApplyGravity"></param>
        /// <param name="gravityDisc"></param>
        /// <returns></returns>
        public bool GetClosestPointOnDiscIfWeCan(Vector3 k, GravityOverrideDisc gravityDisc, out Vector3 closestPoint)
        {
            closestPoint = Vector3.zero;

            if (!gravityDisc.CanApplyGravity || (!AllowBottom && !_plane.IsAbove(k)))
            {
                return(false);
            }

            //project point to a plane
            Vector3 kProjected = ExtPlane.ProjectPointInPlane(_plane, k);

            //if dist² < radius², this projected point is inside the circle
            float distSquared   = (kProjected - _plane.Point).sqrMagnitude;
            bool  isInsideShape = distSquared < _radiusSquared;

            if (isInsideShape)
            {
                if (!gravityDisc.Face)
                {
                    return(false);
                }
                closestPoint = kProjected;
                return(true);
            }

            if (!gravityDisc.Borders)
            {
                return(false);
            }

            //return the closest point on the circle (extremity of the disc)
            closestPoint = _plane.Point + (kProjected - _plane.Point).FastNormalized() * _radius;
            return(true);
        }
示例#2
0
        /// <summary>
        /// Get the intersection between a line and a plane.
        //  If the line and plane are not parallel, the function outputs true, otherwise false.
        /// </summary>
        /// <param name="intersection"></param>
        /// <param name="ray"></param>
        /// <param name="plane"></param>
        /// <returns></returns>
        public static bool LinePlaneIntersection(out Vector3 intersection, Ray ray, ExtPlane plane)
        {
            float   length;
            float   dotNumerator;
            float   dotDenominator;
            Vector3 vector;

            intersection = Vector3.zero;

            //calculate the distance between the linePoint and the line-plane intersection point
            dotNumerator   = Vector3.Dot((plane.Point - ray.origin), plane.Normal);
            dotDenominator = Vector3.Dot(ray.direction, plane.Normal);

            //line and plane are not parallel
            if (dotDenominator != 0.0f)
            {
                length = dotNumerator / dotDenominator;

                //create a vector from the linePoint to the intersection point
                vector = ExtVector3.SetVectorLength(ray.direction, length);

                //get the coordinates of the line-plane intersection point
                intersection = ray.origin + vector;

                return(true);
            }

            //output not valid
            else
            {
                return(false);
            }
        }
示例#3
0
        /// <summary>
        /// Return the closest point on the disc from k
        /// </summary>
        public bool GetClosestPointOnDisc(Vector3 k, ref Vector3 closestPoint)
        {
            closestPoint = Vector3.zero;
            if (!AllowBottom && !_plane.IsAbove(k))
            {
                return(false);
            }

            //project point to a plane
            Vector3 kProjected = ExtPlane.ProjectPointInPlane(_plane, k);

            //if dist² < radius², this projected point is inside the circle
            float distSquared   = (kProjected - _plane.Point).sqrMagnitude;
            bool  isInsideShape = distSquared < _radiusSquared;

            if (isInsideShape)
            {
                closestPoint = kProjected;
            }
            else
            {
                closestPoint = _plane.Point + (kProjected - _plane.Point).FastNormalized() * _radius;
            }
            return(true);
        }
示例#4
0
        /// <summary>
        /// get intersection between line and plane
        /// </summary>
        /// <param name="rayVector"></param>
        /// <param name="rayPoint"></param>
        /// <param name="plane"></param>
        /// <returns></returns>
        public static Vector3 PlaneLineIntersection(Ray ray, ExtPlane plane)
        {
            var diff  = ray.origin - plane.Point;
            var prod1 = Vector3.Dot(diff, plane.Normal);
            var prod2 = Vector3.Dot(ray.direction, plane.Normal);
            var prod3 = prod1 / prod2;

            return(ray.origin - ray.direction * prod3);
        }
示例#5
0
        /// <summary>
        /// return the closest point on the circle from k
        /// </summary>
        /// <param name="k"></param>
        /// <returns></returns>
        public Vector3 GetClosestPointOnCircle(Vector3 k)
        {
            //project point to a plane
            Vector3 kProjected = ExtPlane.ProjectPointInPlane(_plane, k);

            //return the closest point on the circle
            Vector3 pointExtremity = _plane.Point + (kProjected - _plane.Point).FastNormalized() * _radius;

            return(pointExtremity);
        }
示例#6
0
        /// <summary>
        /// assume k is NOT on the same plane as the circle, we first do a projection
        /// √( | xp−xc |² +| yp−yc |²) < r
        /// </summary>
        /// <param name="k"></param>
        /// <returns></returns>
        public bool IsInsideShapeWithProjection(Vector3 k)
        {
            if (!AllowBottom && !_plane.IsAbove(k))
            {
                return(false);
            }

            Vector3 kProjected  = ExtPlane.ProjectPointInPlane(_plane, k);
            float   distSquared = (kProjected - _plane.Point).sqrMagnitude;

            return(distSquared < _radiusSquared);
        }
示例#7
0
 public static Vector3 ProjectPointInPlane(ExtPlane plane, Vector3 pointToProject)
 {
     return(pointToProject - (Vector3.Project(pointToProject - plane.Point, plane.Normal.normalized)));
 }
示例#8
0
 public static bool PlanePlaneIntersection(ExtPlane plane1, ExtPlane plane2, out Vector3 linePoint, out Vector3 lineVec)
 {
     return(PlanePlaneIntersection(plane1.Point, plane1.Normal, plane2.Point, plane2.Normal, out linePoint, out lineVec));
 }