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

Eye movement


Eye contact is an important factor to make characters feel alive and aware of yours and other things' presence. In this chapter, we'll make a control that will follow a spatial with its eyes, as shown in the following screenshot:

How to do it...

Eye tracking can be implemented in a single control using the following steps:

  1. We begin by creating a new class called EyeTrackingControl that extends AbstractControl.

  2. It needs two Bone fields: one called leftEye and another called rightEye. Furthermore, we should add a spatial called lookAtObject and a related Vector3f called focusPoint.

  3. In the setSpatial method, we find and store the bones for leftEye and rightEye.

  4. We also need a method to set lookAtObject.

  5. With this done, we add most of the other functionalities to the controlUpdate method. First of all, we need to take control of the bones or we won't be able to modify their rotation, as shown in the following code:

    if(enabled && lookAtObject != null){
      leftEye.setUserControl(true...