Example #1
0
        public static bool collisionLocationForMovingPointFixedAABox(Vector3 origin, Vector3 dir, AxisAlignedBox box, ref Vector3 location, out bool Inside, ref Vector3 normal)
        {
            Inside = true;
            Vector3 MinB = box.Lo;
            Vector3 MaxB = box.Hi;
            Vector3 MaxT = new Vector3(-1.0f, -1.0f, -1.0f);

            // Find candidate planes.
            for (int i = 0; i < 3; ++i)
            {
                if (origin[i] < MinB[i])
                {
                    location[i] = MinB[i];
                    Inside      = false;

                    // Calculate T distances to candidate planes
                    if ((uint)dir[i] != 0)
                    {
                        MaxT[i] = (MinB[i] - origin[i]) / dir[i];
                    }
                }
                else if (origin[i] > MaxB[i])
                {
                    location[i] = MaxB[i];
                    Inside      = false;

                    // Calculate T distances to candidate planes
                    if ((uint)dir[i] != 0)
                    {
                        MaxT[i] = (MaxB[i] - origin[i]) / dir[i];
                    }
                }
            }

            if (Inside)
            {
                // Ray origin inside bounding box
                location = origin;
                return(false);
            }

            // Get largest of the maxT's for final choice of intersection
            int WhichPlane = 0;

            if (MaxT[1] > MaxT[WhichPlane])
            {
                WhichPlane = 1;
            }

            if (MaxT[2] > MaxT[WhichPlane])
            {
                WhichPlane = 2;
            }

            // Check final candidate actually inside box
            if (Convert.ToBoolean((uint)MaxT[WhichPlane] & 0x80000000))
            {
                // Miss the box
                return(false);
            }

            for (int i = 0; i < 3; ++i)
            {
                if (i != WhichPlane)
                {
                    location[i] = origin[i] + MaxT[WhichPlane] * dir[i];
                    if ((location[i] < MinB[i]) ||
                        (location[i] > MaxB[i]))
                    {
                        // On this plane we're outside the box extents, so
                        // we miss the box
                        return(false);
                    }
                }
            }

            // Choose the normal to be the plane normal facing into the ray
            normal             = Vector3.Zero;
            normal[WhichPlane] = (float)((dir[WhichPlane] > 0) ? -1.0 : 1.0);

            return(true);
        }
Example #2
0
        public static float collisionTimeForMovingPointFixedAABox(Vector3 origin, Vector3 dir, AxisAlignedBox box, ref Vector3 location, out bool Inside)
        {
            Vector3 normal = Vector3.Zero;

            if (collisionLocationForMovingPointFixedAABox(origin, dir, box, ref location, out Inside, ref normal))
            {
                return((location - origin).magnitude());
            }
            else
            {
                return(float.PositiveInfinity);
            }
        }
Example #3
0
 public void merge(AxisAlignedBox a)
 {
     Lo = Vector3.Min(_lo, a.Lo);
     Hi = Vector3.Max(Hi, a.Hi);
 }
Example #4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="AxisAlignedBox"/> class using given values from another box instance.
 /// </summary>
 /// <param name="box">A <see cref="AxisAlignedBox"/> instance to take values from.</param>
 public AxisAlignedBox(AxisAlignedBox box)
 {
     _lo = box.Lo;
     _hi = box.Hi;
 }
Example #5
0
 public void merge(AxisAlignedBox a)
 {
     Lo = Lo.Min(a.Lo);
     Hi = Hi.Max(a.Hi);
 }