- Home /
Axis Aligned Bounding Boxes, Extents, and Rotation
I'm creating a set of cubes at runtime whose sizes vary with some inspector-exposed variables. I'm attempting to write code that grabs the size of a cube's face after generation, which would allow for any changes to the inspector-exposed variables.
In searching for the right methods to grab this information, I've settled on renderer.bounds, which has introduced Axis Aligned Bounding Boxes to my world. I've had success in grabbing the size of an 8x8 plane in the x and z axes like this:
Vector3 faceSize = thisFace.renderer.bounds.size;
which returns
Vector3(8.0, 0, 8.0)
as expected. However, when I grab the same renderer's bounds.max, I get:
Vector3(4.0, 4.0, 4.0)
I expected that the magnitude of the vector3 would be half that of the bounds.size, since the extents are measured from the renderer's center, per the documentation:
http://docs.unity3d.com/Documentation/ScriptReference/Bounds.html
Given that thisFace is a 2-dimensional plane, however, I did not expect the y value to contain anything other than 0.0.
I expect that this has something to do with the nature of Axis Aligned Bounding Boxes. I've had difficulty finding documentation on them. Can someone help me to understand why the extents extrude into the y-plane? Or what the Unity script reference means when it says that AABBs are "a box aligned with coordinate axes?"
EDIT: I'm imagining a box that encapsulates the rendered object at all possible rotations and, therefore, can be said to both always encapsulate an object and to ignore that object's rotations. This doesn't explain why bounds.size would return nothing in the y axis, however.
Where is the centre of your cube? If it is (0,0,0) this makes perfect sense as you have an 8x8x8 cube. Remember bounds.max is a coordinate not a size. Therefore by the values you get, I assume this is the top face you're looking at, so (4,4,4) would be the position of the max corner. Check the $$anonymous$$ value for this face, if my suspicions are correct, this should be (-4,4,-4). If your cube is not centred at (0,0,0) or you are not looking at the top face, you should pretty much scrap everything I said and pretend I didn't type this...ahem
$$anonymous$$akes sense! I was assu$$anonymous$$g that bounds.$$anonymous$$ and bounds.max returned a Vector3 that was relative to the center of the object, but if they're just global coordinates that $$anonymous$$imize and maximize a Vector3 while remaining on the rendered face of the cube, then it seems perfectly reasonable.