🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Finding length of 3D line when given end points.

Started by
2 comments, last by GameDev.net 24 years, 10 months ago
here's C++:

float x = x1 - x2;
float y = y1 - y2;
float z = z1 - z2;

distance = sqrt(x*x + y*y + z*z)


Advertisement
Thanks for your quick response Mutex.

Anyone know how to calculate the length of a line in 3D when you know the endpoints.

Like (x1, y1, z1) and (x2, y2, z2).

Thanks!

Remember, however, that for many applications of the distance function (checking for collisions against bounding spheres, etc). You can usually get away with using the "distance squared", and avoid doing the (SLOW!) square-root operation.

i.e.: if you have two billiard balls which are 2 units in diameter, to see if they collide, you can check to see if their centers are within 4 units of each other, avoiding a square root operation.

-- Pryankster

-- Pryankster(Check out my game, a work in progress: DigiBot)

This topic is closed to new replies.

Advertisement