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

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
mybiandou
Posts: 32
Joined: Thu Sep 10, 2009 6:20 am

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

Post 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
DavidJE13
Posts: 165
Joined: Tue Jan 09, 2007 7:17 pm

Post 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 )
Psan
Posts: 16
Joined: Sun Oct 04, 2009 7:07 pm

Post 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);

Bear_130278
Posts: 237
Joined: Mon Jan 16, 2006 1:18 pm
Location: Odessa,Russian Federation

Post by Bear_130278 »

Psan, he needs a nearest distance... not the distance from the pivot point.
Do you like VODKA???
Image
Image
mybiandou
Posts: 32
Joined: Thu Sep 10, 2009 6:20 am

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