Esempio n. 1
0
        public Vector3 GetClosestPointToPoint(Vector3 point)
        {
            Vector3 fromCenterToPoint = point - Center;
            Vector3 closestPoint      = Center;
            Vector3 scaledExtents     = ScaledExtents;

            Vector3[] localAxes = TransformMatrix.GetAllAxes();
            for (int axisIndex = 0; axisIndex < 3; ++axisIndex)
            {
                Vector3 localAxis  = localAxes[axisIndex];
                float   axisExtent = scaledExtents[axisIndex];

                float projection = Vector3.Dot(localAxis, fromCenterToPoint);
                if (projection > axisExtent)
                {
                    projection = axisExtent;
                }
                else if (projection < -axisExtent)
                {
                    projection = -axisExtent;
                }

                closestPoint += localAxis * projection;
            }

            return(closestPoint);
        }