truyet / hotween

Automatically exported from code.google.com/p/hotween
0 stars 1 forks source link

OnStepComplete with loop #45

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1.Tween the localRotation of an object using "new PlugQuaternion( new Vector3( 
60f, 0f, 0f ), true )".
2.Set and incremental loop by 2.
3.Add an OnStepComplete callback with "Debug.Log( "X: " + 
_transform.localEulerAngles.x + ", Y: " + _transform.localEulerAngles.y + ", Z: 
" + _transform.localEulerAngles.z );"

What is the expected output? What do you see instead?

I was expecting the following logs:

X: 60, Y: 0, Z: 0;
X: 60, Y: 180, Z: 180;
X: 0, Y: 180, Z: 180;

Instead of:

X: 60.03286, Y: 0, Z: 0;
X: 59.12921, Y: 180, Z: 180;
X: -5.008956E-06, Y: 180, Z: 180;

What version of the product are you using? On what operating system?

Version 1.1.724

Please provide any additional information below.

I believe the OnStepComplete callback is executed before the next loop tween 
started.

Original issue reported on code.google.com by marcos.c...@gmail.com on 17 Dec 2012 at 6:38

GoogleCodeExporter commented 9 years ago
Hi Marcos,

that is not a bug. As you can see, the last result is as you expected (the X is 
not 0 but almost infinitely next to it, so is considered 0 - that number is due 
to floating point imprecisions that are due neither to HOTween nor to Unity, 
but simply to every CPU), because the tween is complete and thus is at the 
perfect end of a loop. For the rest instead, you will never have the exact "end 
of loop" result you expect during an OnStepComplete callback, because HOTween 
uses a very high precision tween calculation, and doesn't simply complete a 
loop and wait the next frame to restart it, but calculates the correct time and 
starts a loop before the next frame if needed.

Let me explain better. Let's say we have a tween of a simple number called 
someNum, which goes from 0 to 1 and loops 3 times. Let's also say that we have 
a slow framerate, and each frame takes 400 milliseconds to elapse.
1st frame: 400 MS have elapsed, someNum is 0.4
2nd frame: 800 MS have elapsed, someNum is 0.8
3rd frame: 1200 MS have elapsed, OnStepComplete is called. But the value of 
someNum will be 0.2 this time, since we already stepped in another loop and 
restarted the tween.

I worked a lot to implement this feature. Without it, each loop cycle would 
"waste" some milliseconds at each completion, while waiting for the next frame 
to restart. This means that 1000 loops of 1 second each won't really last 1000 
seconds, but 1000 seconds + all the wasted time. With this precise tween 
calculation instead, no time is wasted, and 1000 loops of 1 second each will 
last 1000 seconds :)

Original comment by daniele....@gmail.com on 17 Dec 2012 at 6:58

GoogleCodeExporter commented 9 years ago
"and doesn't simply complete a loop and wait the next frame to restart it, but 
calculates the correct time and starts a loop before the next frame if needed"

This was the answer I needed. Probably I should use a different aproach for 
what I'm working.

By the way, thanks for answering so fast and for keeping HOTween update.

Original comment by marcos.c...@gmail.com on 17 Dec 2012 at 7:48