Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.
-(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.

share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

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.

share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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