Animations
When building Kube.new, it was completely up to us to decide how you should be able to animate objects. This blog is about how we designed our animation system.
Skeleton Animators
In most places online, people animate characters using Skeletons. Skeletons are essentially just collections of points you move around. Your character follows the points in the skeleton.
So we created a Skeleton animator - an object that simply moves around a Skeleton. It points to three objects: 1. the Skeleton it should animate, 2. the animation to play, and 3. a Clock object that lets you start and stop it.
Here's what it looks like: (And here it is in our API: SkAnimator)
All we need next is a way to attach an object to that Skeleton to move along with it. We did that by adding the SkAttachment type which lets you "glue" items onto the Skeleton.
We were happy to find the AI responds well to this format! AI has seen millions of Blender files online. Our implementation is based directly on the GLTF format, which is the standard for storing skinned meshes. This format is clearly similar enough to our implementation to have some transfer.
Property Animators
We also wanted to support general animation, not just animation based on a Skeleton.
To do this, we introduced PropertyAnimator. It is very similar to the Skeleton Animator above, except it takes in the name of any field you want to animate, instead of pointing to a Skeleton.
You can animate any field, as long as it's a continuous number (position, velocity, color, texture width, anything!).
Tweens
The above animators work well when you want to bake an animation, but they are a little heavy when you want to dynamically move objects. For example, if you wanted to dynamically move a brick from point A to B, where A and B are not predetermined, then you would need to modify a handful of Keyframe and Clock objects in order to do that.
We expose a cheaper alternative: Tweens.
Tweens are basically PropertyAnimators with just two keyframes, but they are cheaper over the network, and they delete themselves when they're done. We expose tweenTo(), which creates a Tween. Just like PropertyAnimators, you can use Tweens to animate position, velocity, color, etc.
Blending
In order to deal with multiple animations on the same object (like Walking, Jumping, Idle on one character), we let each animator specify a priority and weight field. The animations are blended naturally in order of priority, until the total weight reaches 100.
For example, if you want to smoothly transition your character from Walking to Jumping, all you need to do is have two SkAnimator objects pointing to it, and transition the Walking weight down to 0 and the Jumping weight up to 100. We expose the smoothlyChangeWeight() function in TS so that this is easier. It literally updates every frame (not just per physics tick), so transitions happen very smoothly (240fps+).
Clocks
The Clock object is used internally by both animators. It is a special timing type that lets the server and client agree exactly when the animation started/stopped. It uses timestamps under the hood, which we discussed in our Networking blog.
// Simplification:struct Clock { started_at: f32, // a timestamp clock_location: ObjectLocation, // either Server or Client should_loop: bool, speed: f32, }High Framerate
One of the most interesting parts about our Desktop app is that we separate out our Physics thread from our Renderer thread. This allows us to simulate physics at the same rate on all devices (60 times/sec) while rendering the game at 240fps+!
Interpolation is the single mechanic that gives rise to higher framerates. To get a high framerate, we simply have the renderer thread interpolate between the latest two snapshots that the physics thread has given it.
We interpolate each object's position and velocity between the two most recent snapshots (using Hermite polynomials). The interpolation method can be customized when animations are involved (for example, stop-motion). Internally, we recompute skeletons per rendered frame too, so that skeleton animations are high-framerate.
Interpolation is built into our custom shader logic, so it also works when animating shadows, texture sizes, etc.
Andrew Pareles (@Kubester)