Wednesday, May 2, 2012

Gaming Your Way: So how does the NPC AI in Outpost:Swarm work ?

Now Outpost:Swarm is live I thought it may be an idea to explain how I did your in-game partners AI. If you've ever read up on Boids you'll know they have 3 simple rules, Separation Alignment Cohesion And all the examples you'll see are bird like objects flying around, maybe towards your mouse pointer, maybe avoiding obstacles. All seems simple enough. Adding them to a real game however quite a bit trickier. For separation we have to ensure the NPC is avoiding both the player and all the baddies. Firstly we find the distance to the players sprite using a simple distance=dx*dx + dy*dy; Like you would in your usual circle to circle tests. If we're too close then we need to repel the NPC from the player, via: tmpPoint1.x+=dx+dx; tmpPoint1.y+=dy+dy; Where tmpPoint1 is just a new Point(0,0); That's part 1 of the test done, the second is checking against all the baddies, and there can be a load at any one time. What I did was use a flip flop, every even frame we get a list of all the possible neighbours ( Baddies which are close enough to care about, if they're on the other side of the screen then we can skip them ), every odd frame we do exactly the same distance check as we did above. Finally we divide our Point value, tmpPoint1.x/=speedDivisor; tmpPoint1.y/=speedDivisor; ( private var speedDivisor:Number=20; ) This keeps the values within a respectable range so we don't move too fast. Read more: Gaming Your Way: So how does the NPC AI in Outpost:Swarm work ?

No comments:

Post a Comment