Book Image

JMonkeyEngine 3.0 Cookbook

By : Rickard Eden
Book Image

JMonkeyEngine 3.0 Cookbook

By: Rickard Eden

Overview of this book

Table of Contents (17 chapters)
jMonkeyEngine 3.0 Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Sensing – vision


No matter how clever our AI is, it needs some senses to become aware of its surroundings. In this recipe, we'll accomplish an AI that can look in a configurable arc in front of it, as shown in the following screenshot. It will build upon the AI control from the previous recipe, but the implementation should work well for many other patterns as well. The following screenshot shows Jaime with a visible representation of his line of sight:

How to do it...

To get our AI to sense something, we need to modify the AIControl class from the previous recipe by performing the following steps:

  1. We need to define some values, a float called sightRange, for how far the AI can see, and an angle representing the field of view (to one side) in radians.

  2. With this done, we create a sense() method. Inside we define a Quaternion called aimDirection that will be the ray direction relative to the AI's viewDirection field.

  3. We convert the angle to a Quaternion and multiply it with viewDirection to get...