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

Can the damper function be added? #188

Open
Rotanticu opened this issue Jan 1, 2025 · 2 comments
Open

Can the damper function be added? #188

Rotanticu opened this issue Jan 1, 2025 · 2 comments

Comments

@Rotanticu
Copy link

Can the damper function be added? For handling animations that often need to be interrupted, damper is a better choice than Tween.
Here are some introductions I found
https://theorangeduck.com/page/fitting-code-driven-displacement
https://theorangeduck.com/page/code-vs-data-driven-displacement

I have tried to implement a version before, but I couldn't achieve a good integration between Damp and the existing Tween Perhaps you can do better
https://github.com/Rotanticu/LitDamper

@AnnulusGames
Copy link
Owner

I'm not sure what functionality you need. What API do you envision for this specifically?

@Rotanticu
Copy link
Author

Rotanticu commented Jan 3, 2025

I'm not sure what functionality you need. What API do you envision for this specifically?

The damper is essentially equal to a Tweener with an infinite duration and a constantly changing target value.
Therefore, the damper needs to dynamically obtain the current value, the target value to be tracked, and set the calculated interpolation.
Just like this
public static DamperBuilder<TValue, TOptions, TAdapter> CreateDamper<TValue,TOptions, TAdapter>(Func<double> getCurrentValue, Action<double> setCurrentValue, Func<double> getTargetValue)
But because it is an infinite duration, the damper needs to pause Pause(), immediately complete CompleteImmediately() (set the current value as the target value), and check if IsComplete (check if the current value is equal to the target value within multiple frames) is completed
On completion delegation (Action onCompleted, boolean isOnce) (executed when the current value is equal to the target value within multiple frames, reactivated when the current value is not equal to the target value within multiple frames) (Is IsOnce only executed on the first completion)
In addition, an API WithSpring (enum SpringType) such as SimpleSpring, VelocitySpring, TimedSpring, DoubleSpring, etc. that can specify the type of damper tracking function is also required
And the API WithHalfTime (double halfTime) that specifies the tracking speed (determines how quickly the current value approaches the target value)
When using a damper, set it this way

var damperBuilder = LDamper.CreateDamper(
() =>followSlider.Value,                                                     //The function that requires the current value
(value) =>followSlider.Value = (float)value,                      //The function that set the current value
() =>targetSlider.Value)                                                    //The function that get the target value
.WithSpring(SpringType.SimpleSpring)                            //Set spring mode
.WithHalfTime(0.1665d)                                                   //Setting tracking half-life,can be understood as speed
.RunWithoutBinding()                                                      //This part is consistent with Tweener
.OnCompleted(()=>damperBuilder.Kill(),true);                //Similar to Tweener, but even if the damper is completed, it will not end because the target value may change again. So it is necessary to specify whether it should only be completed during the first activation

damperBuilder.Pause()
damperBuilder.Play()
damperBuilder.IsCompleted()                                       //As long as the difference between the current value and the target value remains within the error range for a period of time, it can be considered completed (it is best to specify the number of frames and error range required to determine completion)

damperBuilder.CompleteImmediately()

When I tried to merge the damper and the tweener, the problem I encountered was that the tweener doesn't need to care about specific current and target values, it is normalized from a fixed starting value to an ending value. But the damper is not. The damper needs to obtain specific current and target values for each frame, because the absolute difference between the current and target values determines the interpolation speed, while the target value may be constantly changing. I think changing the Tweener to obtain specific current and target values may unify the two.

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