# Vector/Quaternion help - pitch bone to direction vector

## Recommended Posts

Hello, so I'm having a problem making my character look down. I'm using the OGRE3D as my rendering engine, and the math facilities it provides.

What I need to do is basically determine the amount of pitch needed to get the bone to the same direction as my crosshair/camera direction.

// mDirection is the direction of the character
Vector3 boneDir = getBoneWorldOrientation(mEntity, mHips) * mDirection;
// This is the direction of the crosshair
Vector3 dir = mCameraTrans->_getDerivedDirection();
Quaternion rot = boneDir.getRotationTo(dir);

// The code below has been taken from the sinbad character controller and adapted to pitch, instead of yaw
Real pitchToGoal = rot.getPitch().valueDegrees();
Real pitchAtSpeed = pitchToGoal / Ogre::Math::Abs(pitchToGoal) * timeSinceLastUpdate * 100.0f;
if (pitchToGoal < 0) pitchToGoal = std::min<Real>(0, std::max<Real>(pitchToGoal, pitchAtSpeed)); //yawToGoal = Math::Clamp<Real>(yawToGoal, yawAtSpeed, 0);
else if (pitchToGoal > 0) pitchToGoal = std::max<Real>(0, std::min<Real>(pitchToGoal, pitchAtSpeed)); //yawToGoal = Math::Clamp<Real>(yawToGoal, 0, yawAtSpeed);
mHips->pitch(Degree(pitchToGoal));
The above code has been adapted from multiple sources and somewhat works, but only on one axis. I know I'm probably doing some horrible things to the math related to this, so I appreciate any tips and pointers. :)

Unfortunately I'm not that good with Vector/Quaternion math, and I get the following bug:
Here's a YouTube video showasing the problem (I didn't find a way to embed it into the post): https://www.youtube.com/watch?v=IwUpzEE3uwI
It seems to work properly on +Z, it's shaking a bit on -Z, and it just continues to rotate on +X/-X.

I also posted on the OGRE3D forums, and am mirroring the post here to increase my chance of a reply.

For any future Googlers that happen to stumble upon this post, here's the link just in case: http://ogre3d.org/forums/viewtopic.php?f=2&t=78213

Note to moderators: If you think the Math and Physics is more appropriate, please move it there.
Edited by Mind Calamity

##### Share on other sites
I've kinda solved this myself by adapting the Camera::setDirection code from OGRE to work for my need:

Now I have the following code:
Vector3 boneDir = getBoneWorldOrientation(mEntity, mHips) * Vector3::UNIT_Z; //mDirection;
Vector3 dir = mCameraTrans->_getDerivedDirection();

Quaternion targetWorldOrientation;

xVec.normalise();

yVec.normalise();

// mTransform is basically the SceneNode of the character, I'm multiplying the target orientation with it's inverse orientation to get it to parent/character-space.
mHips->_setDerivedOrientation(mTransform->_getDerivedOrientation().Inverse() * targetWorldOrientation);

The above code was taken from OgreCamera.cpp, the setDirection function, the part with the fixed yaw axis.
Now, that works... Sorta, I now get this problem at near-90 degree pitch, the orientation kind of flips to the other side, I've made another video to showcase this:

Any ideas on how to fix this ?