Friday, November 8, 2013

Agent Movement: Seeking

You have returned! Unless this is your first week, in which case... hi.

Last week, I introduced you to a set of tutorials about basic AI movement. Hopefully you took the time to look at them yourself so you can see how awesome they are. If not... well, it's too late for you. You are doomed.

Now we will examine the first tutorial in the series. You can find it here.

Agent Movement/Steering: Seeking Behaviors

Seeking is the most basic fluid movement that this series looks at. By seeking instead of simply moving in a direction, you can make agents move more realistically. From the top-down gaming perspective of this tutorial series, fluid movements in any direction are pretty important.

The article first highlights the importance of designing a steering-based movement system by showing you what it looks like when you don't use seeking movement. It causes the agent to move in discrete chunks rather than smooth curves. Take a look at the cute little Flash demo they've got there to see what I'm talking about.

As far as how to code in this seeking behavior, it's pretty simple. Simply code up a basic "Agent" class with a few attributes:
  • Position: Coordinates on screen.
  • Velocity: The change in position for a unit time.
  • Mass: Used for regulating the effect of steering on the agent.
This is just the base attributes for an agent class. By setting the velocity to be non-zero, you can create the boring, simple movement described before by adding the velocity to the position every update call. To achieve the fluid seek movement that we want, we need to calculate a steering vector. Within an update call, calculate the following:

  • Desired Velocity: The direction you want the agent to move. In the tutorial, they move towards the mouse cursor.
  • Steering Vector: The vector to be added to the current velocity to move it towards the desired velocity. In a seeking movement, this is equal to the desired velocity minus the velocity.
It's really that easy. The tutorial itself has all the code you need to get this to work aside from the underlying rendering and agent management code.

I have re-created the seek movement using good 'ol trustworthy XNA. It's what we used to make Dot Wars, so, I mean... it technically works. If you'd like to take a look at the code, just visit my brand spanking new Github for it.

I was going to take screenshots, but then you can't see what's going on too well... And my screen recorder was wack. So just run the code yourself.

I must confess that it was hard for me to get this method at first. Explicitly changing the velocity felt vulnerable to me. When we coded the movement for Dot Wars, all changes to the velocity were done by adding accelerations to the dots instead. This allowed us to move the dots around as well as apply outside forces onto them. It was kind of nice and Newtonian.

So I first tried to translate this system into an entirely acceleration-based movement system. It didn't work. These steering techniques manipulate the velocity directly in a lot less code than the acceleration technique. In order to get the same effect as truncating the velocity using accelerations, I'd have to use dragging and boosting forces that cancel each other out at a certain max speed, which isn't really what I wanted to do. We did it for Dot Wars, but even then it was not the best way to handle the movements.

I eventually realized that this method of moving the agent worked better, looked better, and could still have outside forces acting upon it so long as the accelerations were applied after the movement velocity was calculated. I think I'd like to look at that eventually. Maybe after I finish this series.

It turns out that seeking is a pretty easy behavior to implement. On top of that, it looks good. It helps the player believe that the agents are actually steering around the world, which, like in the case of Dot Wars, is really good at making the player feel like they are battling worthy AI opponents.

Granted, this is only a small part of the final agent behavior, but when designing AI's and, really, anything in games, it's important to keep track of details like movement because only by doing so can you design the best game possible.

No comments:

Post a Comment