Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Repeating value is incorrect over time #660

Open
OldManMeta opened this issue Jun 24, 2023 · 1 comment
Open

Repeating value is incorrect over time #660

OldManMeta opened this issue Jun 24, 2023 · 1 comment

Comments

@OldManMeta
Copy link

Hi,

I'm using the lib to attempt to increment a value from a start to a target, and then back down to the start again.

I am using the following code:

        // Set up the TWEEN animation
        const tween = new TWEEN.Tween({ value: startValue })
          .to({ value: endValue }, duration)
          .repeat(1) // Repeats the animation once (forward and backward)
          .yoyo(true) // Causes the animation to play forward and then backward
          .onUpdate(({ value }) => {

            console.log('Updating tween',value);
            
          })
          .onComplete(({ value }) => {
            console.log('Completed tween=>',value);            
          })
          .start();        
       }

I'm calling an update withing a React Three Fiber useFrame() hook, which is executing at 60 FPS.

The issue occurs given that the method containing this code, could be called several times in a second, and indeed the same target value could be being modified at the same time if the duration of the up and back has not stopped.

It's only a small end value difference - so if the start value is 0 and the end value is 0.8, after a few instances of this, the return to the start value in the onComplete method, shows that the value is now 0.00003 instead of pure 0.

The error is then amplified on each successive call to the code.

This of course could be me just not understanding and making the mistake in my coding?

Many thanks

@trusktr
Copy link
Member

trusktr commented Jul 9, 2023

A working sample of the problem would help. It could be there is an issue, and that on return of the yoyo, it should snap to the original starting value like it currently does for non-yoyo end values.

A solution for this is to probably use two tweens instead of yoyo.

tween1.chain(tween2)
tween2.chain(tween1)

where tween 2 is set up to be the opposite of tween 1.


Generally speaking I'd like to remove timeline-like capabilities from the Tween class (yoyo, chain, repeat, etc) because it complicates what the concept of a tween is, and the add a separate Timeline type of feature. F.e.

const timeline= new Timeline([
  someTween,
  someTween.reverse()
])

// In loop
timeline.update(time)

But for now, I bet having two tweens, one in opposite direction, will solve the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants