Hi everyone, I've been having the exact same issues as the OP so I was hoping I might be able to join the discussion here (if anyone is against this I'll happily make my own thread)? I've been attempting to essentially "rebuild" the 8 corner points of the AABB using the current min/max, then I transform them and lastly I get a new min/max by comparing these 8 transformed corner points with the current min/max. However, when I rotate the object the AABB only ever grows in size and scaling simply doesn't seem to work at all (translation works fine).
// Bounds indexing: 0 = min, 1 = max
void Transform( const glm::mat4x4 &transform ) {
// Translation
glm::vec3 translation( transform[3][0], transform[3][1], transform[3][2] );
bounds[0] += translation;
bounds[1] += translation;
// Rotation/scale
// Recreate the original 8 corner points from the current min/max
glm::vec4 cornerPoints[8] = {
// starting from the top left going clockwise; back-face
glm::vec4( bounds[0].x, bounds[1].y, bounds[0].z, 0.f ), glm::vec4( bounds[1].x, bounds[1].y, bounds[0].z, 0.f ),
glm::vec4( bounds[1].x, bounds[0].y, bounds[0].z, 0.f ), glm::vec4( bounds[0].x, bounds[0].y, bounds[0].z, 0.f ),
// front-face
glm::vec4( bounds[0].x, bounds[1].y, bounds[1].z, 0.f ), glm::vec4( bounds[1].x, bounds[1].y, bounds[1].z, 0.f ),
glm::vec4( bounds[1].x, bounds[0].y, bounds[1].z, 0.f ), glm::vec4( bounds[0].x, bounds[0].y, bounds[1].z, 0.f ),
};
// Create "new" AABB
for( U32 i=0; i<8; ++i ) {
cornerPoints[i] = cornerPoints[i] * transform;
// Try and find new min/max
if( cornerPoints[i].x < bounds[0].x ) bounds[0].x = cornerPoints[i].x;
if( cornerPoints[i].y < bounds[0].y ) bounds[0].y = cornerPoints[i].y;
if( cornerPoints[i].z < bounds[0].z ) bounds[0].z = cornerPoints[i].z;
if( cornerPoints[i].x > bounds[1].x ) bounds[1].x = cornerPoints[i].x;
if( cornerPoints[i].y > bounds[1].y ) bounds[1].y = cornerPoints[i].y;
if( cornerPoints[i].z > bounds[1].z ) bounds[1].z = cornerPoints[i].z;
}
// Lastly, set center and size
center = ( bounds[0] + bounds[1] ) / 2.f;
size = ( bounds[1] - bounds[0] ) / 2.f;
}