Results 1 to 3 of 3

Math Help - rotation based on velocity on the x axis?

  1. #1
    Newbie
    Joined
    Jun 2012
    From
    nottingham
    Posts
    2

    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.
    Follow Math Help Forum on Facebook and Google+

  2. #2
    MHF Contributor Reckoner's Avatar
    Joined
    May 2008
    From
    Baltimore, MD (USA)
    Posts
    1,024
    Thanks
    75
    Awards
    1

    Re: rotation based on velocity on the x axis?

    Quote Originally Posted by lewis23 View Post
    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 +=?
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Newbie
    Joined
    Jun 2012
    From
    nottingham
    Posts
    2

    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 =.
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Volume of rotation (y-axis)
    Posted in the Calculus Forum
    Replies: 2
    Last Post: February 1st 2010, 05:51 PM
  2. Volumes by Rotation about y-axis
    Posted in the Calculus Forum
    Replies: 2
    Last Post: March 3rd 2009, 03:02 PM
  3. volume by rotation about x axis
    Posted in the Calculus Forum
    Replies: 1
    Last Post: November 26th 2008, 05:12 AM
  4. Verification on a x-axis rotation
    Posted in the Calculus Forum
    Replies: 1
    Last Post: September 9th 2008, 07:51 PM
  5. Rotation around an axis
    Posted in the Math Topics Forum
    Replies: 1
    Last Post: October 18th 2005, 09:11 PM

Search Tags


/mathhelpforum @mathhelpforum