Game Development Community

Dynamic Animation Speed

by Chris Labombard · in Torque Game Builder · 01/28/2006 (12:52 pm) · 10 replies

Is there a way to change the speed of an animation in the middle of the animation ?

I would like the ability to change the speed based on a value between 0 and 1 and can't figure out how to do it. I need to change it part way through the anim.

I tried changing the animationTime in the datablock but it only takes effect after the animation has completed and been restarted again.

If this isn't in the engine, it would be a very nice feature to have. :)

About the author

I have been a professional game programmer for over 5 years now. I've worked on virtually every platform, dozens of games and released a few of my own games, including 2 iPhone titles and a title waiting release on Big Fish Games.


#1
01/31/2006 (11:44 am)
Would it be possible to have a state machine? Check out the Platformer Animation section of the platformer tutorial on TDN.

Otherwise...

If you can't change the framerate, you can always change the amount of frames. If you want to slow down time you'd just need to create another animation strip with more cells and jump between that strip and your "normal" strip with a flag setting.

Sound possible?
#2
01/31/2006 (11:53 am)
Well, since there doesnt seem to be a solution I think what I'm going to do is manually animate the image by forcing the frame change. Im pretty sure there is a setAnimationFrame function.

What I'll do is schedule a setNextFrame function (that I will create) and then if my animation speed needs to change(I have a mouse controlled valve) I'll just call a changeSpeed function (which I'll create) to change when the next frame should be set.

My functions will look something like this: (pseudo code)

function setNextFrame(%obj)
{
%obj.animFrame++;
%0bj.setAnimationFrame(%obj.animFrame);
if (%obj.animFrame != %obj.numFrames)
schedule(%obj.animSpeed, %obj, setNextFrame, %obj);
}

and

function changeSpeed(%obj, %valveValue)
{
%obj.animSpeed = %obj.animTime / %obj.numFrames * %valveValue; // valveValue 0 to 1
}
#3
01/31/2006 (5:22 pm)
To perform what you want chris, could you just :

use animatedsprite #1 starting at frame X, playing at speed 1.0
delete/hide sprite #1, instantly create sprite #2, starting at frame Y, playing at speed 2.0

etc. just create a new sprite starting at the frame you want, at the new speed.

would that work?
#4
02/01/2006 (3:34 am)
Jason that could work but I am going to have about 50 different images I need to animate at variable speed, and my speed is a float between 0 and 1.
#5
02/01/2006 (8:22 am)
Hmm, i dont see why you still couldnt use my method programatically, but I obviously dont know the details of the problem at hand :)
#6
02/01/2006 (9:05 am)
Jason, that would work if I knew how to start the animation part way through.
#7
02/01/2006 (9:20 am)
If you are ok with modifying C++ code, I think, it would be about 10 - 20 lines of code to add this feature.

In t2dAnimatedSprite.cc - t2dAnimatedSprite::integrateObject() there is the line:
const bool frameChanged = mAnimationController.updateAnimation( elapsedTime );

If you would change it to:
const bool frameChanged = mAnimationController.updateAnimation( elapsedTime * mAnimationSpeed );

and make mAnimationSpeed a F32 member variable of t2dAnimatedSprite, that is somehow modifyable through script, then you could very easily change animation speed any time you want. However, I have not tried this out myself.
#8
02/01/2006 (1:54 pm)
Michael. yes that would do it. I looked for a place to put that but I must have been really tired cause somehoe I missed it.

Changing it via script is very simple. Just add a ConsoleMethod.

Thanks man. I owe you a beer :)

As a note, this should be added to T2D permanently IMO.
#9
02/01/2006 (1:58 pm)
Yes, that does seem like a good and simple feature improvement. props to michael W.
#10
02/02/2006 (1:00 am)
@Chris:
Quote:Thanks man. I owe you a beer :)

If I ever get to Canada I'll knock at your door ;)