Page 1 of 1

2D bounding box

Posted: Fri Apr 13, 2007 9:31 am
by Dave
Hello everyone,

I've searched the forum for any topic with information about bounding boxes, but didn't find what I was looking for.

My question: is it possible to get a bounding box of a 3d object within a 2d plane? Perhaps this is a bit unclear, so I made the following picture:
Image

As you can see, the 3d objects are within 2d bounding boxes. I have no idea how to calculate the dimensions of such a box. Could someone point me in the right direction?

Thanks in advance.
- Dave.

Posted: Fri Apr 13, 2007 10:38 am
by Robert Y.
Hi Dave,

there is a simple way: get the vertices of your model and call get2DCoordinates() on their absolute position. Simply keep score of xmin,xmax,ymin and ymax and you'll have your box. It's computational costly however, if you have high poly models.

Posted: Fri Apr 13, 2007 12:26 pm
by Luben
Is it not enough to get the ordinary 3d bounding box and call get2DCoordinate with the points in the box? Not just min and max, but all positions, sortof.

pseudocode:

OrigBox=Node->getBoundingBox()
VectorList L
OrigBox.getEdges(L)
aabbox NewBox(vector3df(0,0,0))
for each L
Position2d Pos
NewBox.addInternalPoint(Pos.X, Pos.Y, 0)
end for each

end code

With that i think the new bbox should hold the box coords in a 2d plane, since z is ignored. I think this might be ok.

Posted: Fri Apr 13, 2007 12:47 pm
by Robert Y.
I thought of that too, but when viewing from other angles, the bounding box will often be much larger than the model itself, especially if it's small compared to its bounding box (for instance a long thin diagonal object). If you want to have it snug fit around the model (as shown in the image), you'll need get2DCoordinates().

Posted: Fri Apr 13, 2007 1:12 pm
by Luben
Yea, seems you'r right. Just painted it and realized my mistake. Ignore my last post.

Posted: Sun Apr 15, 2007 9:46 am
by Dave
Thank you both for the fast responds.
I'll try what you said and it sounds logical, why didn't I think of that.. :wink:

Posted: Tue Apr 24, 2007 8:55 am
by Dave
Hello again.
I thought it would be nice to post the solution here, so other people can use it too. Note that it doesn't yet work up to 100%, but the results are quite okay ;)

I used the bounding box for my calculations, since this isn't as CPU-intensive as using all the vertices of a model.

1) Calculate the 3D coordinates of all 8 points in the bounding box.

2) Convert those points to 2D screen coordinates (getScreenCoordinatesFrom3DPosition).

3) Calculate the length of the 4 diagonals in the bounding box using the 2D screen coordinates:

Code: Select all

  /4--------/0
 /  |      / |
/   |     /  |
6---------2  |
|   5- - -| -1
|  /      |  /
|/        | /
7---------3/ 

Diagonal 1: from 0 to 7
Diagonal 2: from 1 to 6
Diagonal 3: from 2 to 5
Diagonal 4: from 3 to 4
4) Get the X- and Y-coordinate from the largest diagonal and save them as the size of the box.

5) Get the smallest X- and Y-coordinate from the diagonals and save it as the starting point.

6) Draw a cube with the saved starting point and size.

Posted: Tue Apr 24, 2007 11:33 am
by Robert Y.
Hi Dave,

I don't know exactly why you use the bounding box, but if it is to indicate that the user selected this model, you could also consider changing the models colour or brightness instead. That would take less calculations.

Posted: Tue Apr 24, 2007 11:46 am
by Dave
He Robert, you are absolutely right. For the selected model I already did something like that by showing the wireframe.
I need the 2D bounding boxes for something else. Thanks for mentioning it though ;)

Posted: Fri Jul 06, 2007 7:53 am
by Dave
Sorry for the bump, but it seems that my code didn't work when rotating objects. Vitek just posted a very nice solution right over here: http://irrlicht.sourceforge.net/phpBB2/ ... 361#123361.