Prompt specification
Build or customize the Three.js 3D scene "Rotational inertia exploration rig" inside the active workspace.
Engineering Constraints:
1. Runtime & Orbit: Use Three.js r160 with OrbitControls (damping enabled) and ACESFilmicToneMapping.
2. Frame Timing: Drive all motion strictly with clock.getDelta() so animation speed is FPS-independent.
3. Viewport Hygiene: Handle window resize cleanly, cap DPR at Math.min(window.devicePixelRatio, 2), and dispose geometries/materials/renderer on unmount.
Scene Metadata:
Scene: Rotational inertia exploration rig
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/rotational-inertia-exploration-rig-x1364
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows angular momentum and rotational inertia. Let users move masses radially on an isolated toy rotor; update angular speed from conserved momentum and explicitly account for work associated with moving the masses. Low-poly.
Working Three.js Starter (rotational-inertia-exploration-rig-x1364):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Radial Rotor — Angular Momentum Lab</title>
<style>
:root {
color-scheme: dark;
--bg: #07111b;
--panel: rgba(10, 22, 34, 0.84);
--panel-border: rgba(153, 190, 214, 0.17);
--text: #edf7fb;
--muted: #91a8b7;
--cyan: #6fe7ef;
--mint: #76f0bd;
--amber: #ffd166;
--orange: #ff9b54;
--violet: #c39cff;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#scene canvas:active {
cursor: grabbing;
}
.vignette {
position: fixed;
inset: 0;
pointer-events: none;
background:
radial-gradient(circle at 52% 42%, transparent 25%, rgba(0, 0, 0, 0.42) 100%),
linear-gradient(180deg, rgba(4, 12, 20, 0.12), rgba(4, 12, 20, 0.35));
z-index: 1;
}
.ui {
position: fixed;
z-index: 2;
pointer-events: none;
}
.panel {
pointer-events: auto;
border: 1px solid var(--panel-border);
border-radius: 16px;
background: var(--panel);
box-shadow:
0 18px 55px rgba(0, 0, 0, 0.28),
inset 0 1px 0 rgba(255, 255, 255, 0.035);
backdrop-filter: blur(16px);
}
header {
top: 20px;
left: 22px;
max-width: 480px;
}
.eyebrow {
color: var(--cyan);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 5px 0 7px;
font-size: clamp(24px, 3vw, 38px);
line-height: 1;
letter-spacing: -0.035em;
}
header p {
margin: 0;
color: var(--muted);
font-size: 13px;
line-height: 1.5;
}
#hud {
top: 20px;
right: 20px;
width: 350px;
padding: 15px;
}
.hud-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 12px;
}
.badge {
border: 1px solid rgba(118, 240, 189, 0.28);
border-radius: 999px;
padding: 5px 8px;
color: var(--mint);
background: rgba(118, 240, 189, 0.08);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.1em;
white-space: nowrap;
}
.metrics {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 8px;
}
.metric {
min-width: 0;
padding: 10px;
border: 1px solid rgba(255, 255, 255, 0.055);
border-radius: 11px;
background: rgba(255, 255, 255, 0.025);
}
.metric span {
display: block;
margin-bottom: 5px;
color: var(--muted);
font-size: 9px;
font-weight: 700;
letter-spacing: 0.06em;
text-transform: uppercase;
}
.metric strong {
display: block;
overflow: hidden;
font-size: 17px;
line-height: 1.1;
text-overflow: ellipsis;
white-space: nowrap;
}
.metric small {
display: block;
margin-top: 4px;
color: var(--muted);
font-size: 10px;
}
.energy-strip {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 8px;
}
.energy-strip .metric strong {
color: var(--amber);
}
.ledger {
margin-top: 9px;
padding: 9px 10px;
border-radius: 10px;
color: var(--muted);
background: rgba(0, 0, 0, 0.15);
font-size: 10px;
line-height: 1.5;
}
.ledger b {
color: var(--text);
font-weight: 700;
}
#controls {
bottom: 20px;
left: 20px;
width: 390px;
max-height: calc(100vh - 40px);
overflow: auto;
padding: 15px;
scrollbar-width: thin;
}
.mass-list {
display: grid;
gap: 9px;
}
.mass-control {
padding: 9px 10px 11px;
border: 1px solid rgba(255, 255, 255, 0.055);
border-radius: 12px;
background: rgba(255, 255, 255, 0.025);
}
.mass-title {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-bottom: 8px;
font-size: 12px;
}
.mass-name {
display: flex;
align-items: center;
gap: 8px;
font-weight: 750;
}
.dot {
width: 9px;
height: 9px;
border-radius: 50%;
box-shadow: 0 0 12px currentColor;
}
.mass-meta {
color: var(--muted);
font-size: 10px;
}
.mass-meta b {
color: var(--text);
}
input[type="range"] {
--p: 50%;
width: 100%;
height: 5px;
margin: 4px 0 2px;
appearance: none;
border-radius: 999px;
outline: none;
background:
linear-gradient(
90deg,
var(--slider-color) 0%,
var(--slider-color) var(--p),
rgba(255, 255, 255, 0.11) var(--p),
rgba(255, 255, 255, 0.11) 100%
);
cursor: ew-resize;
}
input[type="range"]::-webkit-slider-thumb {
width: 15px;
height: 15px;
appearance: none;
border: 2px solid #07111b;
border-radius: 50%;
background: var(--slider-color);
box-shadow: 0 0 0 1px var(--slider-color);
cursor: grab;
}
input[type="range"]::-moz-range-thumb {
width: 13px;
height: 13px;
border: 2px solid #07111b;
border-radius: 50%;
background: var(--slider-color);
box-shadow: 0 0 0 1px var(--slider-color);
cursor: grab;
}
.buttons {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
gap: 7px;
margin-top: 11px;
}
button {
min-height: 35px;
border: 1px solid rgba(255, 255, 255, 0.1);
border-radius: 9px;
color: var(--text);
background: rgba(255, 255, 255, 0.045);
font: inherit;
font-size: 10px;
font-weight: 750;
cursor: pointer;
transition:
background 150ms ease,
border-color 150ms ease,
transform 150ms ease;
}
button:hover {
border-color: rgba(111, 231, 239, 0.45);
background: rgba(111, 231, 239, 0.09);
}
button:active {
transform: translateY(1px);
}
details {
margin-top: 11px;
border-top: 1px solid rgba(255, 255, 255, 0.065);
padding-top: 10px;
color: var(--muted);
font-size: 10px;
line-height: 1.55;
}
summary {
color: var(--text);
font-size: 11px;
font-weight: 750;
cursor: pointer;
}
details p {
margin: 8px 0;
}
.formula {
display: block;
margin: 7px 0;
padding: 8px 9px;
border-radius: 8px;
color: #d8f6ff;
background: rgba(111, 231, 239, 0.07);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 10px;
}
#help {
right: 20px;
bottom: 20px;
padding: 10px 12px;
color: var(--muted);
font-size: 10px;
line-height: 1.6;
text-align: right;
}
#help kbd {
padding: 2px 5px;
border: 1px solid rgba(255, 255, 255, 0.14);
border-radius: 4px;
color: var(--text);
background: rgba(255, 255, 255, 0.05);
font: inherit;
}
#status {
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
padding: 7px 11px;
border: 1px solid rgba(255, 209, 102, 0.28);
border-radius: 999px;
color: var(--amber);
background: rgba(7, 17, 27, 0.78);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.13em;
opacity: 0;
transition: opacity 150ms ease;
}
#status.visible {
opacity: 1;
}
@media (max-width: 850px) {
header {
top: 13px;
left: 14px;
max-width: calc(100vw - 28px);
}
header p {
display: none;
}
#hud {
top: 80px;
left: 12px;
right: 12px;
width: auto;
}
#controls {
bottom: 12px;
left: 12px;
width: min(370px, calc(100vw - 24px));
max-height: 48vh;
}
#help {
display: none;
}
.metrics {
grid-template-columns: repeat(3, 1fr);
}
}
@media (max-width: 480px) {
h1 {
font-size: 25px;
}
#hud {
padding: 10px;
}
.metric {
padding: 8px;
}
.metric strong {
font-size: 14px;
}
.metric small,
.energy-strip .metric small {
display: none;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<div class="vignette"></div>
<header class="ui">
<div class="eyebrow">Angular momentum laboratory</div>
<h1>Radial Rotor</h1>
<p>
Move the masses. Angular momentum stays fixed, while the radial actuator
exchanges work with the rotor.
</p>
</header>
<section id="hud" class="ui panel" aria-live="polite">
<div class="hud-heading">
<div>
<div class="eyebrow">Live state</div>
<div style="margin-top:4px;font-size:13px;font-weight:750">
Isolated from external torque
</div>
</div>
<div class="badge">L LOCKED</div>
</div>
<div class="metrics">
<div class="metric">
<span>Angular speed</span>
<strong id="omegaValue">—</strong>
<small id="rpmValue">—</small>
</div>
<div class="metric">
<span>Inertia</span>
<strong id="inertiaValue">—</strong>
<small>kg·m²</small>
</div>
<div class="metric">
<span>Momentum</span>
<strong id="momentumValue">—</strong>
<small>kg·m²/s</small>
</div>
<div class="metric">
<span>Rotational KE</span>
<strong id="kineticValue">—</strong>
<small>joules</small>
</div>
<div class="metric">
<span>Net radial work</span>
<strong id="workValue">—</strong>
<small>on the rotor</small>
</div>
<div class="metric">
<span>Last adjustment</span>
<strong id="lastWorkValue">—</strong>
<small id="lastWorkDirection">no motion yet</small>
</div>
</div>
<div class="energy-strip">
<div class="metric">
<span>Energy ledger</span>
<strong id="ledgerValue">—</strong>
<small>K − W = reference</small>
</div>
<div class="metric">
<span>Reference energy</span>
<strong id="referenceValue">—</strong>
<small>joules</small>
</div>
</div>
<div class="ledger">
Outward motion releases energy; pulling a mass inward adds energy.
The mint arrow is <b>constant L</b>; the yellow arrow is <b>ω</b>.
</div>
</section>
<section id="controls" class="ui panel">
<div class="hud-heading">
<div>
<div class="eyebrow">Mass positions</div>
<div style="margin-top:4px;font-size:13px;font-weight:750">
Drag a mass or use a slider
</div>
</div>
</div>
<div class="mass-list">
<div class="mass-control" style="--slider-color:#ff9b54">
<div class="mass-title">
<span class="mass-name">
<i class="dot" style="color:#ff9b54;background:#ff9b54"></i>
Mass A
</span>
<span class="mass-meta">
r = <b id="radius0">—</b> m ·
W = <b id="massWork0">—</b> J
</span>
</div>
<input id="radiusSlider0" type="range" min="0.7" max="3.15" step="0.01"
aria-label="Mass A radial position" />
</div>
<div class="mass-control" style="--slider-color:#6fe7ef">
<div class="mass-title">
<span class="mass-name">
<i class="dot" style="color:#6fe7ef;background:#6fe7ef"></i>
Mass B
</span>
<span class="mass-meta">
r = <b id="radius1">—</b> m ·
W = <b id="massWork1">—</b> J
</span>
</div>
<input id="radiusSlider1" type="range" min="0.7" max="3.15" step="0.01"
aria-label="Mass B radial position" />
</div>
<div class="mass-control" style="--slider-color:#c39cff">
<div class="mass-title">
<span class="mass-name">
<i class="dot" style="color:#c39cff;background:#c39cff"></i>
Mass C
</span>
<span class="mass-meta">
r = <b id="radius2">—</b> m ·
W = <b id="massWork2">—</b> J
</span>
</div>
<input id="radiusSlider2" type="range" min="0.7" max="3.15" step="0.01"
aria-label="Mass C radial position" />
</div>
</div>
<div class="buttons">
<button id="pauseButton" type="button">Pause spin</button>
<button id="resetButton" type="button">Reset experiment</button>
<button id="vectorsButton" type="button">Hide vectors</button>
</div>
<details open>
<summary>Why does the speed change if momentum is conserved?</summary>
<p>
A radial force produces no torque about the axis, so
<b>L = Iω</b> remains constant. Moving mass outward increases
<b>I</b>, so <b>ω</b> must decrease.
</p>
<span class="formula">dW<sub>on</sub> = −mω²r dr</span>
<span class="formula">dK<sub>rot</sub> = dW<sub>on</sub></span>
<p>
This model uses slow, quasi-static radial motion and neglects radial
kinetic energy. Positive work means energy was added while pulling a
mass inward; negative work means energy was released outward.
</p>
</details>
</section>
<div id="help" class="ui panel">
<div><kbd>Drag mass</kbd> change radius</div>
<div><kbd>Drag empty space</kbd> orbit camera</div>
<div><kbd>Wheel</kbd> zoom · <kbd>Space</kbd> pause · <kbd>R</kbd> reset</div>
</div>
<div id="status" class="ui">RADIAL HOLD · ANGULAR MOMENTUM STILL LOCKED</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ---------------------------------------------------------------------
// Physical model
//
// L is never changed by a radial adjustment:
// L = I * omega
//
// For one slow radial adjustment, with the other masses fixed:
// K = L² / (2I)
// ΔW_on = K_new - K_old
// = L²/2 * (1/I_new - 1/I_old)
//
// Equivalently, dW_on = -m*omega²*r*dr.
// Radial kinetic energy is intentionally neglected.
// ---------------------------------------------------------------------
const MASS = 1.25; // kg
const FIXED_INERTIA = 1.10; // kg m², hub + arms + shafts
const INITIAL_OMEGA = 1.55; // rad/s
const INITIAL_RADII = [2.35, 1.45, 2.85];
const MIN_RADIUS = 0.70;
const MAX_RADIUS = 3.15;
const ROTOR_Y = 0.55;
const colors = [0xff9b54, 0x6fe7ef, 0xc39cff];
const cssColors = ["#ff9b54", "#6fe7ef", "#c39cff"];
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07111b);
scene.fog = new THREE.Fog(0x07111b, 13, 30);
const camera = new THREE.PerspectiveCamera(
43,
innerWidth / innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
document.getElementById("scene").appendChild(renderer.domElement);
// ---------------------------------------------------------------------
// Lighting and environment
// ---------------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0xb9eaff, 0x14202c, 1.75));
const keyLight = new THREE.DirectionalLight(0xffffff, 3.2);
keyLight.position.set(5, 10, 5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -7;
keyLight.shadow.camera.right = 7;
keyLight.shadow.camera.top = 7;
keyLight.shadow.camera.bottom = -7;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 25;
keyLight.shadow.bias = -0.0004;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x54d9ff, 1.4);
rimLight.position.set(-6, 4, -5);
scene.add(rimLight);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(34, 34),
new THREE.MeshStandardMaterial({
color: 0x0a1721,
roughness: 0.94,
metalness: 0.03
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -1.05;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(28, 28, 0x244354, 0x142c3a);
grid.position.y = -1.035;
grid.material.transparent = true;
grid.material.opacity = 0.46;
scene.add(grid);
const floorRing = new THREE.Mesh(
new THREE.RingGeometry(3.48, 3.51, 64),
new THREE.MeshBasicMaterial({
color: 0x39758a,
transparent: true,
opacity: 0.45,
side: THREE.DoubleSide
})
);
floorRing.rotation.x = -Math.PI / 2;
floorRing.position.y = -1.025;
scene.add(floorRing);
// ---------------------------------------------------------------------
// Low-poly rotor
// ---------------------------------------------------------------------
const rotor = new THREE.Group();
rotor.position.y = ROTOR_Y;
scene.add(rotor);
const hubMaterial = new THREE.MeshStandardMaterial({
color: 0x304858,
roughness: 0.5,
metalness: 0.55,
flatShading: true
});
const hub = new THREE.Mesh(
new THREE.CylinderGeometry(0.56, 0.68, 0.48, 12),
hubMaterial
);
hub.position.y = 0.12;
hub.castShadow = true;
hub.receiveShadow = true;
rotor.add(hub);
const hubCap = new THREE.Mesh(
new THREE.CylinderGeometry(0.43, 0.52, 0.18, 12),
new THREE.MeshStandardMaterial({
color: 0x8aa2ad,
roughness: 0.36,
metalness: 0.68,
flatShading: true
})
);
hubCap.position.y = 0.43;
hubCap.castShadow = true;
rotor.add(hubCap);
const axle = new THREE.Mesh(
new THREE.CylinderGeometry(0.075, 0.10, 1.30, 10),
new THREE.MeshStandardMaterial({
color: 0xb5c7ce,
roughness: 0.3,
metalness: 0.78,
flatShading: true
})
);
axle.position.y = 0.86;
axle.castShadow = true;
rotor.add(axle);
const ring = new THREE.Mesh(
new THREE.TorusGeometry(3.35, 0.018, 4, 72),
new THREE.MeshStandardMaterial({
color: 0x31596a,
roughness: 0.5,
metalness: 0.45
})
);
ring.rotation.x = Math.PI / 2;
ring.position.y = -0.015;
rotor.add(ring);
// Small polygonal ruler ticks around the rotor.
const tickGeometry = new THREE.BoxGeometry(0.035, 0.045, 0.15);
const tickMaterial = new THREE.MeshStandardMaterial({
color: 0x527383,
roughness: 0.65,
metalness: 0.2
});
for (let i = 0; i < 24; i++) {
const angle = (i / 24) * Math.PI * 2;
const tick = new THREE.Mesh(tickGeometry, tickMaterial);
tick.position.set(
Math.cos(angle) * 3.48,
0,
-Math.sin(angle) * 3.48
);
tick.rotation.y = angle;
rotor.add(tick);
}
const masses = [];
const massMeshes = [];
const armGeometry = new THREE.CylinderGeometry(
0.065,
0.09,
MAX_RADIUS,
6
);
armGeometry.rotateZ(Math.PI / 2);
const railGeometry = new THREE.CylinderGeometry(
0.019,
0.019,
MAX_RADIUS,
6
);
railGeometry.rotateZ(Math.PI / 2);
for (let i = 0; i < 3; i++) {
const arm = new THREE.Group();
arm.rotation.y = (i / 3) * Math.PI * 2;
rotor.add(arm);
const beam = new THREE.Mesh(
armGeometry,
new THREE.MeshStandardMaterial({
color: 0x527180,
roughness: 0.48,
metalness: 0.58,
flatShading: true
})
);
beam.position.x = MAX_RADIUS / 2;
beam.castShadow = true;
arm.add(beam);
const rail = new THREE.Mesh(
railGeometry,
new THREE.MeshStandardMaterial({
color: 0x182d39,
roughness: 0.8,
metalness: 0.15
})
);
rail.position.x = MAX_RADIUS / 2;
rail.position.y = 0.025;
arm.add(rail);
const carrier = new THREE.Group();
carrier.position.x = INITIAL_RADII[i];
arm.add(carrier);
const stem = new THREE.Mesh(
new THREE.CylinderGeometry(0.065, 0.10, 0.31, 8),
new THREE.MeshStandardMaterial({
color: 0x77909b,
roughness: 0.42,
metalness: 0.6,
flatShading: true
})
);
stem.position.y = 0.17;
stem.castShadow = true;
carrier.add(stem);
const mesh = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.36, 0),
new THREE.MeshStandardMaterial({
color: colors[i],
emissive: colors[i],
emissiveIntensity: 0.055,
roughness: 0.44,
metalness: 0.18,
flatShading: true
})
);
mesh.position.y = 0.48;
mesh.castShadow = true;
mesh.receiveShadow = true;
mesh.userData.massIndex = i;
carrier.add(mesh);
massMeshes.push(mesh);
masses.push({
arm,
carrier,
mesh,
radius: INITIAL_RADII[i],
work: 0
});
}
// Mint arrow: conserved angular momentum.
const momentumArrow = new THREE.ArrowHelper(
new THREE.Vector3(0, 1, 0),
new THREE.Vector3(0, 1.78, 0),
1.65,
0x76f0bd,
0.30,
0.17
);
scene.add(momentumArrow);
// Yellow arrow: current angular speed, tangent to the rotor.
const speedArrow = new THREE.ArrowHelper(
new THREE.Vector3(1, 0, 0),
new THREE.Vector3(0, 0.72, 0.78),
1.0,
0xffd166,
0.27,
0.15
);
rotor.add(speedArrow);
// ---------------------------------------------------------------------
// Physics state and readouts
// ---------------------------------------------------------------------
function computeInertia() {
let total = FIXED_INERTIA;
for (const mass of masses) {
total += MASS * mass.radius * mass.radius;
}
return total;
}
let inertia = computeInertia();
let omega = INITIAL_OMEGA;
let angularMomentum = inertia * omega;
let referenceEnergy = 0.5 * inertia * omega * omega;
let totalWork = 0;
let lastWork = 0;
let paused = false;
let vectorsVisible = true;
const omegaValue = document.getElementById("omegaValue");
const rpmValue = document.getElementById("rpmValue");
const inertiaValue = document.getElementById("inertiaValue");
const momentumValue = document.getElementById("momentumValue");
const kineticValue = document.getElementById("kineticValue");
const workValue = document.getElementById("workValue");
const lastWorkValue = document.getElementById("lastWorkValue");
const lastWorkDirection = document.getElementById("lastWorkDirection");
const ledgerValue = document.getElementById("ledgerValue");
const referenceValue = document.getElementById("referenceValue");
const sliders = [
document.getElementById("radiusSlider0"),
document.getElementById("radiusSlider1"),
document.getElementById("radiusSlider2")
];
const radiusLabels = [
document.getElementById("radius0"),
document.getElementById("radius1"),
document.getElementById("radius2")
];
const massWorkLabels = [
document.getElementById("massWork0"),
document.getElementById("massWork1"),
document.getElementById("massWork2")
];
function signed(value, digits = 2) {
const sign = value > 0.0005 ? "+" : "";
return sign + value.toFixed(digits);
}
function updateSpeedArrow() {
const length = THREE.MathUtils.clamp(0.65 + omega * 0.42, 0.7, 2.35);
speedArrow.setLength(length, 0.27, 0.15);
}
function syncMassUI(index) {
const mass = masses[index];
const slider = sliders[index];
slider.value = mass.radius.toFixed(2);
radiusLabels[index].textContent = mass.radius.toFixed(2);
massWorkLabels[index].textContent = signed(mass.work, 1);
const percentage =
((mass.radius - MIN_RADIUS) / (MAX_RADIUS - MIN_RADIUS)) * 100;
slider.style.setProperty("--p", percentage + "%");
}
function updateReadouts() {
const kineticEnergy = 0.5 * inertia * omega * omega;
const ledger = kineticEnergy - totalWork;
omegaValue.textContent = omega.toFixed(3);
rpmValue.textContent = (omega * 60 / (2 * Math.PI)).toFixed(1) + " rpm";
inertiaValue.textContent = inertia.toFixed(2);
momentumValue.textContent = angularMomentum.toFixed(2);
kineticValue.textContent = kineticEnergy.toFixed(2);
workValue.textContent = signed(totalWork);
lastWorkValue.textContent = signed(lastWork);
ledgerValue.textContent = ledger.toFixed(2);
referenceValue.textContent = referenceEnergy.toFixed(2);
if (Math.abs(lastWork) < 0.0005) {
lastWorkDirection.textContent = "no motion yet";
lastWorkValue.style.color = "var(--muted)";
} else if (lastWork > 0) {
lastWorkDirection.textContent = "energy added inward";
lastWorkValue.style.color = "var(--mint)";
} else {
lastWorkDirection.textContent = "energy released outward";
lastWorkValue.style.color = "var(--orange)";
}
workValue.style.color =
totalWork >= 0 ? "var(--mint)" : "var(--orange)";
updateSpeedArrow();
}
function setMassRadius(index, requestedRadius) {
const newRadius = THREE.MathUtils.clamp(
Number(requestedRadius),
MIN_RADIUS,
MAX_RADIUS
);
const mass = masses[index];
if (Math.abs(newRadius - mass.radius) < 0.00001) return;
const oldInertia = inertia;
const oldKineticEnergy = 0.5 * oldInertia * omega * omega;
mass.radius = newRadius;
mass.carrier.position.x = newRadius;
const newInertia = computeInertia();
// L is conserved exactly. Only omega changes.
omega = angularMomentum / newInertia;
inertia = newInertia;
const newKineticEnergy = 0.5 * newInertia * omega * omega;
// Explicit radial work for a one-at-a-time quasi-static adjustment.
// This equals newKineticEnergy - oldKineticEnergy.
const adjustmentWork =
0.5 * angularMomentum * angularMomentum *
(1 / newInertia - 1 / oldInertia);
totalWork += adjustmentWork;
mass.work += adjustmentWork;
lastWork = adjustmentWork;
syncMassUI(index);
updateReadouts();
}
for (let i = 0; i < sliders.length; i++) {
sliders[i].addEventListener("input", (event) => {
setMassRadius(i, event.target.value);
});
syncMassUI(i);
}
// ---------------------------------------------------------------------
// Direct camera controls: orbit, zoom, and mass dragging
// ---------------------------------------------------------------------
const cameraTarget = new THREE.Vector3(0, 0.35, 0);
let cameraAzimuth = -0.68;
let cameraElevation = 0.67;
let cameraDistance = 10.2;
function updateCamera() {
const horizontal = cameraDistance * Math.cos(cameraElevation);
camera.position.set(
cameraTarget.x + horizontal * Math.sin(cameraAzimuth),
cameraTarget.y + cameraDistance * Math.sin(cameraElevation),
cameraTarget.z + horizontal * Math.cos(cameraAzimuth)
);
camera.lookAt(cameraTarget);
}
function resetCamera() {
cameraAzimuth = -0.68;
cameraElevation = 0.67;
cameraDistance = 10.2;
updateCamera();
}
updateCamera();
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const dragPlane = new THREE.Plane(new THREE.Vector3(0, 1, 0), 0);
let orbitPointerId = null;
let dragPointerId = null;
let draggedMassIndex = null;
let dragOffset = 0;
let lastPointerX = 0;
let lastPointerY = 0;
const status = document.getElementById("status");
function updatePointer(event) {
const rect = renderer.domElement.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
}
function projectedRadiusOnArm(index, worldPoint) {
const localPoint = masses[index].arm.worldToLocal(worldPoint.clone());
return localPoint.x;
}
renderer.domElement.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
updatePointer(event);
scene.updateMatrixWorld(true);
const hits = raycaster.intersectObjects(massMeshes, false);
if (hits.length) {
draggedMassIndex = hits[0].object.userData.massIndex;
dragPointerId = event.pointerId;
const intersection = new THREE.Vector3();
if (raycaster.ray.intersectPlane(dragPlane, intersection)) {
const hitRadius = projectedRadiusOnArm(
draggedMassIndex,
intersection
);
dragOffset = masses[draggedMassIndex].radius - hitRadius;
} else {
dragOffset = 0;
}
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.style.cursor = "grabbing";
status.classList.add("visible");
} else {
orbitPointerId = event.pointerId;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.style.cursor = "grabbing";
}
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (draggedMassIndex !== null &&
event.pointerId === dragPointerId) {
updatePointer(event);
const intersection = new THREE.Vector3();
if (raycaster.ray.intersectPlane(dragPlane, intersection)) {
const hitRadius = projectedRadiusOnArm(
draggedMassIndex,
intersection
);
setMassRadius(
draggedMassIndex,
hitRadius + dragOffset
);
}
} else if (orbitPointerId !== null &&
event.pointerId === orbitPointerId) {
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
cameraAzimuth -= dx * 0.006;
cameraElevation = THREE.MathUtils.clamp(
cameraElevation + dy * 0.005,
0.16,
1.38
);
lastPointerX = event.clientX;
lastPointerY = event.clientY;
updateCamera();
}
});
function releasePointer(event) {
if (event.pointerId === dragPointerId) {
draggedMassIndex = null;
dragPointerId = null;
status.classList.remove("visible");
}
if (event.pointerId === orbitPointerId) {
orbitPointerId = null;
}
renderer.domElement.style.cursor = "grab";
}
renderer.domElement.addEventListener("pointerup", releasePointer);
renderer.domElement.addEventListener("pointercancel", releasePointer);
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
renderer.domElement.addEventListener("wheel", (event) => {
event.preventDefault();
cameraDistance = THREE.MathUtils.clamp(
cameraDistance + event.deltaY * 0.008,
5.8,
17
);
updateCamera();
}, { passive: false });
// ---------------------------------------------------------------------
// Buttons and keyboard shortcuts
// ---------------------------------------------------------------------
const pauseButton = document.getElementById("pauseButton");
const resetButton = document.getElementById("resetButton");
const vectorsButton = document.getElementById("vectorsButton");
function setPaused(value) {
paused = value;
pauseButton.textContent = paused ? "Resume spin" : "Pause spin";
}
pauseButton.addEventListener("click", () => {
setPaused(!paused);
});
vectorsButton.addEventListener("click", () => {
vectorsVisible = !vectorsVisible;
momentumArrow.visible = vectorsVisible;
speedArrow.visible = vectorsVisible;
vectorsButton.textContent =
vectorsVisible ? "Hide vectors" : "Show vectors";
});
function resetExperiment() {
for (let i = 0; i < masses.length; i++) {
masses[i].radius = INITIAL_RADII[i];
masses[i].carrier.position.x = INITIAL_RADII[i];
masses[i].work = 0;
syncMassUI(i);
}
inertia = computeInertia();
omega = INITIAL_OMEGA;
angularMomentum = inertia * omega;
referenceEnergy = 0.5 * inertia * omega * omega;
totalWork = 0;
lastWork = 0;
rotor.rotation.y = 0;
setPaused(false);
updateReadouts();
}
resetButton.addEventListener("click", resetExperiment);
window.addEventListener("keydown", (event) => {
const tag = event.target.tagName;
if (tag === "INPUT" || tag === "TEXTAREA") return;
if (event.code === "Space") {
event.preventDefault();
setPaused(!paused);
}
if (event.key.toLowerCase() === "r") {
resetExperiment();
}
});
// ---------------------------------------------------------------------
// Resize and render loop
// ---------------------------------------------------------------------
function resize() {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
}
window.addEventListener("resize", resize);
let previousTime = performance.now();
renderer.setAnimationLoop((time) => {
const delta = Math.min((time - previousTime) / 1000, 0.05);
previousTime = time;
if (!paused && draggedMassIndex === null) {
rotor.rotation.y += omega * delta;
}
renderer.render(scene, camera);
});
updateReadouts();
</script>
</body>
</html>
```