【问题标题】:How does Mathf.SmoothDamp() work? what is it algorithm?Mathf.SmoothDamp() 是如何工作的?它是什么算法?
【发布时间】:2020-08-05 21:49:08
【问题描述】:

我想知道SmoothDamp 如何统一工作。我正在尝试在统一之外重新创建该功能,但问题是我不知道它是如何工作的。

【问题讨论】:

    标签: c++ algorithm unity3d smoothing


    【解决方案1】:

    来自 Unity3d C# 参考source code

    // Gradually changes a value towards a desired goal over time.
            public static float SmoothDamp(float current, float target, ref float currentVelocity, float smoothTime, [uei.DefaultValue("Mathf.Infinity")]  float maxSpeed, [uei.DefaultValue("Time.deltaTime")]  float deltaTime)
            {
                // Based on Game Programming Gems 4 Chapter 1.10
                smoothTime = Mathf.Max(0.0001F, smoothTime);
                float omega = 2F / smoothTime;
    
                float x = omega * deltaTime;
                float exp = 1F / (1F + x + 0.48F * x * x + 0.235F * x * x * x);
                float change = current - target;
                float originalTo = target;
    
                // Clamp maximum speed
                float maxChange = maxSpeed * smoothTime;
                change = Mathf.Clamp(change, -maxChange, maxChange);
                target = current - change;
    
                float temp = (currentVelocity + omega * change) * deltaTime;
                currentVelocity = (currentVelocity - omega * temp) * exp;
                float output = target + (change + temp) * exp;
    
                // Prevent overshooting
                if (originalTo - current > 0.0F == output > originalTo)
                {
                    output = originalTo;
                    currentVelocity = (output - originalTo) / deltaTime;
                }
    
                return output;
            }
    

    【讨论】:

    • 这段代码实现了一个临界阻尼谐振子。请注意,从数学上讲,它从未真正达到目标。它只是渐近地接近它,作为一个限制。在实践中,32 位浮点值的准确性会很快达到目标。
    猜你喜欢
    • 2014-11-22
    • 2019-11-04
    • 2013-02-13
    • 2018-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-04
    • 2012-09-02
    相关资源
    最近更新 更多