# 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;
}


## 2 Answers

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.

Check this out for more info: http://blog.demofox.org/2012/09/24/bias-and-gain-are-your-friend/

• While that seems useful, I don't see how that solves my problem as those functions require a time variable which was my original problem. – AerospaceP Jul 13 '15 at 4:17
• Oh sorry I missed that. Do you not have an end and start rotation? If not, how come? – Alan Wolfe Jul 13 '15 at 4:19
• I'm confused as to what you mean. The sprite rotates but with the code I have listed, not correctly. I have a function that determines the quickest way to rotate, and if I leave rotation speed constant, it works fine but with no "acceleration". I wanted it to speed up until it hit halfway and start to slow down after that. I made a function to combine with what you linked that returns the percentage of the rotation completed, but I still had no luck. – AerospaceP Jul 13 '15 at 4:42
• Ah. What I'm getting at is that if you store starting and ending rotation, you can calculate how long it should take total (such as say 2 seconds) and then track how much time has elapsed. Each frame you figure out what percentage of the time has elapsed and put that percentage through bias or gain to make speed non linear, then use that adjusted "percentage" to set the rotation at that percentage from the start rotation to the end rotation. Hope that makes sense. – Alan Wolfe Jul 13 '15 at 4:53
• Ah okay. When I'm setting the angle I calculate the time, and each update I use a method that I made that checks the time elapsed and the total time and returns the two divided. I tried plugging in the divided times into the gain function but the rotation speed never started to decrease after the halfway point. – AerospaceP Jul 13 '15 at 5:43