{
"id": "266",
"prompt": "A ball bouncing on a trampoline that actually deforms: the surface is 12 columns, each with an offset and velocity in state slots, coupled to its neighbors by (left + right - 2*self)/6 plus a spring back to rest and 15/16 damping - so an impact dents the mesh and sends ripples sideways. The ball (fixed-point slots, gravity +3) lands on the column under it at the DISPLACED surface height, dumps half its impact speed into that column's velocity, and rebounds with a small fixed kick so the bouncing never dies. Draw the deformed surface as filled columns with a bright rim and the ball above it.",
"solution_rl": "fn frame(t: i32) {\n let w: i32 = host::display::width();\n let h: i32 = host::display::height();\n let colw: i32 = w / 12;\n let rest: i32 = (h - 120) * 8;\n if host::display::state_get(0) == 0 {\n host::display::state_set(0, 1);\n host::display::state_set(2, (w / 2 - 80) * 8);\n host::display::state_set(3, 80 * 8);\n host::display::state_set(4, 14);\n host::display::state_set(5, 0);\n }\n let mut off = [0; 12];\n let mut vel = [0; 12];\n let mut i: i32 = 0;\n while i < 12 {\n off[i] = host::display::state_get(8 + i);\n vel[i] = host::display::state_get(20 + i);\n i = i + 1;\n }\n i = 0;\n while i < 12 {\n let left: i32 = if i > 0 { off[i - 1] } else { 0 };\n let right: i32 = if i < 11 { off[i + 1] } else { 0 };\n vel[i] = vel[i] + (left + right - 2 * off[i]) / 6 - off[i] / 16;\n i = i + 1;\n }\n let mut bx: i32 = host::display::state_get(2);\n let mut by: i32 = host::display::state_get(3);\n let mut bvx: i32 = host::display::state_get(4);\n let mut bvy: i32 = host::display::state_get(5);\n bvy = bvy + 3;\n bx = bx + bvx;\n by = by + bvy;\n if bx < 10 * 8 { bx = 10 * 8; bvx = -bvx; }\n if bx > (w - 10) * 8 { bx = (w - 10) * 8; bvx = -bvx; }\n if by < 10 * 8 { by = 10 * 8; bvy = 0 - bvy; }\n let mut col: i32 = (bx / 8) / colw;\n if col < 0 { col = 0; }\n if col > 11 { col = 11; }\n let surf: i32 = rest + off[col];\n if by + 80 > surf {\n by = surf - 80;\n if bvy > 0 {\n vel[col] = vel[col] + bvy / 2;\n bvy = 0 - (bvy * 3 / 4) - 15;\n }\n }\n i = 0;\n while i < 12 {\n vel[i] = vel[i] * 15 / 16;\n off[i] = off[i] + vel[i];\n if off[i] > 480 { off[i] = 480; }\n if off[i] < -480 { off[i] = -480; }\n host::display::state_set(8 + i, off[i]);\n host::display::state_set(20 + i, vel[i]);\n i = i + 1;\n }\n host::display::state_set(2, bx);\n host::display::state_set(3, by);\n host::display::state_set(4, bvx);\n host::display::state_set(5, bvy);\n host::display::clear(0x0e1014);\n i = 0;\n while i < 12 {\n let top: i32 = (rest + off[i]) / 8;\n host::display::fill_rect(i * colw, top, colw - 1, h - top, 0x203828);\n host::display::fill_rect(i * colw, top, colw - 1, 3, 0x50a068);\n i = i + 1;\n }\n host::display::fill_rect(bx / 8 - 10, by / 8 - 10, 20, 20, 0xff9030);\n host::display::draw_string(8, 8, \"TRAMPOLINE MESH\", 0x606060, 1);\n host::display::present();\n}\n",
"tags": [
"simulation",
"spring-mesh",
"state",
"physics"
]
}