Game Development Reference
In-Depth Information
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);
rightEye.setUserControl(true);
6. Next, we need to establish the lookAtObject position that is relative to the
eyes. We do this by converting the position to model space and storing it in fo-
cusPoint , as shown in the following code:
focusPoint.set(lookAtObject.getWorldTranslation().subtract(getSpatial().getWorldTranslation()));
7. Subtracting the eye position from Vector3f gives us the relative direction:
Vector3f eyePos = leftEye.getModelSpacePosition();
Vector3f direction =
eyePos.subtract(focusPoint).negateLocal();
8. We create a new Quaternion and have it look in the direction of the direction
vector. We can apply this on our eyes after modifying it a bit as its 0-rotation is up:
Quaternion q = new Quaternion();
q.lookAt(direction, Vector3f.UNIT_Y);
q.addLocal(offsetQuat);
q.normalizeLocal();
Search WWH ::




Custom Search