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:
Eye tracking can be implemented in a single control using the following steps:
We begin by creating a new class called
EyeTrackingControl
that extendsAbstractControl
.It needs two
Bone
fields: one calledleftEye
and another calledrightEye
. Furthermore, we should add a spatial calledlookAtObject
and a relatedVector3f
calledfocusPoint
.In the
setSpatial
method, we find and store the bones forleftEye
andrightEye
.We also need a method to set
lookAtObject
.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...