# Update AABB (Center-Radius Representation)

## Recommended Posts

It doesn't work. Center and Radius values go to a large value. Can someone help me?

AABB::AABB(XMFLOAT3 c, XMFLOAT3 r)
{
Center[0] = c.x;
Center[1] = c.y;
Center[2] = c.z;

}

void AABB::UpdateAABB(float orientation[3][3], float translation[3])
{
float C[3], R[3];
for (int i=0; i<3; i++)
{
C[i] = translation[i];
R[i] = 0.0f;
for (int j=0; j<3; j++)
{
C[i] += orientation[i][j] * Center[j];
R[i] += abs(orientation[i][j]) * Radius[j];
}
}
Center[0] = C[0];
Center[1] = C[1];
Center[2] = C[2];

}

XMFLOAT3 AABB::GetCenter()
{
return XMFLOAT3(Center[0], Center[1],Center[2]);
}

{
}

##### Share on other sites

Hi. What is the goal of the update function, should it move the aabb into another space? (E.g worldspace)

##### Share on other sites

It modifies the orientation and the position of aabb according to the rigidbody it is attached with.

Edited by isu diss

##### Share on other sites

First off, you seem to have a bounding volume named AABB.  But an AABB typically has a XMFLOAT3 min, max; aka a bounding box.  No radius involved.

Why are you using XMFLOAT3 but also not using it at the same time?  Clearly Center and Radius should just be XMFLOAT3's.

In your constructor, it should just be Center = c; Radius = r; as both Center and Radius should be XMFLOAT3's (you don't provide the code so they might be, just don't seem it).  Also, get rid of GetCenter() and GetRadius and just leave the variables exposed as public.  This should honestly be struct not a class to begin with anyway (again assuming since you didn't provide all the code).

Also, since you're already using the Direct X math library... why are you not actually using it?  https://docs.microsoft.com/en-us/windows/desktop/api/directxmath/ns-directxmath-xmmatrix

It has matrix/vector functions for this very reason.

##### Share on other sites

It seems like you're not keeping the original ("local") Center and Radius anywhere.  If you use Center and Radius both as input and as output, then yeah, after a few updates your values are going to grow to much larger values.

##### Share on other sites
6 hours ago, CrazyCdn said:

But an AABB typically has a XMFLOAT3 min, max; aka a bounding box.  No radius involved.

Have you heard of center-halfwidth extents(radii) representation? It is the most efficient one among the three. I read the book "Real-Time Collision Detection" by Christer Ericson.

6 hours ago, Nypyren said:

It seems like you're not keeping the original ("local") Center and Radius anywhere.  If you use Center and Radius both as input and as output, then yeah, after a few updates your values are going to grow to much larger values.

Thanks for your suggestion.

##### Share on other sites

Hi,

To update the AABB by that transformation, you want to apply the transform normally to the center.

For the radius vector, you apply a modified version of the transformation in which each element of the transformation is the absolute value of the inputted transformation. Technically you only need to apply the absolute values to the orientation matrix, but you can also apply it to a composition of the scale/rotation/translation matrices and apply that instead (more on that later).

One thing to note that this transform likely maps from the unit object to the object in world space. Your inputs for the AABB must be representing the object in object space, and then applying this will provide the correct AABB in world space. Using the current AABB value in world space and transforming will not work unless the transformation represents the difference from the object's previous configuration to the new one. This is not the same as the transformation from object space to world space, which is likely what you are using.

If transforming from object space to world space, your AABBs center is likely (0,0,0) in object space. This means applying rotation and translation will make: center = translation.

The radius vector must be rotated, but will not be affected by translation, as vectors do not have position. Only orientation. This means you may take your object space AABB radius (likely r = (0.5,0.5,0.5)), and apply the absolute value of rotation to get the new radius of your AABB.

I agree with previous posters that you really ought to be using a math library for this. You could go as far to store the AABB center as (x,y,z,1) and the radius as (x,y,z,0). Then you can compose your translation and orientation matrices using 4x4 matrices, (rotation must come first). If this sounds strange to you, do some research on affine transformations, and the use of 4 dimensional vectors for positions/vectors when doing linear algebra in 3D. It is very common, and very useful.

##### Share on other sites

Thanks @Max Whitehead You are a savior!  Thanks @NypyrenI modified UpdateAABB and its initialization. It works perfectly.

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

(You must login to your GameDev.net account.)

• 15
• 9
• 11
• 9
• 9
• ### Forum Statistics

• Total Topics
634134
• Total Posts
3015749
×