Search code examples
mathrotationexponential

Rotate back to 0 degrees Logarithmically


When the user releases their finger I want my sprite to rotate back to 0 degrees. At the moment it is done like this:

if (touching == NO && rotationValue != 0)
{
    if (rotationValue < 180 && rotationValue < 0)
    {
        rotationValue += 1.5; 
    }
    else
    {
        rotationValue -= 1.5;
    }

    if (rotationValue < 1 && rotationValue > -1)
        rotationValue = 0;
}

Can anyone help me so when the sprite rotates back Logarithmically, so it starts rotating faster then slows down as it reaches 0.


Solution

  • Basically, you want to subtract a fraction of the difference left each tick. 0.125 here is an arbitrary parameter, you can change it to change the overall speed.

    if (fabs(rotationValue) < 0.00000000000000000001){
        rotationValue = 0.0;
    } else {
        rotationValue -= .125 * fabs(rotationValue);
    }