I'm trying to figure out how to get a bone rotate towards a SceneNode as seen below.
At the moment I am using the following code where SourceNode is the node on the left, turretBone is the turret bone (surprise), and targetNode is the node I want to aim at. Node that the turret is optional and otherwise I want to use the SourceNode as rotation pivot (hence the if/else)
Code: Select all
Ogre::Vector3 pivotPoint;
Ogre::Quaternion pivotOrientation;
if(turretBone != NULL){
pivotPoint = sourceNode->convertLocalToWorldPosition(turretBone->_getDerivedPosition());
pivotOrientation = turretBone->_getDerivedOrientation() * sourceNode->getOrientation();
}
else{
pivotPoint = sourceNode->getPosition();
pivotOrientation = sourceNode->getOrientation();
}
Ogre::Vector3 piston1Yaxis = pivotOrientation.yAxis();
Ogre::Vector3 Vec1to2 = targetNode->_getDerivedPosition() - pivotPoint;
return piston1Yaxis.getRotationTo(Vec1to2);
Although sometimes the turret rotates smoothly (i.e the correct amount, but wrong angle) and sometimes it jumps all over the place.