Smoothing rotation

Posted by Lewis on Game Development See other posts from Game Development or by Lewis
Published on 2012-06-23T15:23:52Z Indexed on 2012/06/23 21:25 UTC
Read the original article Hit count: 209

Filed under:
|
|
|
|

I've spent the last three days trying to work out how to rotate a sprite smoothly depending on the velocity.x value of the sprite. I'm using this:

float Proportion = 9.5;
float maxDiff = 200;
float rotation = fmaxf(fminf(playerVelocity.x * Proportion, maxDiff), -maxDiff);
player.rotation = rotation;

The behaviour is what I required but if the velocity changes rapidly then it will look like the sprite will jump to face left or jump to face right.

I'll go into the behaviour in a little more detail:

0 velocity = sprite faces forwards

negative velocity = sprite faces left depending on value.

positive velocity = sprite faces right (higher velocity the more it faces right) same as above.

I've read about using interpolation rather than an absolute angle to rotate it to but I don't know how to implement that.

I have a physics engine available.

There is one other way to get around this: to use += on the rotation angle. The thing is that I would then have to change the equation to produce positive and negative values then to make sure the sprite faces 0 once it reaches 0 velocity again. If I add that in now, it keeps the previous angle even after the velocity has dropped / is dropping.

Any ideas/code snippets would be greatly appreciated.

© Game Development or respective owner

Related posts about math

Related posts about iphone