Game programming techniques, distance between 2 objects.

By Michael Smith

28th April 2014


 

A function to give the distance between 2 objects or between two points is one of the most basic calculations you will need in writing many games.

Find distance between 2 objects

Once upon a time, back in ancient Greece, over 500 years bc, there was a fella that went by the name of Pythagoras.   He was a wise bloke – a thinker.  One day he sat sunbathing and thinking, and what he was thinking was that some day way in the future, people will have magical devices that would enable the whole world to talk to each other, could solve problems, hold thousands upon thousands of pages of information, and – most importantly of all – let people play games where  they’d shoot zombies and stuff.

Pythagoras smiled and thought to himself, “That’ll be cool.”  He was about to drift to a new thought, but a future problem popped in his big old clever head: “A zombie would go into ‘attack mode’  when he was a certain distance from the player”.  He decided future programmers would need a way to  calculate the distance from a player to a zombie.  And the more Pythagoras thought about it, the more scenarios he envisioned where game coders might need to know the distance between two points.

So Pythagoras came up with a solution he probably wanted to call something like: The ‘Damn that zombie’s close’ Theorem, but instead historians have named it The ‘Pythagoras Theorem’

Okay, for those just interested in the ‘how?’ rather than the whole ‘how and why?, here is the formula you’ll need:

  • 2d – distance=sqrt((player.x-zombie.x)^2 + (player.y-zombie.y)^2)
  • 3d - distance=sqrt((player.x-zombie.x)^2 + (player.y-zombie.y)^2 + (player.z-zombie.z)^2)

And the ‘how and why?’:

Now Pythagoras realised that on a right angled triangle, the adjacent side squared added to the opposite side squared, is exactly the same size as the hypotenuse squared:

Distance between two objects

 

2d first. On a 2d plane the line between 2 points forms the hypotenuse of a right-angled triangle.  The difference between the zombies x position & the players x position, and the zombies y position &  the players y position, makes the opposite and the adjacent sides:

Distance between two objects

 

On the above picture, the adjacent side would be zombie y position – player y position, and the opposite side would be zombie x position – player x position.  Actually ABS(zombie.y – player.y) and ABS(zombie.x – player.x), but the ABS is not required as once we square them they will be positive anyway.  So we add together the x&y difference squares (adj & opp), we get the distance from zombie to player (hyp) squared.  So the square root of this figure gives us the length of the hypotenuse (distance from zombie to player.) Thus:

distance=sqrt((player.x-zombie.x)^2 + (player.y-zombie.y)^2)


Now 3d:

Distance between 2 objects

The hypotenuse of the red triangle is what we need to calculate.  The adjacent is player.y-zombie.y and the opposite is actually the hypotenuse of the triangle on the x/z plane – sqrt((player.x-zombie.x)^2 + (player.z – zombie.z)^2) .

So: distance=sqrt((player.y-zombie.y)^2+(sqrt((player.x-zombie.x)^2 + (player.z – zombie.z)^2)^2)

Simplified=sqrt((player.y-zombie.y)^2+(play.x-zombie.x)^2+(zombie.z-zombie.z)^2)

Thus: distance=sqrt((player.x-zombie.x)^2 + (player.y-zombie.y)^2 + (player.z-zombie.z)^2)

 

And there you go, hope you found it useful. If you’d like an e-mail whenever I post my narcissistic ramblings interesting articles, then please subscribe to my blog (top of page, right hand panel).

“To part is the lot of all mankind. The world is a scene of constant leave-taking, and the hands that grasp in cordial greeting today, are doomed ere long to unite for the the last time, when the quivering lips pronounce the word – ‘Farewell” - R.M. Ballantyne