OGRE Wiki
Support and community documentation for Ogre3D
Ogre Forums
ogre3d.org
Log in
Username:
Password:
CapsLock is on.
Remember me (for 1 year)
Log in
Home
Tutorials
Tutorials Home
Basic Tutorials
Intermediate Tutorials
Mad Marx Tutorials
In Depth Tutorials
Older Tutorials
External Tutorials
Cookbook
Cookbook Home
CodeBank
Snippets
Experiences
Ogre Articles
Libraries
Libraries Home
Alternative Languages
Assembling A Toolset
Development Tools
OGRE Libraries
List of Libraries
Tools
Tools Home
DCC Tools
DCC Tutorials
DCC Articles
DCC Resources
Assembling a production pipeline
Development
Development Home
Roadmap
Building Ogre
Installing the Ogre SDK
Setting Up An Application
Ogre Wiki Tutorial Framework
Frequently Asked Questions
Google Summer Of Code
Help Requested
Ogre Core Articles
Community
Community Home
Projects Using Ogre
Recommended Reading
Contractors
Wiki
Immediate Wiki Tasklist
Wiki Ideas
Wiki Guidelines
Article Writing Guidelines
Wiki Styles
Wiki Page Tracker
Ogre Wiki Help
Ogre Wiki Help Overview
Help - Basic Syntax
Help - Images
Help - Pages and Structures
Help - Wiki Plugins
Toolbox
Freetags
Categories
List Pages
Structures
Trackers
Statistics
Rankings
List Galleries
Ogre Lexicon
Comments
History: Make A Character Look At The Camera
View page
Source of version: 3
(current)
{maketoc} !!!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. {CODE(wrap="1", colors="c++")}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} !!!Code {CODE(wrap="1", colors="c++")}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();{CODE} 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. {CODE(wrap="1", colors="c++")} Ogre::Node * neckBone = headBone->getParent(); Ogre::Quaternion neckBoneWorldOrientation = neckBone->getWorldOrientation();{CODE} 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. {CODE(wrap="1", colors="c++")} Ogre::Vector3 headForward = unitBetween; Ogre::Vector3 neckUp = neckBoneWorldOrientation.xAxis(); Ogre::Vector3 headRight = neckUp.crossProduct(headForward); Ogre::Vector3 headUp = headForward.crossProduct(headRight);{CODE} 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. {CODE(wrap="1", colors="c++")} Ogre::Quaternion rot(headUp,headForward,headRight); rot.normalise(); //might have gotten messed up{CODE} Now put the rotation into neck space. {CODE(wrap="1", colors="c++")} rot = neckBoneWorldOrientation.Inverse()*rot;{CODE} 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. {CODE(wrap="1", colors="c++")} Ogre::Real pitchNeckSpace = rot.getRoll().valueRadians(); Ogre::Real yawNeckSpace = rot.getPitch().valueRadians(); if(abs(pitchNeckSpace) > MAX_PITCH || (PI-abs(yawNeckSpace)) > MAX_YAW){ return; }{CODE} 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. {CODE(wrap="1", colors="c++")} Ogre::Quaternion rotationBetween = rot*headBone->getOrientation().Inverse(); Ogre::Radian angle; Ogre::Vector3 axis; rotationBetween.ToAngleAxis(angle,axis);{CODE} 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 {CODE(wrap="1", colors="c++")} 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); }{CODE} Voila!
Search by Tags
Search Wiki by Freetags
Latest Changes
IDE Eclipse
FMOD SoundManager
HDRlib
Building Ogre V2 with CMake
Ogre 2.1 FAQ
Minimal Ogre Collision
Artifex Terra
OpenMB
Advanced Mogre Framework
MogreSocks
...more
Search
Find
Advanced
Search Help
Online Users
132 online users