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 flee


Flee is the opposite of the seek behavior, which steers the vehicle in the opposite direction from the target. Instead of producing the force toward target, we will push the player off to the target as the object has to flee from the target.

Getting ready

To implement the flee behavior, we need to flee to the target and follow the following algorithm:

Vector desiredVelocity = player.locationVector - targetVector;
desiredVelocity.normalize;
desiredVelocity *= player.maxSpeed;
return (desiredVelocity – agent.locationVector);

In the preceding algorithm, we are calculating the force that will be needed to flee the object off from the screen. First, we will calculate the direction vector to determine the direction opposite to the player, so that our object can flee from the target in that location. Now, in the second step, we normalize the vector and increase its magnitude to its max speed. Using this algorithm, we will implement the flee behavior for our object.

How to do it

Perform...