Enemy AI: Player Detection in a 2D Game

Andrea Sorrentino
2D Game Development
5 min readJan 26, 2021

Introduction

In this short article we’ll see how to implement a simple algorithm of player detection starting from a certain range of action. To understand this topic some basics of coding, math, geometry and algebra are required.

Schematization of the problem

Simple case: Pe = (0,0)

Let’s imagine to have a game scene with a player and an enemy. We want to make sure our enemy will attack us only if we’re inside his range of action. How can we schematize our problem? We can start considering a Cartesian plane XY and representing the enemy range of action as a circle with a certain radius, where the center of the circle is the enemy position.

Now, how can we represent the circumference of this circle as a mathematical formula? Let’s consider a generic point P(x, y) on it and let’s project it onto the axes X and Y. You can notice we got a right triangle. For the Pythagorean theorem, we know that the square of the hypotenuse is equal to the sum of the squares on the chateti of the triangle.

x² + y² = R² (property of a point on a circumference of radius R)

This is the property of each point on a circumference of radius equal to R. In fact, this is the equation of a circle with the center in the origin (0,0).

Note: we’re considering the simple case where the enemy position is the origin of the Cartesian plane.

If we want to detect when our player is entering inside the range of action of the enemy, we just have to calculate in which cases his position is a point inside the circumference. But, what is the property of a point inside a circumference? Let’s consider a point P2(x2, y2) inside our circumference and let’s project it onto the axes X and Y as done before.

If we apply the Pythagorean theorem, we can notice that this time the sum of the squares on the two catheti is equal to the square of a radius R2, which is less than R (R2 < R). So, we can notice that each point inside the circumference has the property that the sum of the squares of his coordinates is less than the square of the radius of the circumference. So we can write the formula as:

x² + y² < R² (property of a point inside a circumference of radius R)

We got an inequality in two variables. In other terms, we can write the set of solutions as:

S = { (x, y) ∈ R² : x² + y² < Re² }

Which means “the player is inside the enemy range of action if his position P(x, y) is such that the sum of the squares of his coordinates x and y is less than the square of the radius of action of the enemy Re”.

General case: Pe = (Xe, Ye)

Now, let’s make the things more complex and interesting! Let’s consider the general case where the enemy position is a generic point inside our Cartesian plane. This time, if we consider a point P on the circumference and we project it onto the two axes along the circumference center, the things are a little different. We can call the X projection as dx and the Y projection as dy, that indicate respectively the distance of P to the center of the circumference along the X and Y directions.

In this case, the property of a point on the circumference is that the sum of the squares of dx and dy is equal to the square of the radius of the circumference.

dx² + dy² = R²

So, as deduced before, for the internal points the formula changes to:

dx² + dy² < R²

But our intent is to express this relation in terms of the coordinates X and Y of the point. Observing the drawing, we can write dx as the difference between the coordinate X of the point P and the coordinate X of center of the circumference.

dx = x - Cx
dy = y - Cy

Thus, our formula changes to:

(x - Cx)² + (y - Cy)² = R² (equation of a circumference of radius R and center C)

Finally, the inequality for the internal points is:

(x - Cx)² + (y - Cy)² < R² (property of a point inside a circumference of radius R and center C)

Generalizing the formula for our videogame

Before diving into the coding part, the last step is to specialize the formula we got in the case of our videogame. We have a player with a position Pp and an enemy with a position Pe and a radius of action Re. The formula changes to:

(Ppx - Pex)² + (Ppy - Pey)² < Re²

This inequality relates the position of the player with the position of the enemy and his range of action (expressed through the radius). In other terms, we can write the set of solutions as:

S = { (Ppx, Ppy) ∈ R² : (Ppx - Pex)² + (Ppy - Pey)² < Re² }

Let’s code the enemy!

In this paragraph will write a code that is separated from the framework and the technology used for the final implementation. Let’s consider a class representing an instance of our enemy inside the game scene, and create inside it a method called detect(). We want to design it as a method that takes one parameter, which is an instance of the class Player. To verify the result of the inequality, we just need an if statement. Of course, the method will return a boolean value depending on the result of the comparison. Because we just want to return the result of the expression (that will be evaluated as boolean), we just can return the expression avoiding the use of an if statement.

Note: In the code above I used the notation “this” to make the code more clear, but in some languages you can omit it (it’s used when you want to avoid a name conflict with some parameters of the method). Somewhere in the program, supposing to have an update() function which contains the game logic, we just have to insert the following statements:

That’s all! Now the enemy should move toward the player when the player is inside his range of action.

--

--

Andrea Sorrentino
2D Game Development

I’m a computer engineering student particularly interested in software engineering, programming, computer graphics, game development, databases and AI.