Closest Player Detection Algorithm

basket-ballHave you ever noticed when playing a team sport video game such as a (football, basketball, rugby game, etc.) the computer often finds out who the closest player to the ball is so that you can run to the ball with the closest player.

To do so the computer uses a specific algorithm to identify who, of all the players in your team, is the closest to the ball. This algorithm can also be used when you already control a player who has the ball to find out who is your closest team player to pass the ball to.

In this blog post we will investigate how this algorithm works and will implement this algorithm using Python.

On a team sport video game, each player is a sprite that is given an x and a y coordinate to be positioned on the pitch/screen.

The ball is also positioned using (x,y) coordinates.

To find out who the closest player is to the ball, we need to calculate the distance between the ball and each player of the team. To do so we will use Pythagoras formula:

We can then use an algorithm to find the shortest distance and highlight the corresponding player.

Python Code

Did you like this challenge?

Click on a star to rate it!

Average rating 5 / 5. Vote count: 2

No votes so far! Be the first to rate this post.

As you found this challenge interesting...

Follow us on social media!

Tagged with: