Advertisement

How to center a bounding box?

Started by December 07, 2020 10:42 PM
7 comments, last by ddlox 3 years, 11 months ago

I am having trouble trying to auto-center my geometry, and rather than center each vertex, I would like to center it based on a bounding box.

So I basically have a scene node that I can call, e.g. sceneNode.setPosition(0, 0, 0), but not sure how to calculate the position to set it to so that my geometry loads in the middle of the screen.

Can anyone help?

Loop through all the vertices to find the min and max extent of the bounding box that encapsulate the geometry.
Calculate the center of that bounding box.
Invert that center point to create a translation matrix.


Advertisement

But if I already have the bounding box, why do I need to loop through all the vertices still?

I should already be able to get the center of the bounding box.

I guess the part I don't understand is the translation matrix. I'm thinking all I need to do is somethow call setPositioncorrectly.

2 questions for you first of all:

  • in which coordinate system is your bounding box? local? global? world or what?
  • are u using a camera to render objects to screen or not? if not what are you using ?

your future depends on these answers ?

Until then ?

According to my API, I have the choice of both (local & world), but I'm currently using world.

I am using a camera which I point at (0, 0, 0) to view objects which are also positioned at (0, 0, 0), but if the object starts at say (10, 5, 0), it rotates funny, not at the centroid of the object.

But if I already have the bounding box, why do I need to loop through all the vertices still?

I should already be able to get the center of the bounding box.

If there is already a bounding box there is no need to loop through the vertices to calculate one; in the OP that was not clear.
Using the bounding box, calculated a center point from it.
Then inverse that point and put it into a translation matrix. If your point is (3, 7, -2) then the inverse is (-3, -7, 2).

it rotates funny, not at the centroid of the object.

In local space: Translate your geometry using the inverse center point's translation matrix

then

In world space: Rotate your object first, and then translate by (10, 5, 0)

According to my API

API you are making or existing engine?

Advertisement

Neometron said:

API you are making or existing engine?

I am using Ogre3d v2.1.

Thanks, I'll give that a try.

rrlangly said:

According to my API, I have the choice of both (local & world), but I'm currently using world.

I am using a camera which I point at (0, 0, 0) to view objects which are also positioned at (0, 0, 0), but if the object starts at say (10, 5, 0), it rotates funny, not at the centroid of the object.

Ok have u tried:

  • convert your bounding box from world space to camera space (by the inverse of the camera's view matrix) and in there you compute the center C of this box in cam space
  • then u project C to the view vector to align it with this vector and store as C'
  • convert the C' back to world space C''
  • and call setPosition( C'' )

when your object is rendered it should be at the center of cam view;

All the best ?

This topic is closed to new replies.

Advertisement