AI flocking using Boids

By placing many more enemies in a game we begin to require some group-based AI. A popular algorithm used in both video games and films is the Boids algorithm. It simulates flocking behavior. In this recipe, we will create a large number of enemies who flock together and chase the player.

AI flocking using Boids

Getting ready

Please refer to the project RecipeCollection03 for full working code of this recipe.

How to do it...

Execute the following code:

@interface Ch7_AIFlocking : Ch7_LineOfSight {} /* CODE OMITTED */ @end @implementation Ch7_AIFlocking -(void) step:(ccTime)delta { [super step:delta]; //Process the 'boids' flocking algorithm [self processBoids]; ...

Get Cocos2d for iPhone 1 Game Development Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.