๐Ÿ•ฐ๏ธ XR Pendulum Lab

theory vs. simulation

Most demos animate.
This one measures.

A pendulum lab with one C++17 core compiled twice โ€” native for tests, WebAssembly for the browser. The period is measured from real zero-crossings and compared to 2ฯ€โˆš(L/g), not assumed to match.

length 1.00 m ยท g 9.81 m/sยฒLIVE
2.006 sanalytic
crossingsmeasured
~0.1 msframe median

one core, three consumers

The physics doesn't get reimplemented

The same header compiles into the native test binary and into the WASM module the browser runs. There is no second implementation to drift out of sync.

// pendulum.hpp โ€” semi-implicit Euler, one step
const double acceleration =
  -gravity * std::sin(state.angleRadians) / length;
state.angularVelocity += acceleration * dt;
state.angleRadians    += state.angularVelocity * dt;

// small-angle theory, for comparison
T = 2.0 * PI * std::sqrt(length / gravity);
  • Sub-frame crossings. The period is interpolated between the two samples straddling a zero-crossing, so it isn't quantised to the frame rate.
  • Measurement, not assertion. The HUD shows analytic period next to the measured one โ€” the gap is the interesting part.
  • Warnings are errors. The native suite compiles with -Wall -Wextra -Werror.
  • Reset is clean. Changing length or gravity reinitialises the lab instead of leaving stale state.

in the browser

Three pendulums, a HUD, and controllers

scene

Three.js 0.186

Orbit controls with damping, three pendulums at scaled lengths so the period change is visible side by side.

hud

Canvas textures

Period, measured period, error and median frame time over the last 30 frames, rendered into the scene.

xr

WebXR controllers

Controller objects are wired into the scene; grab-and-release interaction is the next milestone.

run it

Two toolchains, one command each

# needs emscripten (emcc) and a C++17 compiler
$ npm install
$ npm run dev      # builds wasm, then vite
$ npm test         # native + wasm suites

# CXX=clang++ to pick a compiler

What the tests actually do

nativecompiles tests/physics.cpp, runs it
wasmnode --test tests/wasm.test.mjs
harnessscripts/test-native.mjs

status

What works, what's next

Working

Simulation, period measurement, native/WASM parity, browser scene with frame profiling.

Next

OpenXR grab-and-release on the bob, so the pendulum can be perturbed by hand in a headset.

Also planned

An error-versus-dt sweep that makes the integrator's accuracy visible instead of claimed.