Skip to main content
GameDev.net gamedev.net
🔒 Locked

Keeping an AABB inside another AABB?

Started by Spa8nky Sep 3, 2010 at 6:36 AM 2 replies 1.5k views
Original Post
Spa8nky
Spa8nky
I would like to keep an AABB 'A' inside the bounds of another AABB 'B'.

First of all I run a test to see if 'A' is fully contained inside 'B':

        public bool Contains(CD_AABB aABB)        {            return                 MinPoint.X <= aABB.MinPoint.X &&                aABB.MaxPoint.X <= MaxPoint.X &&                MinPoint.Y <= aABB.MinPoint.Y &&                aABB.MaxPoint.Y <= MaxPoint.Y &&                MinPoint.Z <= aABB.MinPoint.Z &&                aABB.MaxPoint.Z <= MaxPoint.Z;        }


Now in order to find the penetration/overlap value I can't use my standard AABB/AABB test as that pushes the box 'A' outside 'B' rather than keeping it inside.

How can I find the penetration value that keeps 'A' inside 'B'?
XeonXT
XeonXT
public void KeepInside(CD_AABB aABB){	int dif;	if (aABB.MinPoint.X < MinPoint.X)	{		dif = MinPoint.X - aABB.MinPoint.X;		aABB.MinPoint.X += dif;		aABB.MaxPoint.X += dif;	}	else if (aABB.MaxPoint.X > MaxPoint.X)	{		dif = MaxPoint.X - aABB.MaxPoint.X;		aABB.MinPoint.X += dif;		aABB.MaxPoint.X += dif;	}	if (aABB.MinPoint.Y < MinPoint.Y)	{		dif = MinPoint.Y - aABB.MinPoint.Y;		aABB.MinPoint.Y += dif;		aABB.MaxPoint.Y += dif;	}	else if (aABB.MaxPoint.Y > MaxPoint.Y)	{		dif = MaxPoint.Y - aABB.MaxPoint.Y;		aABB.MinPoint.Y += dif;		aABB.MaxPoint.Y += dif;	}	if (aABB.MinPoint.Z < MinPoint.Z)	{		dif = MinPoint.Z - aABB.MinPoint.Z;		aABB.MinPoint.Z += dif;		aABB.MaxPoint.Z += dif;	}	else if (aABB.MaxPoint.Z > MaxPoint.Z)	{		dif = MaxPoint.Z - aABB.MaxPoint.Z;		aABB.MinPoint.Z += dif;		aABB.MaxPoint.Z += dif;	}}


Note that I make the assumption that you do not want to resize 'A'. Therefore, 'B' must always be larger than 'A' (on all axes) for this to work 100% of the time.
Spa8nky
Spa8nky
That works well, thank you for that.

        public void KeepSmallerAABBInside(CD_AABB b)        {            Vector3 minA = MinPoint;            Vector3 maxA = MaxPoint;            Vector3 minB = b.MinPoint;            Vector3 maxB = b.MaxPoint;            Vector3 penetration = new Vector3();            if (minB.X < minA.X)            {                penetration.X = minA.X - minB.X;            }            else if (maxB.X > maxA.X)            {                penetration.X = maxA.X - maxB.X;            }            if (minB.Y < minA.Y)            {                penetration.Y = minA.Y - minB.Y;            }            else if (maxB.Y > maxA.Y)            {                penetration.Y = maxA.Y - maxB.Y;            }            if (minB.Z < minA.Z)            {                penetration.Z = minA.Z - minB.Z;            }            else if (maxB.Z > maxA.Z)            {                penetration.Z = maxA.Z - maxB.Z;            }            b.Position += penetration;        }
XeonXT
XeonXT
Just a quick note on that last bit you posted - make sure penetration is initialized to (0,0,0), otherwise you may get some strange behavior when you try to add it if some of the components are undefined. That is, assuming it doesn't automatically initialize to zero.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.