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

Help with Scripting Animations?

Asked by 9 years ago

So i am looking to create a basic function that i can use to animate parts through a script. I decided to go down the path of CFraming since it is the most customizable compared to other forms like motors. so i wrote this general purpose function but my math skills aren't at a very high level so can someone tell me if im on the right track with this and how i can improve it

01function TweenJoint(joint, newCFrame, Time)
02    -- where joint is the part being moved,
03    -- newCFrame is the end pose 
04    -- Time is the length of the animation in seconds
05 
06    local length = Time -- in seconds
07    local Frames = 25
08    local TotalSteps = Frames * length
09    local CFStep = (joint.CFrame - newCFrame) / TotalSteps
10 
11    for i = 0, TotalSteps do
12        joint.CFrame = joint.CFrame + CFStep
13        wait(1/Frames)
14    end
15end

1 answer

Log in to vote
2
Answered by
EgoMoose 802 Moderation Voter
9 years ago

There are actually a few ways to tween CFrames, but to keep things simple let's just keep it to what you suggested, linear interpolation (LERP).

The basic math behind lerping is:

1-- a is start, b is end, c is the percentage between the two
2function lerp(a, b, c)
3    return a + ((b - a) * c);
4end;

With this basic equation we can get a value between two points linearly. Now some clarification, linear doesn't mean the way we change c (as I'll explain in a bit), but rather that we essentially get from point a to be in a straight line. From a number standpoint this doesn't make much sense, but once we get into 3D this can become convoluted. Regardless, that's a topic for another time (google SLERP if you dare :P)

Right, so now that we understand the basis of lerping let's make our own CFrame lerp function:

1function lerpCf(a, b, c)
2    local newCf = {};
3    local a, b = {a:components()}, {b:components()};
4    for i = 1, 12 do -- 12 components in a CFrame
5        newCf[i] = a[i] + ((b[i] - a[i]) * c); -- lerp equation
6    end;
7    return CFrame.new(unpack(newCf));
8end;

Now luckily for us, Roblox has a built in lerp method (that I think might be more accurate(?)) so we don't have to have this function in our code. That being said, it's still useful to understand it.

So onto the tween. We have 3 pieces of information. The start CFrame, the end CFrame, and the time. So taking a look at that information and what we need for a lerp what are we missing? -- The c-value our percentage!

The question becomes how do we translate our time into that c-percentage? The answer is to use tick:

01function tweenCf(child, property, goal, t)
02    local start = child[property];
03    local stime, ctime = tick(), 0;
04    while ctime < t do -- keep updating as long as time passed is < the tween length
05        ctime = (tick() - stime); -- how much time has passed
06        child[property] = start:lerp(goal, ctime/t); -- percentage of current time passed
07        wait(0); -- use renderstepped if you can
08    end;
09    child[property] = goal; -- set to the goal value incase of float math errors, etc
10end;

Now as mentioned above, we can also change how we handle the c-value by playing with what are called easing functions. They essentially change how the c-value move. Eg. It might be really fast at the start and slow down at the end, but it'll still add up to t seconds passed.

Here's an example:

01function tweenCf(child, property, goal, t, ease)
02    local start = child[property];
03    local stime, ctime = tick(), 0;
04    while ctime < t do
05        ctime = (tick() - stime);
06        child[property] = start:lerp(goal, ease(ctime, 0, 1, t)); -- easing function
07        wait(0);
08    end;
09    child[property] = goal;
10end;
11 
12function inOutQuad(t, b, c, d)
13    t = t / d * 2;
14    if t < 1 then
15        return c / 2 * math.pow(t, 2) + b;
View all 22 lines...

Here's a bit list of easing functions pre-written! https://github.com/EmmanuelOga/easing/blob/master/lib/easing.lua

0
Thank you this was extremely helpful ProfessorSev 220 — 9y
Ad

Answer this question