Bone LookAt SceneNode

Problems building or running the engine, queries about how to use features etc.
Post Reply
LePawel
Halfling
Posts: 53
Joined: Tue Jan 22, 2013 10:39 am
x 1

Bone LookAt SceneNode

Post by LePawel »

Hi,
I'm trying to figure out how to get a bone rotate towards a SceneNode as seen below.
Image
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);     
I have tried feeding the result to setOrientation and _setDerivedOrientation, and using both getOrientation and _getDerivedOrientation on the turretBonebut every time I get a result similar to this:
Image
Although sometimes the turret rotates smoothly (i.e the correct amount, but wrong angle) and sometimes it jumps all over the place.
LePawel
Halfling
Posts: 53
Joined: Tue Jan 22, 2013 10:39 am
x 1

Re: Bone LookAt SceneNode

Post by LePawel »

Got it:
SourceNode is the node of the object skeleton is attached to
TargetNode is the target node to look at in 2D
SourceAim is the muzzle of the tank bone
SourcePivot is the turret

Code: Select all

	Ogre::Vector3 pivotPoint, targetPos, aimPos;
	Ogre::Quaternion pivotOrientation;
	if(sourcePivot != NULL){
		pivotPoint = sourceNode->convertLocalToWorldPosition(sourcePivot->_getDerivedPosition());
		aimPos = sourceNode->convertLocalToWorldPosition(sourceAim->_getDerivedPosition());
		pivotOrientation = sourceNode->getOrientation() * sourcePivot->_getDerivedOrientation();
	}
	else{
		pivotPoint = sourceNode->getPosition();
		pivotOrientation = sourceNode->getOrientation();
	}
	targetPos = targetNode->getPosition() - pivotPoint;
	aimPos -= pivotPoint;
	aimPos.y = 0;
	targetPos.y = 0;
	Ogre::Radian angleBetween = Ogre::Math::ATan2(targetPos.z, targetPos.x) - Ogre::Math::ATan2(aimPos.z, aimPos.x);
	return angleBetween;
Post Reply