Jump to content
• ### What is your GameDev Story?

• Advertisement

# sine animation with varying speed

This topic is 4372 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## Recommended Posts

hello, i am animating something based on a sine curve: animatedvalue = sin(passedTime() * speed); now i want to smoothly change the animation speed. if i just modify the speed variable, the animation directly jumps to a new position since, depending on the time, the sine could be somewhere completely else. how can i change the speed of a sine animation seamlessly?

#### Share this post

##### Share on other sites
Advertisement
I do not know if this is applicable but I would have tried to do it like this.

long animPos = 0;atUpdate(long timeSinceLastUpdate){  animPos += timeSinceLastUpdate * speed;  animatedvalue = sin(animPos);}

#### Share this post

##### Share on other sites
Well, if you want to instantly change speed, but keep everything "in phase", then you'll need an additional variable in your sin() function.
animatedvalue = sin(passedTime() * speed + offset);
When you first initialize your values, offset can be 0. However, if you ever need to change speed, you can recalculate offset so that everything matches and you don't get the ugly discontinuity. (Note that I'm assuming C++, and assuming that your datatypes are all double. Change as necessary.)
const double PI_TIMES_TWO = 6.283185307179586476925286766559;double speed = 1.0;double offset = 0.0;double CalculateAnimatedValue(){  return sin(passedTime() * speed + offset);}void ChangeSpeed(double newSpeed){  double time = passedTime();  double currentTime = time * speed + offset;  double unadjustedNewTime = time * newSpeed;  double currentOffset = fmod(currentTime, PI_TIMES_TWO);  double unadjustedNewOffset = fmod(unadjustedNewTime, PI_TIMES_TWO);    speed = newSpeed;  offset = currentOffset - unadjustedNewOffset;}
There may be a better way to do this (and this may actually be incorrect code, I didn't test it), but I think it should work. Basically, I calculate the current time of the animation, and the new time of the animation if I used the new speed and no offset value at all. Then I use fmod to calculate where in a single cycle of a sine wave the animation is currently in, and where it would be if I switched to the new speed. I then subtract the latter from the former to get the new offset, which should (hopefully) be exactly what is needed to match up the old sine wave with the new sine wave.

thanks a lot!

that works!

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement
• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

(You must login to your GameDev.net account.)

• ### Popular Now

• 15
• 14
• 46
• 22
• 27
• Advertisement
• ### Forum Statistics

• Total Topics
634054
• Total Posts
3015266
×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!