Implimenting BADLAND like Body Movement in Box2d

I am trying to make a body (character in my game) move and behave in the same way as the character in the BADLAND Game with regards to rotation.

The character does not have fixed rotation and behaves normally when colliding with objects but automatically fixes its rotation to face forward when there are no other world forces applied.

How do I go about this. is it a use of a revolute joint with a motor? if so what do I attach the joint to?

• You can apply a small force that rotates it in the right direction all the time. – wolfdawn Feb 7 '15 at 13:39
• yes, thanks, from reading the docs...i was looking for applyTorque() pos or neg depending on the current rotation! ill try it out later when i get home. if you post as an answer ill accept – user2145312 Feb 7 '15 at 13:55
• Sure, btw, make sure to slow it back down before it reaches the desired position. – wolfdawn Feb 7 '15 at 14:11

One way to align it back is to check it's current orientation.

float bodyAngle = body->GetAngle();


Compare it with the desired angle:

float delta = desiredAndle - bodyAngle;


Then use applyTorque with a tiny amount to slowly increase the speed in that direction If the angular speed is too high, stop increasing the speed:

if (delta > SMALL_ENOUGH)
{
float angSpeed = body->GetAngularVelocity();
if (sign(speed) != sign(delta) || Math.abs(speed) < MAX_SPEED)
body->ApplyTorque( sign(delta) * EPSILON); // EPSILON is some small value
}
else
{
body->ApplyTorque( -sign(angSpeed) * computeStoppingForce()));
}


Now you need to check how soon you will reach the desired angle and begin to slow down.

Generally, if you are moving as velocity v and need to stop smoothly after a distance of x then you are going to travel for t = 2 * (x / v) time unit, then you need to slow down at a rate of a = v / t. You can use that to callbrate your stopping motion. v is angSpeed. When the delta is small enough, you can have it beging to slow down instead of speeding up.