Thursday, October 11, 2012

Platformer AI: Swarmmers

This past week has been very busy for me. I could get a chance to enjoy my Fall break and play some video games that are sitting on my shelf all alone, but instead I get to spend the week with Game Maker. I'm still developing a tech demo to show off to my professor that must be done by this upcoming Monday, so procrastination isn't an option. All I have up to this point is an angel that can move around and shoot fireballs (just one of many abilities) and the lowest enemy that a player can fight.

I actually did quite enjoy developing this enemy since it required me to do some actual thinking (not just looking at documentation online to make sure I did it right). This enemy is a lot more complicated than your average Goomba who walks back and forth all day. For this enemy, I had to implement basic physics and calculation to estimate trajectory and get from platform to platform. I will be using some advanced physics equations (what you would learn in an intro college physics class that required Calculus).

For clarification, this specific enemy is called an Imp.

The Imp is a not powerful and very weak. It works well in large numbers where it swarms their enemy by strength in numbers. What I needed to do was to create an enemy, that when the angel got into range it would swarm straight towards it. This is not as straight forward in a platformer with multiple platforms at different levels.

The Imps default mode is just to walk back and forth on its platform until the imp spots the angel.

When the Imp spots the angel it determine if the angel is above, below, or on the same level as the angel. The same level argument is easy enough as the Imp just charges by setting the x velocity towards the angel.

When the Imp is above the angel:


The image above shows what should happen in this situation (red denotes Imp, blue denotes Angel). The Imp needs to get from the top platform to the bottom, So either he can run off the edge or jump off. Running off might seem simpler but that does collide with the code that makes the Imp stay on the platform in his default mode. Jumping not only makes it easier for the Imp to get to the target, but it adds a more fluid and natural looks of a demon trying to get to its target in the fastest way possible. 

To find the initial velocity needed to clear the platform, it needs the distance (d) from it to the edge. From there the equation is: 


vi is the initial velocity that is needed, g is gravity and theta is 45 degrees (pi/4) because its the easiest to calculate and x velocity and y velocity are the same. Everything else besides vi is known so we just rearrange the formula to get: 


using vi, just plug in that number into x and y velocity and the imp will jump off the platform and land below where it can swarm the angel.

There are some things to be aware of and it is the negation of the values that are put into x and y. For example, vi can be a positive number but the y velocity needs to be negative because of how the Cartesian coordinate system is arranged by the language. 

When the Imp is below the angel:



This requires some more insight than the previous one. When the Imp was jumping off a platform, it could jump at 45 degree angel making the calculation easier. In this case, X and Y are independent of each each other except time (t). The vector at which the Imp jumps can range from 1 to 89 degrees. The angel at which the imp jumps is irrelevant if the x and y velocities are known. In order to figure out the velocity in which the imp must jump, we must find the minimum velocity to reach the height of the platform (h). 


 Since the y velocity when the Imp reaches its height will be zero, the equation can be rearranged to:


Knowing the initial y velocity, we can now find the initial x velocity. The only thing that x and y velocities share is that it will take the same amount of time to reach their destination. We can use that knowledge to get the x velocity so that the Imp can reach its distance (d). We can start with the most basic of physics equations, the kinematic equation to find position. Since x does not have an outside force on it, acceleration is zero and we get the equation: 


xf and xi subtracted is the difference (delta) x which in turn is distance:


Again using the kinematic equation for the y axis for velocity, we can represent the initial y velocity as:


Again knowing that final y velocity is zero, we can rearrange that equation as:


We can rearrange the x axis formula two above as:


Plugging t into the x axis formula, we get:

Using basic Algebra, we can rearrange this to be:

Now that we have an initial velocity, the Imp can now jump to the next platform. 

There are some problems with this implementation, and that is it does not take into account where the imp is and the dimensions of the platform. The imp can be below the platform and jump straight into it. The other problem is that the imp will always be one step behind. The hero can be jumping up several platforms and the imp will only know when it lands on the previous platforms. There are ways for the imp to follow the angel up the platform and anticipate where the angel will be using more advanced algorithms. For the moment, this algorithm is sufficient for a basic low level enemy and does provide some basic challenge above the walking back and forth goombas. At least the imp is trying. 

That is all while I finish to complete my tech demo and get the first level finished. Game Maker doesn't make it easy as well as I have to forget everything I learned as a CS major to get some things to work also a debugger that doesn't let you step through your code.

No comments:

Post a Comment