# Howto transform and scale an AABB?!

This topic is 5124 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Heya, as the topic says, i'm trying to figure out how transform and scale an axially aligned bounding box. My currently implementaion stores a position and an extent component. So, currently i'm transform the position of the AABB by simply adding the new position to it like: AABB.vPos += vNewPos; and scale the extent like: AABB.vExtent.x *= vNewScale.x; AABB.vExtent.y *= vNewScale.y; AABB.vExtent.z *= vNewScale.z; but that gave me some wired results (i use it for AABB -> frustrum clipping). Maybe, it's because the vPos component is within world-space and the extent is within local-space? Do i have to translate the extent aswell? I guess i mixed something up here..hum please help :) Mfg Impz0r

##### Share on other sites
That looks correct from here. As long as you are not scaling the position, this should work.

I suspect your code is wrong elsewhere...

##### Share on other sites
Maybe you mean to do something like:
AABB.vPos += AABB.vExtent * 0.5fAABB.vExtent *= vScale;AABB.vPos -= AABB.vExtent * 0.5f

to keep the center of the AABB in the same place.

##### Share on other sites
First off, thanks for looking at my code ;)

Hum if the translation & scaling code is correct, it has to be a bug within the AABB -> Plane intersection code. I'm gonna post the code, maybe you guys could take a quick look at it.

The code is like:

eHalfspace cAABB::QuerySide (const cPlane &Plane) const{ const cVector3 vMin = vPos - vExtents; const cVector3 vMax = vPos + vExtents; Float32 fMin, fMax; if (Plane.vNormal.x > 0.0f)  { fMin = Plane.vNormal.x * vMin.x; fMax = Plane.vNormal.x * vMax.x; } else  { fMin = Plane.vNormal.x * vMax.x; fMax = Plane.vNormal.x * vMin.x; }  if (Plane.vNormal.y > 0.0f)  { fMin += Plane.vNormal.y * vMin.y; fMax += Plane.vNormal.y * vMax.y; } else  { fMin += Plane.vNormal.y * vMax.y; fMax += Plane.vNormal.y * vMin.y; }   if (Plane.vNormal.z > 0.0f)  { fMin += Plane.vNormal.z * vMin.z; fMax += Plane.vNormal.z * vMax.z; } else  { fMin += Plane.vNormal.z * vMax.z; fMax += Plane.vNormal.z * vMin.z; }  if (fMin <= Plane.fDist)  return (Positive); if (fMax >= Plane.fDist)  return (Negative); return (OnPlane); /* another stange thing is, if i use the code below (which is the orginal code), the aabb is always culled... if (fMin >= Plane.fDist)  return (Positive); if (fMax <= Plane.fDist)  return (Negative); return (OnPlane); */}

PS: I took most of the code out of the "3d Math Primer" book.

Mfg Impz0r

##### Share on other sites
Unless your plane distance is negated, the original code should be correct:

 if (fMin >= Plane.fDist)    return (Positive); if (fMax <= Plane.fDist)    return (Negative);}

If that is resulting in your AABBs always being culled, then maybe something is wrong elsewhere.

##### Share on other sites
Hum this is strange, i mean the culling works for sphere -> frustrum, so the frustrum planes must be correct. I noticed, if i setup an aabb within the coordinate origin the aabb -> frustrum culling works just fine, but only there. If i move the aabb then the culling gets messed up...
Notice: this is even without the correct plane intersection code, like jyk mentioned.

I guess it has something to do with the way i setup the position and the extent of the aabb.

I've got two methods therefor:

1) input: vMin, vMax
AABB.vPos = (vMax + vMin) * 0.5f;
AABB.vExtent = vMax - AABB.vPos;

2) input: vPos, vExtent (kinda obvious)
...

and furtheremore, i've got a transform method as i posted within my first post:

3) input: vNewPos, vScale
AABB.vPos += vNewPos;
AABB.vExtent.x *= vScale.x;
AABB.vExtent.y *= vScale.y;
AABB.vExtent.z *= vScale.z;

Well, i really hope you guys could help me once more :)

Mfg Impz0r

##### Share on other sites
Well guys, i've got it to work. I exchanged the aabb -> plane intersection code i posted within my second post with the following and it works. So i'm wondering whats wrong with the other one ?!

New Code:

eHalfspace cAABB::QuerySide (const cPlane &Plane) const{const cVector3 vMin = vPos - vExtents;const cVector3 vMax = vPos + vExtents; cVector3 vTmpMin, vTmpMax; // find most aligned if (Plane.vNormal.x >= 0.0f)  { vTmpMin.x = vMin.x; vTmpMax.x = vMax.x; } else  { vTmpMin.x = vMax.x; vTmpMax.x = vMin.x; } if (Plane.vNormal.y >= 0.0f)  { vTmpMin.y = vMin.y; vTmpMax.y = vMax.y; } else  { vTmpMin.y = vMax.y; vTmpMax.y = vMin.y; } if (Plane.vNormal.z >= 0.0f)  { vTmpMin.z = vMin.z; vTmpMax.z = vMax.z; } else  { vTmpMin.z = vMax.z; vTmpMax.z = vMin.z; } if ((dot (Plane.vNormal, vTmpMin) + Plane.fDist) > 0.0f)  return (Positive); if ((dot (Plane.vNormal, vTmpMax) + Plane.fDist) >= 0.0f)  return (OnPlane); return (Negative);}

So, i like to thank you guys for helping me. I really appreciated it!

Mfg Impz0r

• ### What is your GameDev Story?

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

• 10
• 11
• 13
• 9
• 11
• ### Forum Statistics

• Total Topics
634089
• Total Posts
3015460
×