Sign in to follow this  

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

This topic is 4687 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

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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites

This topic is 4687 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this