# Rotation based on x coordinate and x velocity?

``````-(void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{

float deceleration = 0.3f, sensitivity = 8.0f, maxVelocity = 150;

// adjust velocity based on current accelerometer acceleration
playerVelocity.x = playerVelocity.x * deceleration + acceleration.x * sensitivity;

// we must limit the maximum velocity of the player sprite, in both directions (positive & negative values)
playerVelocity.x = fmaxf(fminf(playerVelocity.x, maxVelocity), -maxVelocity);

}
``````

Hi, I want to rotate my sprite based on the velocity and accelerometer input. My sprite can move along the X axis like so:

<--------- sprite ----------->

But it always faces forwards, if it is moving left I want it to point slightly to the left, the degree of how far it is pointing to be judged from the velocity. This should also work for the right.

I tried using atan but as the y velocity and position is always the same the function returns 0, which doesn't rotate it at all.

Any ideas?

Regards,

Lewis.

-

Small hack, but have you just tried making the rotation directly proportional to the object velocity.

``````float Proportion = 0.25f; // adjust value to your needs
float maxDiff = 1; // adjust to limit the maximum change of the orientation.
float playerRotation += fmaxf(fminf(playerVelocity.x * Proportion, maxDiff), -maxDiff);
``````

It won't be accurate but it should be fairly convincing.

-
Thanks, not a bad hack, any idea how to smooth the rotation out without using an action? – Lewis Jun 21 '12 at 16:05
You just need to stop the new playerRotation from being more than x degrees different from the old one. So calculate the new one. See if it's more than x degrees in difference and reduce it if it is. Again, adjust x to suit your needs. – OriginalDaemon Jun 21 '12 at 16:14
You'll have to check it but I think you can use; playerRotation += max(-x, min(x, playerVelocity * Proportion - playerRotation)); I'll edit. – OriginalDaemon Jun 21 '12 at 16:18