Calculate Torque to rotate to desired angle in 1 frame..

zuur

02-05-2008 09:25:55

I'm making a forceAndTorque callback which I want to use to rotate a body by a given angle in one frame.

For example, how would I rotate a body around the local Z axis by 90 degrees in a single frame by setting (or adding to) the torque?

I think this is something like what's done by Newton to manage AngularRow constraints in custom joints - I'm just wanting to know the math behind it and haven't had much luck figuring it out from a bunch of definitions on Wikipedia..

Here's what I have at the moment:

void MyFrameListener::myTorqueCallback( OgreNewt::Body* me )
{
Real mass;
Vector3 inertia;
me->getMassMatrix(mass, inertia);
Vector3 position;
Quaternion orientation;
me->getPositionOrientation(position, orientation);

Vector3 omega = me->getOmega();
omega = orientation * omega; //Move into local space (?)
Real rotationalVelocity = omega.z;

Real desiredAngle = mCurrentRotate.valueRadians();
Real currentAngle = orientation.getRoll(true).valueRadians();

//Make the angles from -180 to 180 degrees
// (all the conversion's just to make it really obvious while working on it)
while(Radian(desiredAngle) > Degree( 180)) desiredAngle -= Degree(360).valueRadians();
while(Radian(desiredAngle) < Degree(-180)) desiredAngle += Degree(360).valueRadians();
if(desiredAngle > Degree( 90).valueRadians() && currentAngle < Degree(-90).valueRadians()) currentAngle += Degree(360).valueRadians();
if(desiredAngle < Degree(-90).valueRadians() && currentAngle > Degree( 90).valueRadians()) currentAngle -= Degree(360).valueRadians();

Real omegaNeeded = (desiredAngle-currentAngle) - (rotationalVelocity*mWorld->getTimeStep());
Real torque = omegaNeeded / mWorld->getTimeStep();

me->setTorque( Vector3(0,0, torque ) );
}


Problem is it "wobbles" a little before finally coming to rest.. I'm not sure if I should be subtracting rotationalVelocity*mWorld->getTimeStep() at the end there; it wobbles even more without it. Also it seems like I should be taking inertia into account, but changing the inertia of the body doesn't seem to affect the result so I'm not so sure...

Another puzzling thing; it doesn't seem to make a difference whether I use setTorque or addTorque..?

Thoughts?

albino

02-05-2008 18:51:45

addTorque adds more torque to body,

setTorque sets torque to body to given amount,
nomatter whats bodys current torque