How to make Artificial Intelligence for a 2D Platform Game – Part 1

Update (04 – apr – 2015)

Here you can find the second post of this series. 


Artificial Intelligence in action
Artificial Intelligence in action

The last 2 weeks of my life were really intense. In the first week I saw how my city and other close to it were destroyed by the rain in the desert. In the second week I was working on the new game I made for this Game Jam. All started 5 – 6 days ago with Ignacio Varas Quinteros, we are an indie team and we participated in the second Game Jam of the community “Unity3D en Español“. Once the Game Jam started the concept was delivered: “Panties”. Our reaction was to be in shock for the next 3 hours, then we proceed to invoke the gods of the Game Jams (god of panties) and think about a simple but attractive concept: two guys fighting to win the golden panties.


Key concepts

After that brief but necessary introduction, now I’m going to talk about what concepts I think you should consider these as the basis of any attempt at using artificial intelligence, not only in video games.

  • Agent: Individual who interacts with the environment through its sensors.
  • Environment: Medium through the actor performs.
  • Sensors: These enable the agent to obtain information about the environment. As well as according to the information obtained from the environment the actor may react in one way or another.

A consideration that I think relevant is:

The purpose of game AI is not to make the enemies in the game intelligent but rather to give the illusion of intelligence


The problem

The problem that must be solved, is about allow AI to recognize different platforms and move through them to be able to attack the player. It sounds simple, but it is not at all, at least not until realizing the optimal solution. This time is not only enough having in consideration the distance between the AI and the player, we need more than that to make the artificial intelligence to move between one and other platform, or at least try.


Solution

Here are describe the key concepts but applied to the problem in particular:

  • Agent: The enemy that is capable of moving across platforms and can attack the player.
  • Environment: Static platforms arranged horizontally on a level.
  • Sensores: Ability to calculate the distance to the platforms and the player.

Here is the first key to the solution, I was thinking about how to make aware to the artificial intelligence on the platforms surrounded it and also on the platform in witch it stood, I ask myself: How can I represent a platform in the simplest way possible? And the answer was very simple: Line segment.

Definition of line segment according to Wikipedia:

Line segment : In geometry, a line segment is a part of a line that is bounded by two distinct end points, and contains every point on the line between its end points. A closed line segment includes both endpoints, while an open line segment excludes both endpoints; a half-open line segment includes exactly one of the endpoints.

Line Segment by Wikipedia

And here is the second key: point. Definition of point according to Wikipedia:

Point: In modern mathematics, a point refers usually to an element of some set called a space.

Point by Wikipedia.

Therefore for our artificial intelligence a platform is not more than a line segment with a start and end point, which we can represent very easily. A picture is worth a thousand of words.

Level prototype
Level prototype

Here’s how I check the vertices (two points) of platforms with light blue circles, in addition the purple circles are the nearest points to the AI, and the black and white circles are the vertices of the platform on which the artificial intelligence stands.


 Conclusion

So now we can conclude much about the above:

  • We can represent a platform with exactly one initial point and one final point.
  • To calculate the distance from the artificial intelligence to each platform and its vertices represented by two points is very easy.
  • When the artificial intelligence collides with a platform and this collision has a point of contact almost perpendicular to the surface then we can conclude that the AI is located on the platform and in turn we can easily recognizes their vertices, since each platform knows his start and end point.
  • We can simply meet all the vertices near to the artificial intelligence, for this the only thing that you must set is a maximum distance to or from AI.
  • Any vertex that is not on the platform where artificial intelligence is located or nearby vertices (calculated by the maximum distance) can be considered a vertex and at the same time an unreachable platform by AI.

And with this ladies and gentlemen I say goodbye, now you can run in circles or cry. I invite you to implement all these ideas (if you want to see my code let me know in the comments). I have to  admit that I learned a lot in this last Game Jam, in fact only in a moment of illumination occurred to me the solution although it sounds exaggerated, I don’t search information related to AI in 2D platforms games before started to make the game, so I don’t think that my solution is the best way to solve this problem. Also finally to 1 day of the end of the Game Jam I was aware of what was the optimal solution to solve the problem of reaching the player’s position, therefore I could not implement it, and this means that the AI has bugs, on the other hand probably I will explain the optimal solution in the last post of this series. Note that the graphics of the game was make by Ignacio Varas Quinteros and my contribution was on the programming (game). I leave you an interesting link, on a thesis of artificial intelligence related to 2D video games. The comments are open to the controversy: Would someday mankind create an artificial intelligence superior to our human race?


Update (04 – apr – 2015)

Here you can find the second post of this series. 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s