Codementor Events

Dodge obstacles

Published Mar 24, 2019
Dodge obstacles

Hey guys!

I always ask myself how an AI do to dodge obstacles. So, i have decided to write this post describing how i will implement it. This is my first attempt, so don't be mean with me, ok.

First, we have to define our world. I am thinking to create an 2D endless runner which generate some obstacles that our character, an AI, must dodge. I have two approaches to this design: The first one, obstacles with predefined Y positions; And the second one, random Y positions with no pattern.

The first approach, the AI will have a list of the different types of obstacles the world’ll spawn. The following diagram explores this.

first approach.jpg

This solution has the advantage of less complex code, but any update to your game will make you change your AI logic. You just need to ask yourself this: “Ok. I have knowledge of obstacle A, B, C, and D. But, what will happen if a what to add one, two, or three more?”.

The process named Process input will get the data of the world through some tecnic that i haven’t defined yet, and Run process… Well. I think it’s obvious.

The second approach, however, is more complex to code, but if there is not a drastic change, you don’t need to update your AI’s logic. Let’s take a look at the diagram.

Second approach.jpg

It looks simple, right? But, the complex code is in step “Process obstacles properties” and “Dodge obstacle with best option”. What is “best”? What properties does the AI have to process? Well. It is up to you answer those questions. A well definition of your game will save you time and effort with this.

Well, i think the wise choice is the second approach. Just because the first one is not a good AI, i think.

We haven’t defined yet our world. I will use the AI behaviour i’ve chosen to make our world. IMPORTANT: This approach isn’t good. Your world must be defined first, then, your AI will come in.

Take a look at this world i’ve made.

world.png

This world is really simple. The platforms are the objects who are going to move, from right to left. This means we will decrease X coordinate for each platform we spawn. This allows us to move our player freely through the world without complex math.

I think we have everything we need, in order to create our game. In the next post, i will set the properties of each platform and obstacles the world’ll spawn, and maybe, set the code once and for all.

I hope you like it, and if you have any advice, tell me.

May the game code stay with you!

Discover and read more posts from Jordan Motta
get started
post commentsBe the first to share your opinion
Show more replies