Tweening

Monkey Forums/Monkey Programming/Tweening

zoqfotpik(Posted 2012) [#1]
Any thoughts on generalized tweening functions?

I would like to establish a family of reusable functions suitable for certain generalized tasks. I would like to apply the same functions to RGB color interpolations, alpha, scaling in two directions, motion, etc.

I'm pretty sure I can pull this off, either with wrappers or from within my code generator, my application is fairly simple.

But does anyone have ideas on the subject? Does Diddy support this or is the tweening there only for animations?

I want this for generalized polish and some animation pizzazz and I would like to save effort because similar tween functions should be applicable to different things, eg scaling, movement etc.

Should I implement the tweening functions as interfaces?


Nobuyuki(Posted 2012) [#2]
It really depends on the type of interpolation and system you're looking for. If you want a "set it and forget it" tweening system, it might make a difference on whether your game uses delta-timing or frame timing. In the end, the system is just a method of interpolating from two states, and a generalized class would probably just constitute a method for producing an output value (or a set of them) from a specified input value. Multidimensional tweens would require their own objects to handle i/o, for example.

My game uses a frame-based timing, and the only thing I've implemented in it thus far that offers a kind of tweening is a transition interface for switching screens. It accepts a value between -100 and 100 (you should generalize this in your own class to something like 0-1 and use floats), and anything that implements Transition needs to take a value and produce an output based on it. It's automated outside of the main classes using my in-game timing. Automatic tweens would have some way to take a time/frames argument, and call the specified interpolation over that number.

Things get more complicated when you want to interrupt the iterating mechanism or make it robust to those changes. Things get more complicated if you want your in-game objects to be able to "hook" itself to an interpolation in an OOP way. Etc.

I think the best way to go about it might be to have a simple static class of various interpolation functions that take the same arguments (minvalue, maxvalue, percent) and return a value between minvalue and maxvalue based on that number, then implement the timers that work with those things elsewhere in your code.


zoqfotpik(Posted 2012) [#3]
I'm just going to have tweens for scale value, rotation, position, color and alpha. Seems like that right there should give me a huge amount of polish for a comparatively small amount of work. Maybe I will have tween objects that can be stored into a queue.


AdamRedwoods(Posted 2012) [#4]
http://monkeycoder.co.nz/Community/posts.php?topic=1975


Shinkiro1(Posted 2012) [#5]
This code is from my framework (example included)
https://dl.dropbox.com/u/2892658/forum/Tweening.zip

Basically all you need to know is

Use
[monkeycode]InitTweenSystem()[/monkeycode]
in OnCreate()

and then the tweening function itself:
[monkeycode]
Tweening (typeOfTween, timeElapsed, begin, change, duration)
[/monkeycode]
typeOfTween is one of 31 different equations that you can use (linear, bounce, expo, etc.)
This function will return a float that you can then use for whatever you want.

e.g.
[monkeycode]object.x = Tweening (EASE_IN_EXPO, timeCounter, object.startPositionX, 300, 60)[/monkeycode]
The above would change the object's x by 300 in 60 frames, the timeCounter variable would be increased by 1 every frame.
Notice however, you can also use Milliseconds if that's what you want.


AaronK(Posted 2012) [#6]
My version of tweening is based on the Cocos2d style which I quite like. In mine you build sequences of actions then run them on an object. Most of those actions are tweening actions. It's still a work in progress but this is how it works.


sequence = new Sequence()
sequence.Add(new MoveTo(100,100,5))  ' x,y, time
sequence.Add(new FadeTo(0,1)) ' opacity, time

runner.Run(sequence, targetObject)


If you want to tween with a different function you might go

sequence.Add(new EaseIn(new MoveTo(100,100,5), 1.3))   ' Easin uses action, rate


And I can just write more tweening objects and they can tween pretty much any action.

I'll make some helpers at some point to make building these a little easier but this gives you an idea.


zoqfotpik(Posted 2012) [#7]
Looks like a list of function objects would be really easy to implement and maintain.