Rotate to an angle using angular velocity

Im using Box2d and CCBox2d (an obj c wrapper for box2d), but the basic box2d logic would still be able to be able to be applied to my project. Anyway im moving a sprite around and I want the sprite to face the direction in which it is heading towards. I also want the rotation of the sprite to be as smooth as possible because this is key to my gameplay.

Code: Select all

//This finds the angle to which direction the sprite is heading
rotationAngle = CC_RADIANS_TO_DEGREES(atan2(player.velocity.x, player.velocity.y));


I have tried to move to this direction by using transform but i want the movement to be smooth and i would much rather use angular velocity.

I have tried doing the following but the sprite doesn't stop once it has reached the angle.

Code: Select all

player.angularVelocity = (rotationAngle - player.rotation) * 4;



Thanks i would really appreciate any help.
I have changed the code used to work out the angle. Now it is:

Code: Select all

 float lastX = player.position.x;
    float lastY = player.position.y;
    float nextX = player.position.x + vx;
    float nextY = player.position.y + vy;
    player.velocity = ccp(vx, vy); //this is the same as linear velocity
    rotationAngle = -ccpToAngle(ccpSub(ccp(nextX, nextY), ccp(lastX, lastY)));
    player.rotation = rotationAngle; //this is the same as transform but only affects angle


There are still some problems and i need to put in a dead zone but the rotation angle is still not right and i am not sure how to make it work with angular velocity.
I think your first try was in the right direction (pun intended?) but you need to refine it a bit more, like:
- clamp the angular velocity to a maximum
- normalize the angle so the ship always rotates the shortest way
The code snippets on this page might help: http://www.iforce2d.net/b2dtut/rotate-to-angle
Thanks for the reply!

sorry but im very new to this, by clamp angular velocity to a maximum do you mean set it so that it can not go over a certain speed. Also to normalize the angle, is there a function that i can use to do this, is it from box2d, cocos2d etc.

As for the link I have checked it out but am still confused. I tried using this code:

Code: Select all

float nextAngle = bodyAngle + body->GetAngularVelocity() / 60.0;
  float totalRotation = desiredAngle - nextAngle;//use angle in next time step
  body->ApplyTorque( totalRotation < 0 ? -10 : 10 );


But instead of apply torque i used angular velocity, also the syntax in the brackets im not to familiar with.

One more thing quickly, the reason i swapped from the first code using atan2 was that the sprite wouldn't do a full swap around if i would suddenly swap directions and apply the rotation using transform.

Thanks and sorry for being so new to this, but i really appreciate any help.

Edit: ive been testing out both methods for finding a rotation angle and change the angle using only transform. The second method seems less buggy but they both sometimes just spin around at different points. When i was not using box2d and only cocos2d I would use the second method but I would just take the position and store it in a variable then move the sprite, after i would get the rotation angle from from the current moved position and the previous position and get the angle from that. It seemed to work well but i believe that velocity for box2d doesnt work in quite the same way.
I believe that I have finally got the rotation perfected, I hope it can be useful for anyone that wants to achieve something similar.

Code: Select all

    player.velocity = ccp(vx, vy);
   
    if ((fabsf(player.velocity.x) > 15) || (fabsf(player.velocity.y) > 15)) {
       
        rotationAngle = -ccpToAngle(ccpSub(ccp(player.position.x + player.velocity.x,
                                               player.position.y + player.velocity.y),
                                           ccp(player.position.x, player.position.y)));
       
        rotationAngle = CC_RADIANS_TO_DEGREES(rotationAngle)/* + 90*/;

        float nextAngle = player.rotation + player.angularVelocity;
        if (nextAngle != rotationAngle) {
                float startAngle = player.rotation;
                if (startAngle > 0) {
                    startAngle = fmodf(startAngle, 360.0f);
                } else {
                    startAngle = fmodf(startAngle, -360.0f);
                }
                rotationAngle = rotationAngle - startAngle;
                if (rotationAngle > 180) {
                    rotationAngle -= 360;
                }
                if (rotationAngle < -180) {
                    rotationAngle += 360;
                }               
                player.angularVelocity = (rotationAngle) * 4.0f;
        } else {
            player.angularVelocity = 0;
        }
    } else {
        player.angularVelocity = 0;
    }


If anyone has any improvements on the code ill be happy to hear them!

Original thread: https://box2d.org/forum/viewtopic.php?t=7306