Handmade Hero»Forums»Code
Jason
235 posts
Question concerning relationship between animations and time
Edited by Jason on Reason: Initial post
Since I've recently been trying to code some animations for my first game project I decided to check out what Casey had to say about the subject. For his day 273 stream he talks about how games typically do calculations in regards to time to update the state of the world. He talks about functions like f(t) and functions like f(t0) + f'(t1) * dt, with the first function typically associated with animation and the second typically associated with physics. In the video, from what I understand, 't' is just a time variable that holds the total time that has elapsed since the game has started since he says he just calculates it by doing t += dt every frame. What I don't understand is how 't' is used in practice for animations? Why are animations updated with t instead of with dt? Is my understand of what 't' is even correct?
Simon Anciaux
1337 posts
Question concerning relationship between animations and time
I suppose you're talking about 17:26 in the video.

boagz57
Is my understand of what 't' is even correct?


t is the amount of time elapsed from some point in time (e.g. when an animation has started), not from the game start.

boagz57
Why are animations updated with t instead of with dt?


t is the amount of time since the animation started. That amount is still calculated using dt (each frame t is incremented by dt).

boagz57
What I don't understand is how 't' is used in practice for animations?


When you create an animation in an animation package you have precise curves (or a key each frame) between 0 and the length of the animation. So you can call a function with a time parameter and get the exact result of the animation. This also work if you execute a function that can give the result independently of the previous state (the only parameter is the time). This is the f(t) case. It will always give you the same result. Note that this result can be used in further computation instead of using it directly (for example the root motion in Unity 3D that uses the displacement of the root node to move the actual object).

When you move a character, you can't use a function f(t) because depending of where the character is in the world, f(t) would need to return a different position. In other words, the current state f(t0) of the character is necessary to compute the next state f(t1). This is the f(t0) + f'(t1) * dt case. f'(t1) * dt is the amount of change from f(t0) to f(t1).

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/* In this example
 * - pos.x is updated using physics;
 * - pos.y is updated using animation.
 */
Vec2 pos = { };
r32 anination_t = 0.0f;
b32 jump = false;
while ( running ) {
    b32 move = keyPressed( "move" );
    if ( !jump && keyPressed( "jump" ) ) {
        jump = true;
        animation_t = 0.0f;
    }

    if ( move ) {
        pos.x = pos.x + speed * dt; /* f(t0) + f'(t1) * dt */
    }
    
    if ( jump ) {
        pos.y = animate( "jump", animation_t ); /* f(t) */
        animation_t += dt;
        if ( animation_t > animation_length( "jump" ) ) {
            jump = false;
        }
    }
}
Jason
235 posts
Question concerning relationship between animations and time
Ahh, okay. I'm going to try to sum up my current understanding of your explanation so please correct me if I'm wrong. So since (typically) each animation contains it's own sort of timeline it knows what to position its bones to at any given point within that animation. So if you pass a hypothetical animation function a time of 1.4 seconds then the function will look up what animation key frame is suppose to display at 1.4 seconds of play time and then pose the skeleton to that position and render. I'm guessing if there isn't a particular frame associated with that exact time then interpolation comes into play to draw an 'in-between' frame. So when Casey was saying there are typically lots of little t's being worked with at a low level he means there are typically lots of different times for which various animations have been started, stopped, reversed or what have you. These animation functions are usually a part of a greater whole though and are often coupled with different physics functions to animate AND then move the character into place. Does this sound right?
Simon Anciaux
1337 posts
Question concerning relationship between animations and time
Yes, it does sound right.

boagz57
These animation functions are usually a part of a greater whole though and are often coupled with different physics functions to animate AND then move the character into place.


You wrote "AND" in capitals so I want to clarify that nothing is required, you can use animation or "physics" or both as needed to achieve what you want. You can do character animation with "physics" approach and/or character movement using animation. It's generally not the case but depending on what you do it might be the thing to do.

Animation curves are often used to control other thing than animation. In the end, they are just functions that return a Y value for a given X value.
Jason
235 posts
Question concerning relationship between animations and time
Perfect, thank you. You had an excellent explanation which really helped.