Page 1 of 1

How can I get the distance between a box and a point?

Posted: Mon Oct 12, 2009 11:14 am
by mybiandou
I have a core::aabbox3df box and a vector3df point

How can I get the nearest distance between them?

I can get center of the box by function getCenter()



Than you for your suggestions

Posted: Mon Oct 12, 2009 12:53 pm
by DavidJE13
assuming your box is axis-aligned, this is very easy;

I'll demonstrate this in 2d for graphic simplicity, but 3d is exactly the same;

box and point:

Code: Select all

   x1     x2
y1 +------+
   |      |
   |      |   * px,py
   |      |
y2 +------+
first collapse the sides of the box; not sure how to explain this, but you should be able to see what's going on from the code.

Code: Select all

if( px < x1 ) px -= x1;
else if( px > x2 ) px -= x2;
else px = 0.0f;

if( py < y1 ) py -= y1;
else if( py > y2 ) py -= y2;
else py = 0.0f;

// In 3d, same for z
now calculate the distance from 0,0

Code: Select all

distance = sqrt( px * px + py * py )

Posted: Mon Oct 12, 2009 1:48 pm
by Psan

Code: Select all


//update all positions before this

core::vector3df x = node1->getAbsolutePosition();
core::vector3df y = node2->getAbsolutePosition();
f32 d = x.getDistanceFrom(y);


Posted: Mon Oct 12, 2009 2:26 pm
by Bear_130278
Psan, he needs a nearest distance... not the distance from the pivot point.

Posted: Tue Oct 13, 2009 3:39 am
by mybiandou
As my box is aligned,
I will try Mr. DavidJE13 's method

It is simple than I imagine before

Thank you all very much