-(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?




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.

  • \$\begingroup\$ Thanks, not a bad hack, any idea how to smooth the rotation out without using an action? \$\endgroup\$ – Lewis Jun 21 '12 at 16:05
  • \$\begingroup\$ 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. \$\endgroup\$ – OriginalDaemon Jun 21 '12 at 16:14
  • \$\begingroup\$ You'll have to check it but I think you can use; playerRotation += max(-x, min(x, playerVelocity * Proportion - playerRotation)); I'll edit. \$\endgroup\$ – OriginalDaemon Jun 21 '12 at 16:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.