O'Reilly logo

3D Game Development with Microsoft Silverlight 3 Beginner's Guide by Gastón C. Hillar

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Time for action—using evasion algorithms

You created a very good chase algorithm. It is time to make the other UFO evade its hunter.

Now, we are going to add more AI to the game in order to make one UFO run away from the other:

  1. Stay in the 3DInvadersSilverlight project.
  2. Open Ufo.cs.
  3. Replace the AIEvade method with the following:
    protected void AIEvade(Ufo enemy) { Vector2 distance = (Body.Position - enemy.Body.Position); if ((Math.Abs(distance.X) < (Width * 2)) || (Math.Abs(distance.Y) < (Height * 2))) { // The Ufo can wander without problems because the enemy is too far _state = UfoAIState.Wandering; AIWander(); } _state = UfoAIState.Evading; if ((Math.Abs(distance.X)) > (Math.Abs(distance.Y))) { if (distance.X > 0) Body.ApplyImpulse(new Vector2(0.2f, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required