Make A Character Look At The Camera         Using quaternions and SLERP to make a character look at a camera (or any other object for that matter) naturally, with constraints on head movement

Introduction

Suppose you have a character with a skeleton that has a neck and a head bone (head attached to neck) and you want the head (orientation defined by you in your modelling/animation tool) to rotate and face the camera. You don't want the character to turn it's head ala the Exorcist or anything, and you want the head to rotate incrementally and naturally. This may not be so straightforward unless you're pretty good with 3D transformations. This how-to will teach you how to do this in a fairly generic way that you could apply to many object-facing problems.

Getting Control of the Head

Your existing animations have control of the bones of the skeleton when they are running. Each bone has an animation track for each animation which must be destroyed. You must also set the bone to manually controlled in order to modify it.

Ogre::Bone * headBone;
Ogre::Skeleton* skel = head->getSkeleton();
headBone = skel->getBone("Head");
headBone->setManuallyControlled(true);
int numAnimations = skel->getNumAnimations();
for(int i=0;i<numAnimations;i++){
    Ogre::Animation * anim = skel->getAnimation(i);
    anim->destroyNodeTrack(headBone->getHandle());
}

Code

void HeadManager::turnHeadToLook(double deltaT, Ogre::Camera * object){
    //we want the head to be facing the camera, along the vector between the camera and the head 
        //this is done in world space for simplicity
    Ogre::Vector3 headBonePosition = headBone->getWorldPosition();
    Ogre::Vector3 objectPosition = object->getPosition();
    Ogre::Vector3 between = objectPosition-headBonePosition;
    //what is the unit vector
    Ogre::Vector3 unitBetween = between.normalisedCopy();



Now let's get the neckBone because that is the space we want to orient the head in. Again, we get the world orientation because this is a convenient space to work in.

Ogre::Node * neckBone = headBone->getParent();
    Ogre::Quaternion neckBoneWorldOrientation = neckBone->getWorldOrientation();



Now we need to build a coordinate system for head relative to the neck. An orthogonal coordinate system IS A ROTATION! The head is defined to have it's x-axis aligned with the neck, y axis pointing straight out the front of th head between the eyes and z axis pointing towards the left ear. We do not want to induce any roll(rotation about y in my case) in the head relative to the neck. Note: Cameras use the UP (0,1,0) vector for this, but that will only work for a character if the character is standing straight up! What if the character was lying down...doesn't work then...


Basically it's a bunch of cross products. look x up = right, look x right = new up. The cross product of two vectors is a vector orthogonal to the original two...pretty convenient for building orthoganol coordinate systems, huh? The reason we use the neck up is that this enforces that there will be no roll about the neck axis because the "right" vector will always be orthogonal to it. Remember, we're working completely in world space right now. This is probably the hardest part for people to understand, so think about it clearly.

Ogre::Vector3 headForward = unitBetween;
    Ogre::Vector3 neckUp = neckBoneWorldOrientation.xAxis();
    Ogre::Vector3 headRight = neckUp.crossProduct(headForward);
    Ogre::Vector3 headUp = headForward.crossProduct(headRight);

Put them together. Remember, up is x, forward is y, right is z. We also normalize the rotation because of possible numerical errors in computing the cross product.

Ogre::Quaternion rot(headUp,headForward,headRight);
    rot.normalise(); //might have gotten messed up

Now put the rotation into neck space.

rot = neckBoneWorldOrientation.Inverse()*rot;

So now we have a rotation, but a head can't go out of a certain range...I don't know what this is, IANAD...figure it out yourself, or use something sensible, like PI/2 for both. Because of the way I set up my skeleton, and how ogre defines yaw, pitch and roll (rotation about y, x, z respectively), the code looks strange, but I assure you it works. The head is rotated 180 about the neck x-axis, that's why we have the PI-abs(yawNeckSpace) in there.

Ogre::Real pitchNeckSpace = rot.getRoll().valueRadians();
    Ogre::Real yawNeckSpace = rot.getPitch().valueRadians();
    if(abs(pitchNeckSpace) > MAX_PITCH || (PI-abs(yawNeckSpace)) > MAX_YAW){
        return;
    }

Note:If you don't care about smoothly moving the head, skip to the end.


We need to figure out how much of an angular change the head movement is (we're only going to consider one angle...if you want separate speeds for yaw, pitch, and roll, figure it out yourself and add to this how-to). The way to do this is basically if B = A + ? then ? = B-A. We have B (the desired rotation) and A (the current rotation), so it's simple quaternion algebra.

Ogre::Quaternion rotationBetween = rot*headBone->getOrientation().Inverse();
    Ogre::Radian angle;
    Ogre::Vector3 axis;
    rotationBetween.ToAngleAxis(angle,axis);

Ok, we're in the home stretch. We want to create a smooth rotation. We use Slerp for this. We determine how far along the animation we are by the ratio of the farthest we can rotate in this frame and the total rotation. Make sure you use the shortestPath=true option in slerp or you could get exorcist-like results. "lookSpeed" is in radians per second, deltaT is in seconds

Ogre::Real maxAngleThisFrame = lookSpeed*deltaT;
    Ogre::Real ratio = 1;
    if(angle.valueRadians() > maxAngleThisFrame){
        ratio = maxAngleThisFrame/angle.valueRadians();
    }
    rot = Ogre::Quaternion::Slerp(ratio,headBone->getOrientation(),rot,true);
    headBone->setOrientation(rot);
}

Voila!