align bounding box on object rotation/orientation, how?

Started by
6 comments, last by Programmer71 2 years, 6 months ago

I have a bounding box which was computed from a mesh/model's max and min vertices as normally would in 3D space. I want the bounding box to be aligned to the mesh/model rotation/orientation. I have the orientation of the model such as its position, right, up and look/forward vector,

How can I rotate the bounding box to orient based on model's orientation?

This might have been asked before, but i cant seem to find resources or references to do this, perhaps my keywords are not correct.

Advertisement

There are two kinds of bounding boxes:
‘Axis Aligned Bounding box (AABB)’: Made in world space, with boxes aligned to the world space xyz vectors, so you transform your vertices to world space before you do max and min.
‘Oriented Bounding Box (OBB)’: Usually made in object space, then you don't transform vertices before min max, but you have to transform the resulting local box to world space if you want to visualize it for example. You can do so by transforming the 8 corner vertices of the box the same way you do for mesh vertices.

Often we may not use the local frame of an object for OBBs, but try to find an ideal orientation to minimize the volume of the box. That's a hard problem.
A simple solution was proposed here: https://gamedev.net/forums/topic/708578-aabb-vs-obb/5433482/
An exact solution here: https://www.gamedev.net/forums/topic/668868-an-exact-algorithm-for-finding-minimum-oriented-bounding-boxes/

What are you doing with your bounding boxes? Are you optimizing for wasted volume, wasted area in a projection (in what directions?) or something else? Do you want a close approximation (e.g. as a collision proxy) or a cheap one (e.g. for broadphase collision detection)? Depending on applications, an AABB could be cheaper even it it isn't very tight, or other bounding volumes (spheres, convex hull, conservative simplifications of the convex hull…) could perform better.

Omae Wa Mou Shindeiru

Unless you need to orient a bounding box to an object direction , without knowing its transform matrix, its simply trivial to do that:

transform all 8 vertices by the transform matrix ( rotation, translation, scaling )

if you want some code, just drop me a message i am too lazy to format here

@Programmer71 Yes that is exactly what i want to do, i have a bounding box and the orientation of an object without the transform matrix,

the sitation is, image a tooth with a bracket (braces), i computed the bounding box based on the vertices of the tooth + bracket, but I want to orient the bounding box on the axis of the bracket,.

cebugdev2 said:
i have a bounding box and the orientation of an object without the transform matrix,

Sounds you are maybe unaware that you do have the transformation matrix?

cebugdev2 said:
I have the orientation of the model such as its position, right, up and look/forward vector,

That's all a transform matrix is. The matrix would then be simply:

struct Matrix4x4
{
	vec4 front;
	vec4 up;
	vec4 right;
	vec4 position;
};

Matrix4x4 mat;
mat.front = vec4(forward, 0); // assuming your forward is a vec3, we set the 4th. element to 0 for vectors
mat.up = vec4(up, 0);
mat.right = vec4(right, 0);
mat.position = vec4(position, 1); // we set the 4th. element to 1 for points 

The same works if your matrix has member data like ‘vec4 columns[4]’ or just ‘float numbers[4][4]’. Just replace names with indices in proper order.
There is however a pitfall with row major vs. column major conventions, so you eventually need to flip indices from numbers[a][b] to numbers[b][a].

I guess you knew this already, but if not it's really important to understand and work with transformation matrices.

cebugdev2 said:
I want to orient the bounding box on the axis of the bracket,.

You would then transform the tooth vertices from local space of the teeth to the local space of the bracket first. This can be done in two ways:
Transform vertices to world space, then transform them to object space of the bracket,
or make a transform matrix from one local space to the other and transform the vertices just once after that.

Still confused if that's you question, though.

Hello, I assume that you at least have bounding box vertices , so basically if you wind up your vertices clockwise you can easily determine the right, up, and forward vector simple by subtracing vertices, and normalizing , then, you do this : see , you have a 3x3 order matrix holding orientation, if you need the transaltion, upgrade your matrix to a 4x4, copy the 3x3 and in the last row ( i use opengl / vulkan ) put your translation, note that if you want to scale , you need to multiply for a further 3x3 scaling matrix

				// create rotation matrix
				
				float halfxradius = 0.5f * halfsizes.x;
				float halfyradius = 0.5f * halfsizes.y;
				float halfzradius = 0.5f * halfsizes.z;

				// scale parent matrix

				float M00 = right.x  * halfxradius;
				float M01 = right.y  * halfxradius;
				float M02 = right.z  * halfxradius;

				float M10 = up.x     * halfyradius;
				float M11 = up.y     * halfyradius;
				float M12 = up.z     * halfyradius;

				float M20 = fwd.x    * halfzradius;
				float M21 = fwd.y    * halfzradius;
				float M22 = fwd.z    * halfzradius;

This topic is closed to new replies.

Advertisement