Task 4 of 5
Accelerations are just numbers until an integrator turns them into motion. The
simplest scheme that doesn't wreck orbits is semi-implicit Euler: update
the velocity first, then move the body with the new velocity —
v′ = v + a·dt, then x′ = x + v′·dt. Do it in the other order
(plain Euler) and orbits visibly spiral outward, gaining energy from nowhere.
Both updates are embarrassingly parallel — body i never looks at body
j — so they're two tiny kernels. Between them, the [vx, vy] pairs
come back to JavaScript and get unpacked into plain arrays for the next kernel. Clunky?
Yes. Instructive? Also yes — and Pipelines & Textures shows how to skip
the round trip.
stepVel returns
[v + a·dt] per component, stepPos returns
[x + v·dt] — and feed the position step the new velocities.stepVel returns [vx + ax·dt, vy + ay·dt] for its bodystepPos returns [x + vx·dt, y + vy·dt] for its bodyEverything in both kernels is indexed by this.thread.x:
this body's velocity, this body's acceleration, this body's position.
return [velX[this.thread.x] + accX[this.thread.x] * dt,
velY[this.thread.x] + accY[this.thread.x] * dt];
— the position kernel is the
same shape with pos and vel.
This page is an interactive exercise — the editor, the GPU runner and your saved progress need JavaScript. The text above is the full brief.