Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Discussion / 3d distance formula

Author
Message
pictionaryjr
15
Years of Service
User Offline
Joined: 12th Mar 2009
Location:
Posted: 15th Mar 2011 06:48 Edited at: 15th Mar 2011 07:00
This is kind of funny because i actually made the correct function a while back. I can't understand the maths behind these and i made them.

Correct function:


Incorrect function:


Pythagorean theorem=a^2+b^2=c^2 translated to c=sqrt(a^2+b^2)

Now in my head. the second function makes more sense. what i'm doing is taking the x and z points and using the pythagorean theorem to get the hypotenuse of the space between the x and z points, (x2-x1)^2=a while (z2-z1)^2=b c=sqrt(a+b), then i'm take the hypotenuse and the space between y points and getting the hypotenuse between them, a=old c while b=(y2-y1)^2 c=sqrt(a+b). That makes sense to me in my head but its incorrect. I know i have the right formula for the first one, but i just want to understand what i did again
and using the hypotenuse as my a and

edit:
nevermind. i resolved it. i wasn't squaring the old hypotenuse when i brought it into the function again

here's the incorrect function corrected


BN2 Productions
20
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 15th Mar 2011 07:56
Further analysis:

The distance formula is a way of formatting the equation for a circle or a sphere.

In 2D:
Equation for a circle: (X-Cx)^2+(Y-Cy)^2=R^2

Where Cx and Cy are the X and Y coordinates for the center of the circle, respectively. X and Y would be any coordinate that lays on the edge of the circle.

So we see that R=Sqrt((X-Cx)^2+(Y-Cy)^2)
Look familiar?
If you draw out a circle, then draw a line from the center to any point, you will get a right triangle (where the line drawn is the hypotenuse, then you drop straight down to the X axis, and head across back to the center point). You will then see that the pythagorean theorem is merely the circle formula.

What about a sphere?

Same thing.

Equation for sphere
(X-Cx)^2+(Y-Cy)^2+(Z-Cz)^2=R^2

Once again, this will yield the distance formula where:

D=R=Sqrt((X-Cx)^2+(Y-Cy)^2+(Z-Cz)^2)

Just throwin in my two cents in case anyone wants another way to look at it.

Great Quote:
"Time...LINE??? Time isn't made out of lines...it is made out of circles. That is why clocks are round!" -Caboose
pictionaryjr
15
Years of Service
User Offline
Joined: 12th Mar 2009
Location:
Posted: 22nd Mar 2011 04:56 Edited at: 22nd Mar 2011 04:57
I have no idea what you just said, how can a right triangle be part of the circle making process?

BN2 Productions
20
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 22nd Mar 2011 07:39
The idea is that the distance formula will take 2 points and find the distance between them. The circle formula has a given distance and will find all points that distance away from a center. Its the same equation, just different unknowns. A sphere works in a similar way, it just goes around another axis as well (much harder to make a program to demonstrate).

Demo program:



Great Quote:
"Time...LINE??? Time isn't made out of lines...it is made out of circles. That is why clocks are round!" -Caboose
TheComet
16
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 23rd Mar 2011 13:20
Also, if you are using this formula just to check if an enemy is in range of the player (or similar), and you don't need the actual distance, use the quick version:



use:



TheComet

pictionaryjr
15
Years of Service
User Offline
Joined: 12th Mar 2009
Location:
Posted: 25th Mar 2011 02:02
so your doing the same thing without squaring? and how are you going to divide by 0. not possible

BN2 Productions
20
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 25th Mar 2011 02:12
Don't know why you would divide by 0 in this case, but it is possible to do (at least to run the calc without a crash).

Simply multiply by 0^-1 (technically the same as multiplying by 1/0). The result will be something like #INF but the program will still run.

Great Quote:
"Time...LINE??? Time isn't made out of lines...it is made out of circles. That is why clocks are round!" -Caboose
pictionaryjr
15
Years of Service
User Offline
Joined: 12th Mar 2009
Location:
Posted: 26th Mar 2011 18:46
wow guess you learn something new every day lol

Login to post a reply

Server time is: 2024-03-29 14:35:54
Your offset time is: 2024-03-29 14:35:54