summaryrefslogtreecommitdiff
path: root/examples/spheres/shaders/physics.glsl
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-17 00:15:15 -0300
committerGabriel Schneider <[email protected]>2026-11-17 12:15:01 -0300
commit9bd77e54943d954291b5faef287ccca5dcebd7a4 (patch)
tree3f2d628781441568620b8108fab920713f26f649 /examples/spheres/shaders/physics.glsl
parente997fc3e6b312b30f5af90012fc22365bab701d4 (diff)
downloadsketchbook-9bd77e54943d954291b5faef287ccca5dcebd7a4.tar.gz
sketchbook-9bd77e54943d954291b5faef287ccca5dcebd7a4.zip
examples: micscope and spheres come home, as they were, building
Both lived outside any repository, beside sketchbook, with manifests that pointed at ../01-drat-meout/lib/sketchbook, which no longer exists. They are copied into examples/micscope and examples/spheres (their sources, shaders, tests, test material and web icons; not their caches, builds, packages, state files or .gitignore, which the root's covers), their manifests depend on "../.." as the other examples do, and their READMEs link sketchbook there. Nothing else changed: both build against today's library and pass their own zig build test (micscope 62/62 steps, 11/11 tests, its transport, record, render and tuner scripts; spheres 75/75 steps, 3/3 tests, its play script). The originals are untouched. micscope's test/reference.flac (3.7 MiB, CC BY-SA 3.0, credited in its test/README.md) is the rendering reference its render script opens. The README lists both examples. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'examples/spheres/shaders/physics.glsl')
-rw-r--r--examples/spheres/shaders/physics.glsl36
1 files changed, 36 insertions, 0 deletions
diff --git a/examples/spheres/shaders/physics.glsl b/examples/spheres/shaders/physics.glsl
new file mode 100644
index 0000000..5d74bee
--- /dev/null
+++ b/examples/spheres/shaders/physics.glsl
@@ -0,0 +1,36 @@
+// The three-body physics, shared by sim.comp and lookahead.comp so the live
+// system and its futures move alike: gravity with softening, and leapfrog
+// (kick, drift, kick), symplectic and time-reversible, split finer through
+// close passes, about 2 % of the local dynamical time a part: too coarse
+// and a close pass gains energy and flings a body out.
+
+void accelerations(vec2 p[3], vec3 m, float softening, out vec2 a[3]) {
+ float eps2 = softening * softening;
+ a[0] = vec2(0.0);
+ a[1] = vec2(0.0);
+ a[2] = vec2(0.0);
+ for (int i = 0; i < 3; i++) {
+ for (int j = i + 1; j < 3; j++) {
+ vec2 d = p[j] - p[i];
+ float inv = inversesqrt(dot(d, d) + eps2);
+ vec2 f = d * (inv * inv * inv);
+ a[i] += m[j] * f;
+ a[j] -= m[i] * f;
+ }
+ }
+}
+
+// One step of `dt`, in up to `most` parts; `a` holds the accelerations at
+// `p`, before and after.
+void leapfrog(inout vec2 p[3], inout vec2 v[3], inout vec2 a[3], float dt, vec3 m, float softening, float most) {
+ float near = min(min(distance(p[0], p[1]), distance(p[0], p[2])), distance(p[1], p[2])) + softening;
+ float local = sqrt(near * near * near / (m.x + m.y + m.z));
+ uint k = uint(clamp(ceil(abs(dt) / (0.02 * local)), 1.0, most));
+ float h = dt / float(k);
+ for (uint j = 0u; j < k; j++) {
+ for (int i = 0; i < 3; i++) v[i] += 0.5 * h * a[i];
+ for (int i = 0; i < 3; i++) p[i] += h * v[i];
+ accelerations(p, m, softening, a);
+ for (int i = 0; i < 3; i++) v[i] += 0.5 * h * a[i];
+ }
+}