If you just want your character never to rotate at all, you can just set fixedRotation to be true in the body def:
Code:
b2BodyDef bodyDef;
bodyDef.fixedRotation = true;
If you want to allow your character a certain amount of rotation so that he is not totally rigid, you could apply a torque to correct the rotation as necessary. This is a little more involved. I am using this code:
Code:
float desiredAngle = 0;
float angleNow = body->GetAngle();
float changeExpected = body->GetAngularVelocity() * timeStepLengthInSeconds; //expected angle change in next timestep
float angleNextStep = angleNow + changeExpected;
float changeRequiredInNextStep = desiredAngle - angleNextStep;
float rotationalAcceleration = timeStepsPerSecond * changeRequiredInNextStep;
float torque = rotationalAcceleration * torqueAdjustment;
if ( torque > maximumPossibleTorque )
torque = maximumPossibleTorque;
body->ApplyTorque(torque);
It looks quite long-winded but I left it like that so I could figure out what I was doing.
torqueAdjustment is a value which you will need to play with, it alters how quickly the rotation will be corrected. I've found that a value of 3 for this makes my 75kg character look rigid almost all the time, except when he slams into something while running fast. Don't set this value too high because the rotation in the next time step might end up way too far in the opposite direction, causing the next correction to be even larger, etc.
Although this works fine I've always wondered if there was another way.... ?