# Acceleration and Deceleration During Rotation

I'm attempting to have a sprite rotate in a way where its rotation speed increases until it has reached the halfway point in its rotation, to which it starts to slow down. I'm currently calculating the halfway point by calculating the amount of time it should take to rotate the sprite, and then checking if a current time variable is larger.

The code works rather well except for the fact that I can't figure out how current time should increment each update. Currently I'm using delta time, but that doesn't work. I'm thinking it needs to be some combination of the calculated time to rotate and delta, but I haven't gotten it yet (clearly).

EDIT: Specifically, currentTime shouldn't be incremented by delta solely, but I have no idea what it should be in place of delta.

        if (currentTime <= timeToRotate)
{
rotationSpeed += 5f * delta;
currentTime += delta;
}

else
{
if (rotationSpeed > 5f)
rotationSpeed -= 5f * delta;

else
rotationSpeed = 5f;

currentTime += delta;
}


Correct me if I am wrong, but from what I understand, you want to achieve the following:

If so, this can be achieved using trigonometry:

float easing = 0.075f;

float direction = Math.atan2(target.y - sprite.y, target.x - sprite.x) / Math.PI * 180;

if (direction < sprite.rotation - 180) {
direction += 360;
}

if (direction > sprite.rotation + 180) {
direction -= 360;
}

sprite.rotation += (direction - sprite.rotation) * easing;

• That actually works once I got rid of my code that was checking if the angle should be incremented in a positive or negative direction. My only question is how could I incorporate a delta time so that the rotation would take the same amount of time on two computers? – AerospaceP Jul 13 '15 at 16:59
• Just multiply sprite.rotation by deltaTime after all previous calculations. – driima Jul 13 '15 at 17:07
• Ah okay, that didn't exactly work as that just caused the rotation to be minuscule. So I multiplied easing by delta instead and increased it, and it would seem to work but I'll have to test it on a different computer. Thank you. – AerospaceP Jul 13 '15 at 17:16
• No problem - it's strange that spriterotation *= deltaTime would yield a slower result; have you tried logging deltaTime? It should be around 1, give or take a few decimals. – driima Jul 13 '15 at 17:22
• It isn't. It is around 0.05 +- 0.02. I'm using LibGDX if that tells you anything. – AerospaceP Jul 13 '15 at 17:39

This seems like a real nice fit for using something like bias and gain.

Using those functions, you do a simple linear interpolation, but before using your "percent" value in the lerp, you pass it through a function to make the percent value non linear.

This makes it so it still takes the same amount of time to do, but you can make it faster in the beginning and slower in the end or make it slow near the ends and fast in the middle - or other behaviors as well.