Code as World
A world represented as code, not just pixels: its composition, its dynamics and its appearance live in one small Python module, and the lab's own coding agent writes, steps and renders it locally. This is the first run, v0.1: one world, one seed, three frames. The dynamics are a script, not a solver. Every rule is a line you can read. A physical-simulation backend (MuJoCo) stays in reserve for the one scene that actually needs a solver instead of a script; this one does not.
Source: media/src/world/world.py (composition, step, render) and gen.py (seed it, run it, write the frames). Stdlib only, no model in the loop at render time. Same seed, same world, same frames.
The world, as code
Three parts, named the way the Code-as-World paper names them. The composition is a list of entities: a sun, the water with its tide and current, a headland and an island, a canoe (mass, floats, the one loud colour), an orca (mass, neutrally buoyant, a swim speed and a dive cycle), a heron, three clouds. The dynamics are the step function below: a sinusoidal tide on the water level, a sun arc, a current that follows the tide and drifts the canoe, an orca that swims and rides a dive cycle, a heron that stays put. The appearance is render: the state as flat shapes, 1200 by 900, earth tones first and one loud colour against them.
7, 180 ticks of dt = 0.05 h (9.0 h of world time). Tide -1.60 m at tick 0 to +0.25 m at tick 180, a rise of 1.85 m. Sun angle 0.0 to 129.6 degrees. The run crosses the turn of the tide at 6.21 h, so the current reverses inside it: the canoe drifts east to 541 px at the midpoint and is carried back west to 450 px by the end, a net 204 px east of where it started. Orca at 0.0 m depth at the end, surfaced, swimming west after turning at the frame edge. Heron at x = 883 in every frame, feet on the island's surface at that x.The step function, read from world.py when the site was built:
def step(state, dt):
"""Advance the world by dt hours. Every rule is explicit; nothing is solved."""
state["tick"] += 1
state["dt"] = dt
t = state["t"] = state["tick"] * dt
water, sun, canoe, orca = get(state, "water"), get(state, "sun"), get(state, "canoe"), get(state, "orca")
# tide: a sinusoid on the water level, low water at t = 0, one M2 period of 12.42 hours
tide = water["tide"]
water["tide_m"] = state["tide_m"] = -tide["amplitude_m"] * math.cos(2 * math.pi * t / tide["period_h"])
water["pos"][1] = state["water_y"] = WATER_Y0 - state["tide_m"] * PX_PER_M
# sun: an arc from the left horizon at sunrise (t = 0) to the right horizon at sunset
sun["angle_deg"] = state["sun_angle_deg"] = min(180.0, 180.0 * t / sun["day_length_h"])
a = math.radians(sun["angle_deg"])
sun["pos"] = [sun["arc"]["cx"] - sun["arc"]["rx"] * math.cos(a), sun["arc"]["cy"] - sun["arc"]["ry"] * math.sin(a)]
# current: follows the tide, east on the flood, west on the ebb; the canoe drifts with it
water["current_px_per_h"] = water["current"]["max_px_per_h"] * math.sin(2 * math.pi * t / tide["period_h"])
canoe["pos"][0] += water["current_px_per_h"] * dt
canoe["drift_px"] += water["current_px_per_h"] * dt
canoe["pos"][1] = state["water_y"] # it floats: the hull sits on whatever the water line is
# orca: swims at a steady pace and turns at the frame edge rather than wrapping, because a
# body that leaves one side and reappears at the other is not a persistent world;
# neutrally buoyant, so depth is set by the breath cycle rather than by weight
half = orca["size"][0] / 2
x = orca["pos"][0] + orca["heading"] * orca["swim_px_per_h"] * dt
if x > W - half:
x, orca["heading"] = 2 * (W - half) - x, -1 # reflect off the east edge
elif x < half:
x, orca["heading"] = 2 * half - x, 1 # reflect off the west edge
orca["pos"][0] = x
orca["depth_m"] = orca["dive"]["depth_m"] * max(0.0, math.sin(2 * math.pi * t / orca["dive"]["cycle_h"]))
orca["surfaced"] = orca["depth_m"] < 0.2
orca["pos"][1] = state["water_y"] + orca["depth_m"] * PX_PER_M
# heron: stays put. It is hunting, and the rock does not move.
return state
state.json as gen.py wrote it: the composition first, then the state at the end of the run.
{
"seed": 7,
"ticks": 180,
"dt_h": 0.05,
"composition": [
{
"id": "sun",
"kind": "sun",
"pos": [
80,
440
],
"size": 44,
"arc": {
"cx": 600,
"cy": 440,
"rx": 520,
"ry": 340
},
"day_length_h": 12.5,
"angle_deg": 0,
"palette": "earth"
},
{
"id": "water",
"kind": "water",
"pos": [
0,
520
],
"size": [
1200,
380
],
"tide": {
"amplitude_m": 1.6,
"period_h": 12.42
},
"tide_m": -1.6,
"current": {
"max_px_per_h": 90
},
"current_px_per_h": 0,
"palette": "earth"
},
{
"id": "headland",
"kind": "land",
"pos": [
0,
380
],
"size": [
1200,
220
],
"shape": [
[
0,
600
],
[
0,
470
],
[
140,
430
],
[
330,
400
],
[
520,
420
],
[
640,
450
],
[
700,
600
]
],
"palette": "earth"
},
{
"id": "island",
"kind": "land",
"pos": [
700,
440
],
"size": [
300,
180
],
"shape": [
[
700,
620
],
[
720,
500
],
[
780,
460
],
[
860,
440
],
[
960,
470
],
[
1000,
620
]
],
"palette": "earth"
},
{
"id": "canoe",
"kind": "canoe",
"pos": [
245.90662118665298,
584
],
"size": [
130,
26
],
"mass_kg": 55,
"buoyancy": "floats",
"drift_px": 0,
"palette": "loud"
},
{
"id": "orca",
"kind": "orca",
"pos": [
435.08491739245017,
584
],
"size": [
220,
70
],
"mass_kg": 4000,
"buoyancy": "neutral",
"swim_px_per_h": 110,
"heading": 1,
"dive": {
"depth_m": 4,
"cycle_h": 0.45
},
"depth_m": 0,
"surfaced": true,
"palette": "earth"
},
{
"id": "heron",
"kind": "heron",
"pos": [
882.5467236519927,
446.7640170955978
],
"size": [
34,
96
],
"mass_kg": 2.3,
"stands_on": "island",
"palette": "earth"
},
{
"id": "cloud-0",
"kind": "cloud",
"pos": [
172.43628666754276,
181.09994073213716
],
"size": [
183.88267002951028,
27.04398064594472
],
"palette": "earth"
},
{
"id": "cloud-1",
"kind": "cloud",
"pos": [
607.4357331894203,
96.37426193513743
],
"size": [
192.0374820394863,
27.25739762434314
],
"palette": "earth"
},
{
"id": "cloud-2",
"kind": "cloud",
"pos": [
190.71301334386504,
162.16826215422736
],
"size": [
239.22225496064456,
28.22843530069362
],
"palette": "earth"
}
],
"final_state": {
"tick": 180,
"t_h": 9,
"tide_m": 0.254,
"water_y": 509.8,
"sun_angle_deg": 129.6,
"canoe_x": 449.8,
"canoe_drift_px": 203.9,
"orca_x": 754.9,
"orca_depth_m": 0,
"orca_surfaced": true,
"heron_x": 882.5
}
}
Three frames from one seed
The same world at tick 0, at the midpoint and at the end. Nothing is drawn by hand between frames; each is render(state) after that many calls to step. The captions are the program's own numbers.
SVG · rendered state · tick 0Frame 1, tick 0t = 0.0 h, tick 0 of 180. Low water: tide -1.60 m, sun on the horizon at 0.0 degrees, the canoe at x = 246 px before any drift, the orca surfaced, the heron on the rock.Inquire
SVG · rendered state · tick 90Frame 2, mid runt = 4.5 h, tick 90 of 180. The tide rose 2.64 m to +1.04 m, the sun reached 64.8 degrees, the canoe drifted 295 px east on the flood, the orca is surfaced, the heron has not moved.Inquire
SVG · rendered state · tick 180Frame 3, end of runt = 9.0 h, tick 180 of 180. The tide rose 1.85 m to +0.25 m, the sun reached 129.6 degrees, the canoe drifted 204 px east on the flood, the orca is surfaced, the heron has not moved.Inquire
Watermarked like everything on Code as Art; the SVG originals stay in the repo's source tree. Inquire for a print or the source of a frame.
The second world: Kyoto
A temple-garden pond scene: a torii gate on the far bank, a stone lantern and a cherry tree on the near one, a koi swimming its lane, twelve petals falling and drifting on their own fixed cycles, one afternoon into dusk. Same three-part split, same discipline. Source: media/src/world-kyoto/kyoto.py and gen.py.
11, 140 ticks of dt = 0.05 h (7.0 h of world time). Sun angle 0.0 to 180.0 degrees. Lantern unlit at tick 0, lit at tick 140. Koi at x = 345 px at the end, ripple 4.6 px.The step function, read from kyoto.py when the site was built:
def step(state, dt):
"""Advance the world by dt hours. Every rule is explicit; nothing is solved."""
state["tick"] += 1
state["dt"] = dt
t = state["t"] = state["tick"] * dt
sun, pond, koi, lantern = get(state, "sun"), get(state, "pond"), get(state, "koi"), get(state, "lantern")
# sun: an arc from the left horizon at sunrise (t = 0) to the right horizon at sunset,
# same shape as the Salish Sea world's sun, a shorter day (one autumn afternoon into dusk)
sun["angle_deg"] = state["sun_angle_deg"] = min(180.0, 180.0 * t / sun["day_length_h"])
a = math.radians(sun["angle_deg"])
sun["pos"] = [sun["arc"]["cx"] - sun["arc"]["rx"] * math.cos(a), sun["arc"]["cy"] - sun["arc"]["ry"] * math.sin(a)]
# lantern: lights itself once the sun has dropped past dusk threshold, not before
lantern["lit"] = state["lantern_lit"] = sun["angle_deg"] >= 150.0
# koi: swims a sinusoidal lane across the pond, a small vertical bob riding the same period;
# the ripple it leaves is a pure function of how fast it is crossing the centre of the lane
sw = koi["swim"]
koi["pos"][0] = sw["center_x"] + sw["amp_px"] * math.sin(2 * math.pi * t / sw["period_h"])
koi["pos"][1] = sw["base_y"] + 7 * math.sin(4 * math.pi * t / sw["period_h"])
pond["ripple_amp"] = state["ripple_amp"] = 13.0 * abs(math.cos(2 * math.pi * t / sw["period_h"]))
# petals: each is a pure function of elapsed time since its own last spawn (t + t0, modulo its
# own fall cycle) - a steady fall, a steady eastward drift, a flutter riding on top of both
for e in state["entities"]:
if e["kind"] != "petal":
continue
local_t = (t + e["t0_h"]) % e["cycle_h"]
e["pos"][1] = e["spawn"][1] + e["fall_speed_px_h"] * local_t
e["pos"][0] = (e["spawn"][0] + e["drift_speed_px_h"] * local_t
+ e["flutter_amp_px"] * math.sin(2 * math.pi * local_t / e["flutter_period_h"] + e["phase"]))
return state
state.json as gen.py wrote it: the composition first, then the state at the end of the run.
{
"seed": 11,
"ticks": 140,
"dt_h": 0.05,
"composition": [
{
"id": "sun",
"kind": "sun",
"pos": [
80,
420
],
"size": 40,
"arc": {
"cx": 600,
"cy": 420,
"rx": 520,
"ry": 330
},
"day_length_h": 7,
"angle_deg": 0,
"palette": "earth"
},
{
"id": "hill",
"kind": "land",
"pos": [
0,
300
],
"size": [
1200,
260
],
"shape": [
[
0,
560
],
[
0,
420
],
[
180,
370
],
[
420,
340
],
[
650,
365
],
[
900,
335
],
[
1200,
400
],
[
1200,
560
]
],
"palette": "earth"
},
{
"id": "pond",
"kind": "water",
"pos": [
140,
560
],
"size": [
840,
220
],
"palette": "earth",
"ripple_amp": 0
},
{
"id": "cherry",
"kind": "tree",
"pos": [
260,
560
],
"size": [
220,
180
],
"canopy": {
"cx": 260,
"cy": 430,
"rx": 150,
"ry": 95
},
"trunk_w": 16,
"palette": "earth"
},
{
"id": "torii",
"kind": "torii",
"pos": [
800,
560
],
"size": [
150,
240
],
"palette": "loud"
},
{
"id": "lantern",
"kind": "lantern",
"pos": [
1090,
560
],
"size": [
42,
92
],
"lit": false,
"palette": "earth"
},
{
"id": "koi",
"kind": "koi",
"pos": [
490.4759107019637,
700
],
"size": [
74,
24
],
"swim": {
"amp_px": 230,
"period_h": 2.6,
"center_x": 560,
"base_y": 700
},
"palette": "earth"
},
{
"id": "pine-0",
"kind": "pine",
"pos": [
113.58634316482976,
412.72631752071186
],
"size": [
59.45040112159637,
90.15682546124542
],
"palette": "earth"
},
{
"id": "pine-1",
"kind": "pine",
"pos": [
585.2430897309938,
390.5398103156463
],
"size": [
60.190538224668884,
92.59765440433604
],
"palette": "earth"
},
{
"id": "pine-2",
"kind": "pine",
"pos": [
1067.578612351197,
387.82370368687657
],
"size": [
56.85442020199241,
81.81341074983679
],
"palette": "earth"
},
{
"id": "cloud-0",
"kind": "cloud",
"pos": [
897.2587529924905,
177.08138755577346
],
"size": [
153.76923027328615,
37.7507078911829
],
"palette": "earth"
},
{
"id": "cloud-1",
"kind": "cloud",
"pos": [
1046.167469880544,
171.54915469473767
],
"size": [
205.40064341207136,
26.204917331962275
],
"palette": "earth"
},
{
"id": "petal-0",
"kind": "petal",
"pos": [
177.92514370517296,
419.0609696393763
],
"spawn": [
177.92514370517296,
419.0609696393763
],
"size": [
11,
7
],
"fall_speed_px_h": 130,
"drift_speed_px_h": 22,
"flutter_amp_px": 15,
"flutter_period_h": 0.5,
"phase": 0.37417062902325593,
"t0_h": 0.5281045072374094,
"cycle_h": 2.776454079697105,
"palette": "earth"
},
{
"id": "petal-1",
"kind": "petal",
"pos": [
222.17888766471688,
391.65454240736335
],
"spawn": [
222.17888766471688,
391.65454240736335
],
"size": [
11,
7
],
"fall_speed_px_h": 130,
"drift_speed_px_h": 22,
"flutter_amp_px": 15,
"flutter_period_h": 0.5,
"phase": 2.914986190252419,
"t0_h": 1.3159866006294199,
"cycle_h": 2.98727275071259,
"palette": "earth"
},
{
"id": "petal-2",
"kind": "petal",
"pos": [
339.2732900611137,
418.5518263120242
],
"spawn": [
339.2732900611137,
418.5518263120242
],
"size": [
11,
7
],
"fall_speed_px_h": 130,
"drift_speed_px_h": 22,
"flutter_amp_px": 15,
"flutter_period_h": 0.5,
"phase": 4.023071451506697,
"t0_h": 1.3895545624879044,
"cycle_h": 2.780370566830583,
"palette": "earth"
},
{
"id": "petal-3",
"kind": "petal",
"pos": [
304.1776587186218,
415.1531434879757
],
"spawn": [
304.1776587186218,
415.1531434879757
],
"size": [
11,
7
],
"fall_speed_px_h": 130,
"drift_speed_px_h": 22,
"flutter_amp_px": 15,
"flutter_period_h": 0.5,
"phase": 1.747749044170597,
"t0_h": 2.7999363739898944,
"cycle_h": 2.8065142808617254,
"palette": "earth"
},
{
"id": "petal-4",
"kind": "petal",
"pos": [
369.15987012295886,
436.2118552221074
],
"spawn": [
369.15987012295886,
436.2118552221074
],
"size": [
11,
7
],
"fall_speed_px_h": 130,
"drift_speed_px_h": 22,
"flutter_amp_px": 15,
"flutter_period_h": 0.5,
"phase": 4.44729901407626,
"t0_h": 0.8337582271424072,
"cycle_h": 2.644524190599174,
"palette": "earth"
},
{
"id": "petal-5",
"kind": "petal",
"pos": [
219.7848508135416,
405.8971971022811
],
"spawn": [
219.7848508135416,
405.8971971022811
],
"size": [
11,
7
],
"fall_speed_px_h": 130,
"drift_speed_px_h": 22,
"flutter_amp_px": 15,
"flutter_period_h": 0.5,
"phase": 0.4412272606536586,
"t0_h": 2.2051572779438624,
"cycle_h": 2.8777138684439914,
"palette": "earth"
},
{
"id": "petal-6",
"kind": "petal",
"pos": [
253.07796195910575,
436.5620992034648
],
"spawn": [
253.07796195910575,
436.5620992034648
],
"size": [
11,
7
],
"fall_speed_px_h": 130,
"drift_speed_px_h": 22,
"flutter_amp_px": 15,
"flutter_period_h": 0.5,
"phase": 2.4285361434408186,
"t0_h": 2.530985115395894,
"cycle_h": 2.6418300061271935,
"palette": "earth"
},
{
"id": "petal-7",
"kind": "petal",
"pos": [
340.2254057940469,
390.0299715380564
],
"spawn": [
340.2254057940469,
390.0299715380564
],
"size": [
11,
7
],
"fall_speed_px_h": 130,
"drift_speed_px_h": 22,
"flutter_amp_px": 15,
"flutter_period_h": 0.5,
"phase": 1.3176933788887804,
"t0_h": 2.7306059208530455,
"cycle_h": 2.9997694497072587,
"palette": "earth"
},
{
"id": "petal-8",
"kind": "petal",
"pos": [
266.64751882266495,
443.9197417645861
],
"spawn": [
266.64751882266495,
443.9197417645861
],
"size": [
11,
7
],
"fall_speed_px_h": 130,
"drift_speed_px_h": 22,
"flutter_amp_px": 15,
"flutter_period_h": 0.5,
"phase": 2.4970910758945775,
"t0_h": 0.18882111889781647,
"cycle_h": 2.5852327556570303,
"palette": "earth"
},
{
"id": "petal-9",
"kind": "petal",
"pos": [
297.7437078856347,
432.8180972272158
],
"spawn": [
297.7437078856347,
432.8180972272158
],
"size": [
11,
7
],
"fall_speed_px_h": 130,
"drift_speed_px_h": 22,
"flutter_amp_px": 15,
"flutter_period_h": 0.5,
"phase": 1.6950500035325669,
"t0_h": 0.23272991230396425,
"cycle_h": 2.670630021329109,
"palette": "earth"
},
{
"id": "petal-10",
"kind": "petal",
"pos": [
239.85419696535286,
443.02419191265705
],
"spawn": [
239.85419696535286,
443.02419191265705
],
"size": [
11,
7
],
"fall_speed_px_h": 130,
"drift_speed_px_h": 22,
"flutter_amp_px": 15,
"flutter_period_h": 0.5,
"phase": 4.762909038622003,
"t0_h": 0.305848780922968,
"cycle_h": 2.592121600671869,
"palette": "earth"
},
{
"id": "petal-11",
"kind": "petal",
"pos": [
223.04565003416036,
395.55754699261877
],
"spawn": [
223.04565003416036,
395.55754699261877
],
"size": [
11,
7
],
"fall_speed_px_h": 130,
"drift_speed_px_h": 22,
"flutter_amp_px": 15,
"flutter_period_h": 0.5,
"phase": 0.37632134935635975,
"t0_h": 2.356991577791915,
"cycle_h": 2.9572496385183173,
"palette": "earth"
}
],
"final_state": {
"tick": 140,
"t_h": 7,
"sun_angle_deg": 180,
"lantern_lit": true,
"koi_x": 344.9,
"koi_y": 704.6,
"ripple_amp": 4.61
}
}
SVG · rendered state · tick 0Frame 1, tick 0t = 0.0 h, tick 0 of 140. Sun at 0.0 degrees on the arc, the lantern unlit, the koi at x = 490 px, ripple 0.0 px.Inquire
SVG · rendered state · tick 70Frame 2, mid runt = 3.5 h, tick 70 of 140. Sun reached 90.0 degrees (from 0.0), the lantern is unlit, the koi at x = 749 px, ripple 7.4 px.Inquire
SVG · rendered state · tick 140Frame 3, end of runt = 7.0 h, tick 140 of 140. Sun reached 180.0 degrees (from 0.0), the lantern is lit, the koi at x = 345 px, ripple 4.6 px.Inquire
Watermarked like everything on Code as Art; the SVG originals stay in the repo's source tree. Inquire for a print or the source of a frame.
Where it came from
How it started: a stake sign on the Work page while the idea was scoped. Two papers set the shape. Code-as-World (MirroS Lab, "Agentic Discovery of Executable World Representations for Physical Reasoning") gave the three-part split, composition, dynamic evolution and visual appearance as executable code. Their propose, execute, render, verify, refine loop did not produce this run, and Algorithm 1 is not in their release: the composition here is hand-authored, the dynamics are scripted, and there is no verify stage. Borrowing the split is what v0.1 does. Adding the loop is what a v0.2 would have to earn. Code World Model (Westlake AGI Lab / NTU, "Coding Agent as World Brain") gave the framing that the coding agent itself keeps the persistent world state and re-renders it, which is why state.json is on this page and not only the pictures. Reviewed with Pickles before ground was broken.
Next: a second world, and the first scene that needs something to fall, bounce or stack, which is the one MuJoCo is reserved for.