Object Tweening
by chris · in Torque Game Builder · 03/21/2005 (3:39 pm) · 7 replies
Initially, I wanted to create a new C++ class to handle positional tweening in T2D. Basically, it would be an object that would know how to perform various types of motion (quadratic easing, sinusoidal easing, etc), and would also keep track of all the necessary information about the object being tweened and the individual steps in the tween itself.
Then I started looking at the T2D source and realized I had no idea where to start :)
So I started to think about how I might do this with TorqueScript, and came up with some handy functions to animate an objects motion. These are based on the tweening functions in Robert Penner's "Programming Macromedia Flash MX"... I'm no math guy :)
Linear Tween
Using linearTween() -
Quadratic Ease-out
Using quadratic ease out is the same as linear tween -
These aren't optimized in any kind of way, and if any one knows of a better way to do it, please let me know :)
I hope these are at least of some use to people. If so, I might post up a few others (cubic easing, circular easing etc)
Thanks,
Chris
[edit]Fixed the loop as suggested by Matt[/edit]
Then I started looking at the T2D source and realized I had no idea where to start :)
So I started to think about how I might do this with TorqueScript, and came up with some handy functions to animate an objects motion. These are based on the tweening functions in Robert Penner's "Programming Macromedia Flash MX"... I'm no math guy :)
Linear Tween
function linearTween(%startX, %startY, %changeX, %changeY, %duration, %obj) {
%timeStep = 1;
%duration = %duration / 5;
while (%timeStep <= %duration) {
%x = %changeX * %timeStep / %duration + %startX;
%y = %changeY * %timeStep / %duration + %startY;
%obj.schedule((5 * %timeStep), setPosition, (%x SPC %y));
%timeStep++;
}
}Using linearTween() -
// get the current x and y position of the object we want to tween %x = getWord(%objectToTween.getPosition(), 0); %y = getWord(%objectToTween.getPosition(), 1); // create our tween, moving our object 20 units right // and 6 units down over a duration of 2 seconds (2000 ms) linearTween(%x, %y, 20, 6, 2000, %objectToTween);
Quadratic Ease-out
function easeOutQuad(%startX, %startY, %changeX, %changeY, %duration, %obj) {
%timeStep = 1;
%duration = %duration / 5;
while (%timeStep <= %duration) {
%timeStepNormal = %timeStep/%duration;
%x = -(%changeX) * (%timeStepNormal) * (%timeStepNormal - 2) + %startX;
%y = -(%changeY) * (%timeStepNormal) * (%timeStepNormal - 2) + %startY;
%obj.schedule((5 * %timeStep), setPosition, (%x SPC %y));
%timeStep ++;
}
}Using quadratic ease out is the same as linear tween -
// get the current x and y position of the object we want to tween %x = getWord(%objectToTween.getPosition(), 0); %y = getWord(%objectToTween.getPosition(), 1); // create our tween, moving our object 20 units right // and 6 units down over a duration of 2 seconds (2000 ms) easeOutQuad(%x, %y, 20, 6, 2000, %objectToTween);
These aren't optimized in any kind of way, and if any one knows of a better way to do it, please let me know :)
I hope these are at least of some use to people. If so, I might post up a few others (cubic easing, circular easing etc)
Thanks,
Chris
[edit]Fixed the loop as suggested by Matt[/edit]
About the author
#2
I'm desperately trying to find the time to put together some C++ examples so people like yourself can create some neat helpers like this.
- Melv.
03/21/2005 (9:42 pm)
Nice Chris.I'm desperately trying to find the time to put together some C++ examples so people like yourself can create some neat helpers like this.
- Melv.
#3
03/22/2005 (5:21 am)
Doung the tutorial game in C++ instead of script would be a good example, perhaps someone aside from the very busy Melv can do it?
#4
There seems to be a bug in both the tween functions, as in it doesn't auctally go all the way to where I tell it to. Ie, if I want it to go "4 0" from "2 0", it only gets to "5.9 0".
However, adding an = to "while (%timeStep < %duration) {" (ie, while (%timeStep <= %duration) {)
it goes all the way to "6 0".
03/31/2005 (7:03 pm)
Just a heads up.. (This one had be scratching my head for a while, as I'm using A* and it was only moving to the first tile with all the scheduling functions/$ismoving variables i'm using)There seems to be a bug in both the tween functions, as in it doesn't auctally go all the way to where I tell it to. Ie, if I want it to go "4 0" from "2 0", it only gets to "5.9 0".
However, adding an = to "while (%timeStep < %duration) {" (ie, while (%timeStep <= %duration) {)
it goes all the way to "6 0".
#5
Thanks for that. I didn't actually check to see how accurate they were :)
I've built linear tweening into the fxSceneObject2D class now, so all of the schedules can be avoided, and stopping the tween is as simple as calling %obj.stopTween().
If you're interested in testing it out, I might post something in the C++ section of the forum later today.
Chris
04/01/2005 (5:08 am)
Hey Matt,Thanks for that. I didn't actually check to see how accurate they were :)
I've built linear tweening into the fxSceneObject2D class now, so all of the schedules can be avoided, and stopping the tween is as simple as calling %obj.stopTween().
If you're interested in testing it out, I might post something in the C++ section of the forum later today.
Chris
#6
04/01/2005 (10:08 am)
I'd certainly like to see your c++ code. If I can see a good start, I should be able to add more complex tweens such as bezier curves. I was thinking of making a path class and passing it a sceneobject pointer instead of building the path into the sceneobject class, but I'm not sure my Torque-fu is good enough yet. Anyway, would love to see what your working on.
#7
I'd love to hear about any improvements you can make to them :)
Chris
04/01/2005 (11:56 am)
Okay, Matt(s) - I've posted the source for my in-class tweening functions. They're in the C++.I'd love to hear about any improvements you can make to them :)
Chris
Torque Owner Jorgen Ewelonn
If you have more examples I would be very grateful if you shared.
//Jorgen