distance between two points in 3d space

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
Yokom
Posts: 30
Joined: Sat Oct 09, 2004 10:50 pm

distance between two points in 3d space

Post by Yokom »

if your first point is vector3df(rpx1,rpy1,rpz1)
and your second point is vector3df(rpx2,rpy2,rpz2)

sqrt(((rpx1-rpx2)^2)+((rpy1-rpy2)^2)+((rpz1-rpz2)^2)));

will this resolve the distance between the two points.

(rustrated edit)
this seems odd i though ^2 was to squar a number it is xor function. It took me some time to figure that out not that great in C. But here is the new c friendly math.

delta = sqrt((pow((rpx1-rpx2),2))+(pow((rpy1-rpy2),2))+(pow((rpz1-rpz2),2)));


it seems to work i plug in some obvious numbers and i get a distance you would expect.

0,0,10
0,0,1

you get a number close just a hair over 9.
Tyn
Posts: 932
Joined: Thu Nov 20, 2003 7:53 pm
Location: England
Contact:

Post by Tyn »

There is a ray that you could use here in Irrlicht, core::line3d<T>. If you enter the start and finish it will tell you the correct length, it won't even go negative if you get the values round the wrong way.
Ronin
Posts: 116
Joined: Wed Mar 03, 2004 6:23 pm
Location: Germany

Post by Ronin »

Or you can just use vector3d<T>::getDistanceFrom(othervector), it will return the exact distance as irr::f64.

But your math seems to be right to me though...
Guest

Post by Guest »

Ya im writing a tool that will not load the engine so i had to do the math.

Anywhay its the best whay to understand stuff. I usally try to go back and do the math of most stuff i can get my mind around just so i understand what went wrong when it screws up.

Thanks guys

btw is that the only way to square a number is do the pow(int,2);
calimero
Posts: 45
Joined: Sun Aug 08, 2004 4:31 pm
Location: Nice, France

Post by calimero »

if you don't like pow you can directly make x*x :D
so distance in 3d space is sqrt(x*x + y*y + z*z) :wink:
Yokom
Posts: 30
Joined: Sat Oct 09, 2004 10:50 pm

Post by Yokom »

calimero wrote:if you don't like pow you can directly make x*x :D
so distance in 3d space is sqrt(x*x + y*y + z*z) :wink:
well you would have to add the second set in there but ya.
Post Reply