Keyframes

The **keyframing** system is used by entity animations to create smooth animations which would otherwise be difficult to create with sinewaves.

## Usage

### Overview

In an animation, various keyframe transitions are used at different times to construct the overall movement. In the Spider Brain death animation, keyframes are used:

```
temp:entity/spider_brain/animate/death/main

```

```
...

# Apply keyframes

execute if score @s nexus.anim_time_06 matches 00..10 run function temp:entity/spider_brain/animate/death/keyframe/brain/fall
execute if score @s nexus.anim_time_06 matches 11..15 run function temp:entity/spider_brain/animate/death/keyframe/brain/bounce
execute if score @s nexus.anim_time_06 matches 16..20 run function temp:entity/spider_brain/animate/death/keyframe/brain/settle

execute if score @s nexus.anim_time_06 matches 00..13 run function temp:entity/spider_brain/animate/death/keyframe/leg/collapse
execute if score @s nexus.anim_time_06 matches 14..17 run function temp:entity/spider_brain/animate/death/keyframe/leg/fall
execute if score @s nexus.anim_time_06 matches 18..20 run function temp:entity/spider_brain/animate/death/keyframe/leg/settle

...
```
In each segment, certain parameters in the bone positions and rotations are interpolated from point A to point B. Parameters which share start and stop times are bundled together. While the animation timer scores don't have any overlap between the segments (to avoid needless computation), the timestamps used by the segments themselves for point A and point B share endpoints to ensure seamless movement.

A segment between keyframes consists of a timer variable, and a timestamp for point A and point B shared by a series of parameters. These parameters each having a position and a velocity at both points A and B. These parameters are in a single dimension. Multiple parameters can be combined to represent multiple dimensions. Since they are merely numbers to be interpolated, they may represent anything. Up to 20 parameters may be bundled together with a single function call.

### Calling the function

The parameters for the keyframe function are as follows:

```
nexus:entity/generic/animate/keyframe/<number>

```

#### Input

 `#point_01_b_velocity nexus.value` | The velocity of the first parameter at point B.

The number can be anywhere from 01 to 20 (zeros pad the values less than 10) to specify more parameters to interpolate as a bundle.

The function has a single output for every parameter.

### Blending keyframe segments

To ensure a smooth animation, have the point B parameters from the first segment be identical to the point A parameters in the second segment. It is possible, however, to make the animation discontinuous by making the segments not share values.

### Example segment

```
temp:entity/spider_brain/animate/death/keyframe/brain/bounce

```

```
# Assign variables

scoreboard players set #point_a_timestamp nexus.value 10
scoreboard players set #point_b_timestamp nexus.value 15
scoreboard players operation #input_timestamp nexus.value = @s nexus.anim_time_06

scoreboard players set #point_01_a_position nexus.value -1500
scoreboard players set #point_01_b_position nexus.value -1000
scoreboard players set #point_01_a_velocity nexus.value 200
scoreboard players set #point_01_b_velocity nexus.value 0

scoreboard players set #point_02_a_position nexus.value 200
scoreboard players set #point_02_b_position nexus.value 100
scoreboard players set #point_02_a_velocity nexus.value -20
scoreboard players set #point_02_b_velocity nexus.value 0

function nexus:entity/generic/animate/keyframe/02

scoreboard players operation #animate_brain_position_y temp.value = #output_point_01 nexus.value
scoreboard players operation #animate_brain_rotation_x temp.value = #output_point_02 nexus.value
```

## How it works

It uses the smoothstep function to interpolate the values, with the derivative of the function at the two timestamps being equal to the velocities at points A and B respectively.