# Distance between two points in 3D space without Sqrt() ?

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

## Recommended Posts

Hi, recently I made a ray-shooting method for an engine I'm building, as a learning experience. In the engine I shoot rays from pixels and determine what they collide with in a 3D grid...basically a voxel engine, but really slow. :P I'm having trouble with determining how to aquire the distance between the start point(camera), and what is actually hit. Everything up until now works fine, and it even works fine if I use the time-proven method: TotalDistance = Math.Sqrt((XDist*XDist) + (YDist*YDist) + (ZDist*ZDist)); I would like to use Sin/Cos/Tan if possible though, since I can throw them into preprocessed arrays if I convert it into C in the future. Something snags, however, and I'm not sure what, or even where the problem might be located. Here is what I have: HAngle(represents the horizontal plane - left/right/forward/backward, 0 to 360 degrees) VAngle(represents the vertical plane - up/down, even though it can go from 0 to 360 degress aswell) XPixel(Pixel being shot from) YPixel(Pixel being shot from) XAngle(Horizontal Angle to shoot the ray at) YAngle(Vertical Angle to shoot the ray at) CamPosX(Camera Position X(horizontal)) CamPosY(Camera Position Y(vertical)) CamPosZ(Camera Position Z(depth)) HitPosX(X Location of Ray upon colliding with a filled grid coordinate) HitPosY(Y Location of Ray upon colliding with a filled grid coordinate) HitPosZ(Z Location of Ray upon colliding with a filled grid coordinate) XDistance YDistance ZDistance So given all that, I SHOULD be able to figure out how to do this, but I'm totally stuck - anyone want to help? :D (And yes, I already tried searching, but couldn't come up with much) -Kramy

##### Share on other sites
Maybe this will help:
http://www.flipcode.com/articles/article_fastdistance.shtml

##### Share on other sites
Hmm....thank you. I'll consider using a fast method if the effect isn't too disasterous on the distance accuracy I'll need.

Until I port it, I'll keep trying to figure it out a 99% accurate way though. :P

##### Share on other sites
Hmm, if the original direction vector (calculated from the vertical/horizontal angles) is normalized, you could divide say hitPosX by directionX to get the length.
Think of the hit vector as the direction scaled by the distance. Then hitPos is (directionX*dist, directionY*dist, directionZ*dist), so dividing any of them by the matching direction component will get you the length.

1. 1
Rutin
31
2. 2
3. 3
4. 4
5. 5

• 13
• 53
• 11
• 10
• 14
• ### Forum Statistics

• Total Topics
632967
• Total Posts
3009553
• ### Who's Online (See full list)

There are no registered users currently online

×