Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

SmoothDamp function for Roblox?

Asked by
Exsius 162
9 years ago

http://docs.unity3d.com/ScriptReference/Vector3.SmoothDamp.html

Is there anything similar to this?

Thanks,

Exsius ;)

1 answer

Log in to vote
2
Answered by
Unclear 1776 Moderation Voter
9 years ago

If you simply want to interpolate between a start vector and an end vector, then this is merely a matter of applying your favorite interpolation function to all dimensions of a vector.

Interpolation can be boiled down to getting what a value would be at a point in time between a start time and an end time. A variable alpha would describe (currentTime - startTime) / (endTime - startTime), or how completed the movement is from 0 to 1. An interpolation function would usually return the starting value when alpha is 0 and the ending value when alpha is 1. Whatever happens in between varies from function to function.

Here is what the overall code would look like:

function interpolate(startVector3, finishVector3, alpha)
    local function currentState(start, finish, alpha)
        -- your favorite interpolation function
    end

    return Vector3.new(
        currentState(startVector3.x, finishVector3.x, alpha),
        currentState(startVector3.y, finishVector3.y, alpha),
        currentState(startVector3.z, finishVector3.z, alpha)
    )
end

If my favorite function is linear interpolation, I would simply change the definition of currentState to that of a linear interpolation function:

function interpolate(startVector3, finishVector3, alpha)
    local function currentState(start, finish, alpha)
        return start + (finish - start)*alpha
    end

    return Vector3.new(
        currentState(startVector3.x, finishVector3.x, alpha),
        currentState(startVector3.y, finishVector3.y, alpha),
        currentState(startVector3.z, finishVector3.z, alpha)
    )
end

Or, if I wanted a quadratic interpolation, I would change the definition again like above:

function interpolate(startVector3, finishVector3, alpha)
    local function currentState(start, finish, alpha)
        return (start - finish)*alpha*(alpha-2) + start
    end

    return Vector3.new(
        currentState(startVector3.x, finishVector3.x, alpha),
        currentState(startVector3.y, finishVector3.y, alpha),
        currentState(startVector3.z, finishVector3.z, alpha)
    )
end

Here are some cool interpolation equations you should take a look at.

0
Thank you! Exsius 162 — 9y
Ad

Answer this question