Rebuilding Procedural Playground by hand
Stage 4 of 11: all the posts
  1. Before you start
  2. 1A new project
  3. 2Leela's model and texture
  4. 3The ground, the sun and the dusk
  5. 4Second-order dynamics
  6. 5The camera
  7. 6The picture
  8. 7Leela's scene
  9. 8Standing and walking on physics
  10. 9Her bones
  11. 10The player's keys
  12. 11Going further
  13. Last: full screen
  14. How this was checked

Stage 4 of 11

Second-order dynamics

By the end

  • The idea the whole project follows on: a spring and a damper, set by three numbers you can feel.
  • proc/second_order.gd, the script that does it.

It's the one post with nothing new to see. The camera puts it to work in stage 5.

This is the first piece of procedural animation, and the one the project uses most: for the camera, for Leela's head, and (in the parts this tutorial leaves out) for a knocked-off part flying home and for chasing the mouse.

The idea

Often one value should follow another: a camera follows a character, a head follows a point.

  • Copy it every frame and the follower is rigid. When the target jumps, it jumps.
  • Ease towards it (a lerp each frame) and it's smooth, but always in the same sleepy way.
  • Make it behave like a thing with weight and it has character. It takes a moment to get going. It can overshoot and settle, like something on a spring. It can even draw back first, like a character winding up.

The third is a spring and a damper. The spring pulls the follower towards its target, harder the further away it is. The damper resists movement, harder the faster the follower is moving, like the closer on a door. A spring alone bounces for ever; the damper takes the bounce out. How much it takes out is called the damping.

Second-order dynamics is that system written as one equation. x is the input (the target) and y the output (the follower):

y + k1 * y' + k2 * y'' = x + k3 * x'

y' is how fast y is moving, and y'' is how fast that speed is changing. ("Second-order" only means the equation goes as far as y''.) Turned round, it says how the output speeds up:

y'' = (x - y  +  k3 * x'  -  k1 * y') / k2
  • x - y: it speeds up towards the input, more the further away it is. The spring.
  • - k1 * y': its own speed holds it back. The damper.
  • k3 * x': it's nudged by how fast the input is moving.
  • / k2: a bigger k2 makes it heavier, slower to answer.

Nobody can tune k1, k2 and k3 by feel. The trick, from t3ssel8r's talk "Giving Personality to Procedural Animations using Math", is to work them out from three numbers you can feel:

NumberNameWhat it does
ffrequency, in HzHow quickly it answers, and how fast it wobbles if it wobbles.
zdamping0 rings for ever. Below 1 it overshoots and settles. At 1 it arrives without overshooting. Above 1 it's slow and soft.
rresponseHow it starts. 0 eases in. 1 moves at once. Above 1 it overshoots first. Below 0 it goes the other way first: anticipation.

The script

Create proc/second_order.gd. It's the first script you make, and the clip under Scripts and shaders shows this very one being made. Its first line, class_name SecondOrder, makes it a type that every other script can use by name.

The three numbers you feel become the three the equation needs:

proc/second_order.gd lines 31–37
func set_parameters(frequency: float, damping: float, response: float) -> void:
	f = maxf(frequency, 0.001)
	z = damping
	r = response
	_k1 = z / (PI * f)
	_k2 = 1.0 / ((TAU * f) * (TAU * f))
	_k3 = r * z / (TAU * f)

And this moves it on by one step of t seconds:

proc/second_order.gd lines 49–59
func update(t: float, x, xd = null):
	if t <= 0.0:
		return _y
	if xd == null:
		xd = (x - _xp) / t
		_xp = x
	# (k2 kept big enough for the step to stay stable however long it is)
	var k2 := maxf(_k2, maxf(t * t / 2.0 + t * _k1 / 2.0, t * _k1))
	_y = _y + _yd * t
	_yd = _yd + (x + xd * _k3 - _y - _yd * _k1) * (t / k2)
	return _y
  • xd is how fast the input is moving. If you don't pass it in, it's worked out from the input last time.
  • _y = _y + _yd * t: the output moves at its speed for t seconds.
  • _yd = _yd + (...) * (t / k2): its speed changes by the equation's y'' for t seconds.
  • The k2 line keeps it from blowing up. A simulation that goes in steps like this explodes when a step is long for how stiff the spring is (one slow frame is enough). So k2 is never allowed to be smaller than this step can stand: for that frame it goes a little soft.

x and _y have no type on purpose. The same sums work on a float, a Vector2 or a Vector3.

Checkpoint

There's nothing to see yet: this is only sums, and it's the one script in the tutorial whose result shows a stage later. Save it and check the script editor marks no line in red. The next stage puts it to work.