Jump to content
• Advertisement

# Distance between 2 points in 3D

This topic is 3782 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## Recommended Posts

Hello there! From the basics, i know, that we can calculate distance between 2 points in 2D using the:
distance = sqrt( (x2 - x1)^2 + (y2 - y1)^2 )
That is simple, isn't it? The problem( for me ), is how to do it when we are using 3-dimensional coordinates? A(x1, y1, z1) and B(x2, y2, z2) ? regards

#### Share this post

##### Share on other sites
Advertisement
Add " + (z2 - z1)^2" inside the square root.

#### Share this post

##### Share on other sites
Thought about the same, but wasn't sure. Thanks.

#### Share this post

##### Share on other sites
Remember SQRT is an expensive operation.

If you are comparing distances it's quicker to compare the Squared distances.

i.e.

If SQRT((x1-x2)^2+(y1-y2)^2+(z1-z2)^2) > 10 Then
...

becomes

If (x1-x2)^2+(y1-y2)^2+(z1-z2)^2 > 100 Then

I hope this is useful!

#### Share this post

##### Share on other sites
Thanks for the tips[smile]

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
Rutin
41
2. 2
3. 3
4. 4
5. 5
• Advertisement

• 16
• 18
• 12
• 14
• 9
• ### Forum Statistics

• Total Topics
633360
• Total Posts
3011524
• ### Who's Online (See full list)

There are no registered users currently online

×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!