Rebuilding Procedural Playground by hand
Stage 5 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 5 of 11

The camera

By the end

  • A camera with no perspective, at pixel art's 2:1 angle, following a box on stage 4's spring.
  • The game running for the first time.
  • A feel for the spring's three numbers, from changing them while it runs.

The idea

Three things make this camera, and stage 6 adds a fourth.

  1. No perspective. It's orthographic: far things aren't drawn smaller, and lines that are parallel in the world stay parallel on screen. How much it shows is set by how many metres of the world fit between the bottom of the screen and the top (view_height), not by how far away it stands.
  2. A fixed angle: turned 45° round and tilted 30° down. At 30° down a square floor tile is drawn as a diamond exactly twice as wide as it is tall (sin 30° = 0.5). That's pixel art's classic "2:1": the tile's edges climb one pixel for every two across, which pixels draw cleanly. True isometric is 35.26° down, and its edges don't land on pixels as neatly.
  3. It follows on a spring: stage 4's SecondOrder, on the position of what it follows.

Why it's like this

The angle was measured against t3ssel8r's game view in his video: his tiles repeat every 197 × 99 pixels, and this project's every 36 × 18 at the script's default zoom. Both are 2:1.

The script and its node

  1. Create camera/iso_camera.gd.
  2. In main.tscn, add a Camera3D under Main and name it Camera. Drag iso_camera.gd from the FileSystem dock onto it in the Scene dock: that attaches the script, and a small script icon shows by the node.
  3. It needs something to follow, and Leela isn't here yet. Add a MeshInstance3D under Main and name it StandIn. In the Inspector set Mesh to New → BoxMesh, and Transform → Position to 0, 0.5, 0 (only its y needs typing). That's a 1 m box sitting on the floor. You'll delete it in stage 7.
  4. Select Camera. The Inspector starts with a Camera Preview picture. Under it is a heading with the script's name, iso_camera.gd, and under that the script's exported values:
PropertyValueWhy
TargetStandIn: press Assign..., click StandIn in the Select a Node dialog, then OKThe node it follows.
View Height3.5Metres of the world from the bottom of the screen to the top.

Leave the rest: Pitch −30, Yaw 45, Distance 30, Snap on, and under Follow, Frequency 1, Damping 1, Response 0.

Clip · 1:29 · no sound Steps 2 to 4. Select a Node only lists the nodes the property can take. Target is a Node3D, so WorldEnvironment, which isn't one, is missing from it.

Why 3.5

The script's own default is 14, a long way out. The owner chose 7, then asked to zoom in twice the same day: 5, then 3.5.

In the editor's 3D view the camera still sits at the origin, looking along the floor. That's as it should be. The script isn't a @tool script, so it only places the camera when the game runs.

How it works

Its numbers. Each @export is a property you just saw in the Inspector:

camera/iso_camera.gd lines 13–18
@export var target: Node3D
@export_range(-89.0, -5.0) var pitch := -30.0  ## degrees down: -30 is pixel art's 2:1, true isometric -35.26
@export var yaw := 45.0           ## degrees round
@export var view_height := 14.0   ## m of the world from the bottom of the screen to the top
@export var distance := 30.0      ## m back (orthographic: only so nothing's behind it; look/environment.tres's depth fog is set for 30)
@export var snap := true          ## to the picture's pixels (off: plain following, and the pixels crawl)

When the game starts, it switches to orthographic and makes its follower, starting where the target is:

camera/iso_camera.gd lines 36–43
func _ready() -> void:
	projection = PROJECTION_ORTHOGONAL
	_shown = view_height
	_home_yaw = yaw
	_home_height = view_height
	var at := _target_position()
	_follow = SecondOrder.new(frequency, damping, response, at)
	_place(at)

Every frame it feeds the target's position through the spring and puts itself where that comes out. The first line hands the spring its three numbers again every frame, which is what lets you change them while the game runs:

camera/iso_camera.gd lines 46–47, 57
func _process(delta: float) -> void:
	_follow.set_parameters(frequency, damping, response)  # (so they can be tuned while it runs)
	# ...
	_place(_follow.update(delta, _target_position()))

And _place() begins like this:

camera/iso_camera.gd lines 87–92
func _place(at: Vector3) -> void:
	size = _shown
	far = distance + 100.0
	global_basis = Basis.from_euler(Vector3(deg_to_rad(pitch), deg_to_rad(yaw), 0.0))
	var b := global_basis
	var where := at + b.z * distance  # (it looks along -Z)
  • size is an orthographic camera's zoom: the metres from the bottom of the view to the top.
  • global_basis is which way it faces, built from the two angles.
  • where: a camera looks along its own −Z, so b.z points out of its back. It stands distance back that way from the point it's looking at.

With no perspective, distance changes nothing you can see, with one exception: fog is measured from the camera. That's why stage 3's fog numbers were set for 30. The rest of _place() is the pixel snapping, which waits for stage 6.

Checkpoint

Run the project (F5). The game shows in the editor's Game workspace, which pops out as a window of its own unless you've told it not to (Before you start says how).

  • You're looking down on the floor at an angle, and its tiles are diamonds twice as wide as tall.
  • The white box is in the middle of the picture, with a long shadow to its right.
  • The ground at the top of the picture fades into violet.

If not:

  • The view is flat along the floor, with a horizon: the script isn't on the Camera node.
  • An error naming SecondOrder: stage 4's script isn't at proc/second_order.gd, or its first line isn't class_name SecondOrder.
  • Everything is tiny: View Height is still 14.

Try it: the three numbers

This is the best place in the project to get the feel of f, z and r, because the thing being followed moves in clean jumps.

  1. With the game running, press Remote at the top of the Scene dock, open Main and click StandIn.
  2. In the Inspector change its Position's x from 0 to 4. The box jumps 4 m, and the camera glides after it.
  3. Click Camera, open Follow, set a row of this table, and move the box 4 m again:
FrequencyDampingResponseWhat the camera does (measured)
110The project's values. Halfway in a quarter of a second, there in about a second, no overshoot.
10.30Swings 1.5 m past the box and back, settling in about 1.8 s.
11−1Backs off 0.8 m the wrong way first, then goes. Anticipation.
112Leaps ahead, half a metre too far, and is there sooner.
310There in a third of a second.

Stop the game and the camera has its saved numbers again.

Checkpoint: before stage 6

  • The game is stopped, so StandIn is back at 0, 0.5, 0 and the camera's Follow numbers are 1, 1, 0 again.
  • main.tscn is saved, with Camera and StandIn under Main. Camera has iso_camera.gd on it, Target StandIn and View Height 3.5.