Steering 3 Pursue and Evade
Steering behavior is a great way to make your game characters move in a lifelike manner. After reading a great set of tutorials by Fernando Bevilacqua here, I thought I’d implement what he did using Wade.
This post will cover his fourth article (Pursuit and Evade). I suggest you give the article a quick read because I’m just going to jump right into how to do this in Wade.
A few notes before I get started
- This demo uses Wade 3.8.1
- The ant and fish art assets are the intellectual property of Robert Brooks
- The background tile art assets are the intellectual property of kenney
Final Results preview
Click the ant button in the lower left of the demo (you may have to click twice, once to get focus and once to register the click). You should see our two usual adversaries; ant and beetle, with the ant chasing the beetle around (why can’t they just get along ?). You can use the scroll wheel to zoom in and out.
The ant’s max speed is a little lower than the beetle, so the demo keeps going. The ant’s AI is set to pursue the beetle. Pursuit in a nutshell means to account for the targets velocity to predict its’, “future position“, then move towards that position. If you’ve every tried to catch a moving object, you’re doing the exact same thing.
The beetle has a variable called “safe distance”. If the beetle is inside this distance, then its’ AI is set to evade (pursue in reverse). If the beetle is outside the distance, then its’ AI is set to wander.
Sometimes, the beetle will get pinned in a corner. This is because the method for keeping the beetle inside the game space is simply flipping its’ velocity. This is not a good way to handle this condition, because the character can ping pong between don’t go out of bounds versus evade. For a demo, it’s ok, but in a real game situation, you would probably add a separate state to handle that case.
Download and extract the zip file, load steering3.html in your browser to see it in action.
The code contains a lot of comments, so I hope it is easy to follow. Feel free to play around and use the code however you want.
cheers – Shri