Skip to main content
2 of 2
Added code on what I meant with inverse and the control code.
rioki
  • 2.9k
  • 1
  • 23
  • 28

This is a classical control problem. You want to create a feedback loop that takes the divergence from optimal position and applies the appropriate torque to nudge it back into position.

btQuaternion targetOrientation  = // whatever you need
btQuaternion currentOrientation = myObject->getOrientation();

Getting the delta orientation is quite simple, you need to find the "difference" between two the quaternions target and current orientation.

btQuaternion deltaOrientation = targetOrientation *  currentOrientation.inverse();

Unfortunately this quaterion does not say much about any divergence that we can use. But if you convert this delta orientation into euler angles.

btVector3 deltaEuler = QuaternionToEulerXYZ(deltaOrientation );

You basically get the scaled inverse of the torque you want to apply. Now you "just" need to find an appropriate amount to ease it in.

btVector torqueToApply = control(-deltaEuler);

The simple solution is to multiply it with a factor. You may need to play around with the factor until you find a good value.

// simple proportional controller
btVector control(btVector in) 
{
    // play around with the factor until you find a matching one
    static Kp = 0.5;
    return in * Kp;
}

You may also want to look into a PID-Controller to get a smoother more responsive easing.

rioki
  • 2.9k
  • 1
  • 23
  • 28