Book Image

iOS Game Programming Cookbook

Book Image

iOS Game Programming Cookbook

Overview of this book

Table of Contents (19 chapters)
iOS Game Programming Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Implementing wall avoidance


The AI characters will look a little odd if they collide with a wall while wandering. So, we have to make them even more intelligent so that they can seek the wall and can respond or change direction accordingly.

The wander behavior returns a force that steers the AI away from the wall to avoid collision.

Getting ready

The following approach will be used to implement the wall avoidance behavior:

  • Create feelers to sense the wall

  • We will use one feeler in front of the AI to sense the wall

  • When wall is detected, apply the force at the reflected vector

How to do it

Follow the following algorithm to achieve the wall avoidance behavior in the project:

  1. The visual explanation of the technique is shown in the following screenshot:

  2. Create feelers that will sense the wall:

    Front_Feeler = player->Get velocity; // vector
    Front_Feeler = Front_Feeler.normalize(); // vector

    This will project the feeler in front of the player.

    Front_Feeler =  Front_Feeler *  FeelerLength; // vector
    Front_Feeler...