# Thread: rotation based on velocity on the x axis?

1. ## rotation based on velocity on the x axis?

Hi this maybe more of a programming question, but I can't write any code before I get my head around how its going to work so maybe you guys can help me out.

A sprite in my game can move along the x axis, to do this I'm giving it a velocity value. Now I need this sprite to rotate depending on what this velocity value is. Obviously 0 velocity means it faces forward, a low negative number of velocity means it is turning left, smaller the negative number, the more I want to rotate the sprite. This is the same for positive values too, any positive value means its rotating right higher the value the more it should rotate.

Is there a formula to help me work this out?

I've tried this in code so far, and it works to a certain extent, just when the velocity slows back down the sprite doesn't rotate back to the centre, it stays in the position its been rotated to. The only time it faces forward is when its smack in the centre of the screen.

Code:
float Proportion = 0.25f; // adjust value to needs
float maxDiff = 1; // adjust to limit the maximum change of the orientation.
player.rotation += fmaxf(fminf(playerVelocity.x * Proportion, maxDiff), -maxDiff);
Hope I've posted in the right forum and section, and you can help me!

Regards,

Lewis.

2. ## Re: rotation based on velocity on the x axis?

Originally Posted by lewis23
I've tried this in code so far, and it works to a certain extent, just when the velocity slows back down the sprite doesn't rotate back to the centre, it stays in the position its been rotated to. The only time it faces forward is when its smack in the centre of the screen.
Why are you adding the rotation to its previous value? That is, why don't you use a simple assignment, =, instead of +=?

3. ## Re: rotation based on velocity on the x axis?

To begin with it was there to make the rotation seem a lot less choppy as its being called with the game clock it can seem a little jumpy at times, adding it to the previous rotation allowed for smoother movement. Removing that does do the trick but I wondered if there was a much more nicer function I could use where I add it to the angle each time and still get similar results (but smoother) to when its just an =.