Skip to main content
GameDev.net gamedev.net
🔒 Locked

Math Check: Distance Between 2 Obbjects

Started by RLS0812 Jan 14, 2012 at 7:01 PM 4 replies 1.5k views
Original Post
RLS0812
RLS0812
I would like to knoow if my math is correct in calculating the distance between 2 objects on a 2D plain:
def distance(x1,x2,y1,y2):
# A squared + B squared = C squared
return ( ( (x2 - x1)**2) + ( (y2-y1)**2) )**.5
# Object 1 Location
coordinate1 = 2,8 # x,y
# Object 2 Location
coordinate2 = 5,10 # x,y
print (distance(coordinate1[0],coordinate2[0],coordinate1[1],coordinate2[1]))



Thanks.
I cannot remember the books I've read any more than the meals I have eaten; even so, they have made me. ~ Ralph Waldo Emerson
jjd
jjd

I would like to knoow if my math is correct in calculating the distance between 2 objects on a 2D plain:
def distance(x1,x2,y1,y2):
# A squared + B squared = C squared
return ( ( (x2 - x1)**2) + ( (y2-y1)**2) )**.5
# Object 1 Location
coordinate1 = 2,8 # x,y
# Object 2 Location
coordinate2 = 5,10 # x,y
print (distance(coordinate1[0],coordinate2[0],coordinate1[1],coordinate2[1]))



Thanks.


Yes, that is correct.

-Josh
HappyCoder
HappyCoder
yup. That distance equation will give you the distance between two points.
My current game project Platform RPG
RLS0812
RLS0812
Thank you.
I cannot remember the books I've read any more than the meals I have eaten; even so, they have made me. ~ Ralph Waldo Emerson
Narf the Mouse
Narf the Mouse
As a side note, it's also useful to include a "DistanceSquared" function, which omits the square root - If you're simply comparing which is farther, and don't need to know how far, omitting a costly square root can save significant time in time-sensitive applications.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.