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
How can I get the distance between a box and a point?
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:
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.
now calculate the distance from 0,0
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 +------+
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
Code: Select all
distance = sqrt( px * px + py * py )
Code: Select all
//update all positions before this
core::vector3df x = node1->getAbsolutePosition();
core::vector3df y = node2->getAbsolutePosition();
f32 d = x.getDistanceFrom(y);
-
- Posts: 237
- Joined: Mon Jan 16, 2006 1:18 pm
- Location: Odessa,Russian Federation