2D bounding box

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Dave
Posts: 25
Joined: Wed Mar 28, 2007 11:00 am

2D bounding box

Post 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.
Robert Y.
Posts: 212
Joined: Sun Jan 28, 2007 11:23 pm

Post 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.
Luben
Posts: 568
Joined: Sun Oct 09, 2005 10:12 am
Location: #irrlicht @freenode

Post 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.
If you don't have anything nice to say, don't say anything at all.
Robert Y.
Posts: 212
Joined: Sun Jan 28, 2007 11:23 pm

Post 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().
Luben
Posts: 568
Joined: Sun Oct 09, 2005 10:12 am
Location: #irrlicht @freenode

Post by Luben »

Yea, seems you'r right. Just painted it and realized my mistake. Ignore my last post.
If you don't have anything nice to say, don't say anything at all.
Dave
Posts: 25
Joined: Wed Mar 28, 2007 11:00 am

Post 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:
Dave
Posts: 25
Joined: Wed Mar 28, 2007 11:00 am

Post 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.
Robert Y.
Posts: 212
Joined: Sun Jan 28, 2007 11:23 pm

Post 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.
Dave
Posts: 25
Joined: Wed Mar 28, 2007 11:00 am

Post 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 ;)
Dave
Posts: 25
Joined: Wed Mar 28, 2007 11:00 am

Post 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.
Post Reply