Distance from BoundingBox

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
JellyDude
Posts: 9
Joined: Tue Dec 11, 2007 6:35 am
Location: Australia

Distance from BoundingBox

Post by JellyDude »

Hi,

I'm looking to calculate the distance from the camera to a bounding box. I can calculate the distance to the center, but I really need the distance to the closest edge. The bounding box is defined as (MaxEdge, MinEdge); how does this define a box? Is it, Center+MaxEdge defines a sphere around the box? Sorry, it's late :?
JellyDude
Posts: 9
Joined: Tue Dec 11, 2007 6:35 am
Location: Australia

Post by JellyDude »

OK, ignore all that. I'll get the edges using getEdges, create 6 planes then call getDistanceTo.
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

Yup, that's how it's done in CSceneCollisionManager.

Code: Select all

         if (box.intersectsWithLine(line))
         {
            box.getEdges(edges);
            f32 distance = 0.0f;

            for (s32 e=0; e<8; ++e)
            {
               f32 t = edges[e].getDistanceFromSQ(line.start);
               if (t > distance)
                  distance = t;
            }

            if (distance < outbestdistance)
            ....
Note the use of getDistanceFromSQ() for the comparison checks.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

That code provides the distance to the nearest edge of the box. If you want the nearest point on the box [which might not be a corner], more code would be necessary.

Travis
JellyDude
Posts: 9
Joined: Tue Dec 11, 2007 6:35 am
Location: Australia

Post by JellyDude »

Thinking of creating a sphere from the boundingbox then determining the distance to it. Should work, unless someone can see a fault in my cunning plan.

Code: Select all

vector3d middle = BB.getCenter();
vector3d diag = middle - BB.maxEdge;
float radius = diag.getLength();

Distance = max_(0.0, (point-middle).getLength() - radius)
I know it doesn't strictly give me the distance to the box, but it does allow me to roughly sort objects based on distance.
Post Reply