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

smoothDamp Function Implementation!!! #2

Open
scc172fly opened this issue Nov 22, 2022 · 0 comments
Open

smoothDamp Function Implementation!!! #2

scc172fly opened this issue Nov 22, 2022 · 0 comments

Comments

@scc172fly
Copy link

static smoothDamp(current: number, target: number, currentVelocity: number, smoothTime: number, maxSpeed?: number, deltaTime?: number): number[] {
deltaTime = deltaTime ? deltaTime : Laya.timer.delta * 0.001;
maxSpeed = maxSpeed ? maxSpeed : Number.MAX_VALUE;
smoothTime = Math.max(0.0001, smoothTime);
let num = 2 / smoothTime;
let num2 = num * deltaTime;
let num3 = 1 / (1 + num2 + 0.48 * num2 * num2 + 0.235 * num2 * num2 * num2);
let num4 = current - target;
let num5 = target;
let max = maxSpeed * smoothTime;
num4 = this.clamp(num4, -max, max);
target = current - num4;
let num7 = (currentVelocity + (num * num4)) * deltaTime;
currentVelocity = (currentVelocity - num * num7) * num3;
let num8 = target + (num4 + num7) * num3;

    if ((num5 > current) == (num8 > num5)) {
        num8 = num5;
        currentVelocity = (num8 - num5) / deltaTime;
    }

    return [num8, currentVelocity];
}

return [current, currentVelocity]

It has been tested and perfect!

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

1 participant