Prompt specification
Build or customize the Three.js 3D scene "Spacecraft reaction-wheel exhibit" 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: Spacecraft reaction-wheel exhibit
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/spacecraft-reaction-wheel-exhibit-x1363
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows angular momentum and rotational inertia. Compose a simplified spacecraft body and internal wheel rotating oppositely; illustrate angular-momentum exchange without claiming a complete realistic attitude-control simulation. Voxel style.
Working Three.js Starter (spacecraft-reaction-wheel-exhibit-x1363):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Voxel Angular Momentum Lab</title>
<style>
:root {
--bg: #050914;
--panel: rgba(9, 18, 34, 0.82);
--border: rgba(140, 190, 230, 0.22);
--text: #e7f4ff;
--muted: #91a9bd;
--cyan: #48dcff;
--orange: #ffb547;
--green: #78f0ae;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
color: var(--text);
font-family:
"SFMono-Regular", Consolas, "Liberation Mono", Menlo, monospace;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
#hud {
position: fixed;
inset: 0;
pointer-events: none;
z-index: 2;
}
.panel {
pointer-events: auto;
background: var(--panel);
border: 1px solid var(--border);
border-radius: 12px;
box-shadow:
0 16px 50px rgba(0, 0, 0, 0.35),
inset 0 1px rgba(255, 255, 255, 0.04);
backdrop-filter: blur(12px);
}
#intro {
position: absolute;
top: 20px;
left: 20px;
width: min(470px, calc(100vw - 40px));
padding: 17px 19px;
pointer-events: none;
}
#intro .eyebrow {
color: var(--green);
font-size: 10px;
letter-spacing: 0.22em;
margin-bottom: 8px;
}
#intro h1 {
margin: 0;
font-size: clamp(19px, 2.6vw, 29px);
line-height: 1.05;
letter-spacing: 0.035em;
}
#intro p {
margin: 10px 0 0;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
#controls {
position: absolute;
left: 20px;
bottom: 56px;
width: 350px;
padding: 16px;
}
.control-title {
color: var(--muted);
font-size: 10px;
letter-spacing: 0.14em;
text-transform: uppercase;
margin-bottom: 8px;
}
.control-row {
margin: 13px 0;
}
.control-label {
display: flex;
justify-content: space-between;
align-items: baseline;
gap: 12px;
font-size: 11px;
margin-bottom: 7px;
}
.control-label output {
color: var(--orange);
font-weight: bold;
white-space: nowrap;
}
input[type="range"] {
width: 100%;
margin: 0;
accent-color: var(--orange);
cursor: pointer;
}
#inertia {
accent-color: var(--cyan);
}
.buttons {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 7px;
margin-top: 14px;
}
button {
appearance: none;
border: 1px solid rgba(150, 195, 225, 0.25);
border-radius: 6px;
background: rgba(120, 170, 205, 0.08);
color: var(--text);
padding: 8px 4px;
font: 10px/1.2 inherit;
letter-spacing: 0.04em;
cursor: pointer;
}
button:hover {
background: rgba(120, 170, 205, 0.18);
border-color: rgba(150, 195, 225, 0.48);
}
button:active {
transform: translateY(1px);
}
#telemetry {
position: absolute;
top: 20px;
right: 20px;
width: 320px;
padding: 16px;
}
.telemetry-head {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 12px;
}
.telemetry-head strong {
font-size: 11px;
letter-spacing: 0.13em;
}
#status {
color: var(--green);
font-size: 9px;
letter-spacing: 0.08em;
border: 1px solid rgba(120, 240, 174, 0.25);
border-radius: 999px;
padding: 4px 7px;
}
.stats {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
.stat {
padding: 9px 10px;
border-radius: 7px;
background: rgba(255, 255, 255, 0.035);
border: 1px solid rgba(255, 255, 255, 0.045);
}
.stat .name {
color: var(--muted);
font-size: 9px;
letter-spacing: 0.08em;
margin-bottom: 5px;
}
.stat .value {
font-size: 15px;
font-weight: bold;
white-space: nowrap;
}
.stat .unit {
color: var(--muted);
font-size: 9px;
font-weight: normal;
margin-left: 3px;
}
.body-color {
color: var(--cyan);
}
.wheel-color {
color: var(--orange);
}
.balance {
margin-top: 13px;
}
.balance-row {
display: grid;
grid-template-columns: 44px 1fr 58px;
align-items: center;
gap: 8px;
margin: 8px 0;
font-size: 9px;
}
.balance-label {
color: var(--muted);
}
.track {
position: relative;
height: 8px;
border-radius: 2px;
background:
linear-gradient(
90deg,
rgba(255, 255, 255, 0.025),
rgba(255, 255, 255, 0.08)
);
overflow: hidden;
}
.track::after {
content: "";
position: absolute;
left: 50%;
top: 0;
bottom: 0;
width: 1px;
background: rgba(255, 255, 255, 0.42);
}
.bar {
position: absolute;
left: 50%;
top: 0;
bottom: 0;
width: 0;
transition: width 80ms linear;
}
#bodyBar {
background: var(--cyan);
}
#wheelBar {
background: var(--orange);
}
.balance-number {
text-align: right;
color: var(--text);
}
.formula {
margin-top: 12px;
padding: 9px 10px;
border-radius: 6px;
background: rgba(0, 0, 0, 0.18);
color: #b9d0df;
font-size: 10px;
line-height: 1.55;
}
#note {
position: absolute;
left: 50%;
bottom: 14px;
transform: translateX(-50%);
width: min(850px, calc(100vw - 32px));
padding: 9px 13px;
color: #a9bbc9;
text-align: center;
font-size: 10px;
line-height: 1.45;
pointer-events: none;
background: rgba(5, 9, 20, 0.7);
border: 1px solid rgba(140, 190, 230, 0.13);
border-radius: 8px;
}
.world-label {
position: absolute;
transform: translate(-50%, -50%);
padding: 6px 8px;
border-radius: 6px;
font-size: 9px;
line-height: 1.45;
white-space: nowrap;
pointer-events: none;
border: 1px solid;
background: rgba(5, 9, 20, 0.8);
box-shadow: 0 8px 25px rgba(0, 0, 0, 0.3);
}
#wheelTag {
color: var(--orange);
border-color: rgba(255, 181, 71, 0.45);
}
#bodyTag {
color: var(--cyan);
border-color: rgba(72, 220, 255, 0.45);
}
.orbit-hint {
position: absolute;
right: 22px;
bottom: 24px;
color: rgba(180, 210, 230, 0.55);
font-size: 9px;
text-align: right;
line-height: 1.55;
pointer-events: none;
}
@media (max-width: 900px) {
#intro {
width: calc(100vw - 40px);
}
#telemetry {
top: auto;
bottom: 178px;
right: 12px;
width: 285px;
}
#controls {
left: 12px;
bottom: 52px;
width: min(350px, calc(100vw - 24px));
}
#note {
font-size: 9px;
}
.orbit-hint {
display: none;
}
}
@media (max-width: 560px) {
#intro {
top: 10px;
left: 10px;
width: calc(100vw - 20px);
}
#intro p {
display: none;
}
#telemetry {
right: 10px;
bottom: 178px;
width: 250px;
padding: 12px;
}
.stats {
gap: 5px;
}
.stat {
padding: 7px;
}
.stat .value {
font-size: 13px;
}
.formula {
display: none;
}
#controls {
left: 10px;
width: calc(100vw - 20px);
bottom: 54px;
}
#note {
bottom: 8px;
width: calc(100vw - 16px);
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive voxel spacecraft angular momentum demonstration"></canvas>
<div id="hud">
<section id="intro" class="panel">
<div class="eyebrow">VOXEL DYNAMICS / SINGLE-AXIS DEMO</div>
<h1>ANGULAR MOMENTUM<br>COUNTER-SPIN</h1>
<p>
The motor accelerates an internal flywheel. Its angular momentum is exchanged
with the spacecraft body, producing reaction rotation about the same axis.
</p>
</section>
<section id="controls" class="panel">
<div class="control-title">Motor & inertia controls</div>
<div class="control-row">
<div class="control-label">
<span>Flywheel target speed</span>
<output id="targetValue">+14.0 rad/s</output>
</div>
<input
id="flywheel"
type="range"
min="-24"
max="24"
step="0.5"
value="14"
aria-label="Flywheel target angular velocity"
>
</div>
<div class="control-row">
<div class="control-label">
<span>Spacecraft inertia I<sub>b</sub></span>
<output id="inertiaValue">6.4 kg·m²</output>
</div>
<input
id="inertia"
type="range"
min="3"
max="12"
step="0.1"
value="6.4"
aria-label="Spacecraft rotational inertia"
>
</div>
<div class="buttons">
<button id="pauseButton" type="button">PAUSE</button>
<button id="brakeButton" type="button">BRAKE</button>
<button id="reverseButton" type="button">REVERSE</button>
<button id="resetButton" type="button">RESET</button>
</div>
</section>
<section id="telemetry" class="panel">
<div class="telemetry-head">
<strong>LIVE TELEMETRY</strong>
<span id="status">COUNTER-ROTATING</span>
</div>
<div class="stats">
<div class="stat">
<div class="name">WHEEL ω</div>
<div class="value wheel-color">
<span id="wheelOmega">+14.00</span>
<span class="unit">rad/s</span>
</div>
</div>
<div class="stat">
<div class="name">BODY ω</div>
<div class="value body-color">
<span id="bodyOmega">-0.92</span>
<span class="unit">rad/s</span>
</div>
</div>
<div class="stat">
<div class="name">WHEEL L = Iω</div>
<div class="value wheel-color">
<span id="wheelL">+5.88</span>
<span class="unit">kg·m²/s</span>
</div>
</div>
<div class="stat">
<div class="name">BODY L = Iω</div>
<div class="value body-color">
<span id="bodyL">-5.88</span>
<span class="unit">kg·m²/s</span>
</div>
</div>
<div class="stat">
<div class="name">WHEEL INERTIA</div>
<div class="value">
<span id="wheelInertia">0.42</span>
<span class="unit">kg·m²</span>
</div>
</div>
<div class="stat">
<div class="name">TOTAL L</div>
<div class="value">
<span id="totalL">0.00</span>
<span class="unit">kg·m²/s</span>
</div>
</div>
</div>
<div class="balance">
<div class="balance-row">
<span class="balance-label body-color">BODY</span>
<div class="track"><i id="bodyBar" class="bar"></i></div>
<span id="bodyBarValue" class="balance-number">-5.88</span>
</div>
<div class="balance-row">
<span class="balance-label wheel-color">WHEEL</span>
<div class="track"><i id="wheelBar" class="bar"></i></div>
<span id="wheelBarValue" class="balance-number">+5.88</span>
</div>
</div>
<div class="formula">
<strong>L = Iω</strong><br>
ω<sub>body</sub> =
(L<sub>total</sub> − I<sub>wheel</sub>ω<sub>wheel</sub>) / I<sub>body</sub><br>
Equal and opposite momenta are exchanged internally.
</div>
</section>
<div id="wheelTag" class="world-label">
L<sub>wheel</sub><br><strong>+5.88</strong>
</div>
<div id="bodyTag" class="world-label">
L<sub>body</sub><br><strong>-5.88</strong>
</div>
<div class="orbit-hint">
DRAG TO ORBIT<br>
SCROLL TO ZOOM<br>
DOUBLE-CLICK TO RESET VIEW
</div>
<div id="note">
Illustrative angular-momentum exchange only—not a complete realistic attitude-control
simulation. Assumes one principal axis, no external torque, lumped rigid-body
inertias, and no gyroscopic cross-coupling, structural flex, or actuator-limit model.
</div>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// DOM and renderer
// -------------------------------------------------------------------------
const canvas = document.querySelector("#scene");
const ui = {
flywheel: document.querySelector("#flywheel"),
inertia: document.querySelector("#inertia"),
targetValue: document.querySelector("#targetValue"),
inertiaValue: document.querySelector("#inertiaValue"),
pauseButton: document.querySelector("#pauseButton"),
brakeButton: document.querySelector("#brakeButton"),
reverseButton: document.querySelector("#reverseButton"),
resetButton: document.querySelector("#resetButton"),
wheelOmega: document.querySelector("#wheelOmega"),
bodyOmega: document.querySelector("#bodyOmega"),
wheelL: document.querySelector("#wheelL"),
bodyL: document.querySelector("#bodyL"),
wheelInertia: document.querySelector("#wheelInertia"),
totalL: document.querySelector("#totalL"),
bodyBar: document.querySelector("#bodyBar"),
wheelBar: document.querySelector("#wheelBar"),
bodyBarValue: document.querySelector("#bodyBarValue"),
wheelBarValue: document.querySelector("#wheelBarValue"),
status: document.querySelector("#status"),
wheelTag: document.querySelector("#wheelTag"),
bodyTag: document.querySelector("#bodyTag")
};
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.setSize(window.innerWidth, window.innerHeight, false);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x050914);
scene.fog = new THREE.FogExp2(0x050914, 0.018);
const camera = new THREE.PerspectiveCamera(
43,
window.innerWidth / window.innerHeight,
0.1,
120
);
const cameraTarget = new THREE.Vector3(0, 0, 0);
const cameraState = {
theta: 0.68,
phi: 1.02,
radius: 11.2
};
function updateCamera() {
const { theta, phi, radius } = cameraState;
const sinPhi = Math.sin(phi);
camera.position.set(
cameraTarget.x + radius * sinPhi * Math.sin(theta),
cameraTarget.y + radius * Math.cos(phi),
cameraTarget.z + radius * sinPhi * Math.cos(theta)
);
camera.lookAt(cameraTarget);
}
updateCamera();
// -------------------------------------------------------------------------
// Lighting
// -------------------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0x9dc9ff, 0x111522, 1.45));
const keyLight = new THREE.DirectionalLight(0xffffff, 3.3);
keyLight.position.set(6, 9, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -8;
keyLight.shadow.camera.right = 8;
keyLight.shadow.camera.top = 8;
keyLight.shadow.camera.bottom = -8;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 30;
keyLight.shadow.bias = -0.0007;
scene.add(keyLight);
const cyanLight = new THREE.PointLight(0x25c8ff, 15, 16, 2);
cyanLight.position.set(-5, 1.5, 4);
scene.add(cyanLight);
const orangeLight = new THREE.PointLight(0xff8b32, 13, 15, 2);
orangeLight.position.set(5, -1, -3);
scene.add(orangeLight);
// -------------------------------------------------------------------------
// Reusable voxel geometry and materials
// -------------------------------------------------------------------------
const cubeGeometry = new THREE.BoxGeometry(1, 1, 1);
const material = (color, options = {}) =>
new THREE.MeshStandardMaterial({
color,
roughness: 0.72,
metalness: 0.08,
flatShading: true,
...options
});
const materials = {
body: material(0x71869b, { metalness: 0.2 }),
bodyLight: material(0x9aafc0, { metalness: 0.18 }),
bodyDark: material(0x2e4052, { metalness: 0.3 }),
bodyBlue: material(0x496783, { metalness: 0.22 }),
trim: material(0xd8e4eb, { metalness: 0.15 }),
orange: material(0xff813c, {
emissive: 0x7a2108,
emissiveIntensity: 0.28,
metalness: 0.18
}),
gold: material(0xf0b33e, {
emissive: 0x5b3506,
emissiveIntensity: 0.24,
metalness: 0.48,
roughness: 0.48
}),
goldDark: material(0x9a6919, { metalness: 0.55 }),
red: material(0xff4d38, {
emissive: 0x8f160d,
emissiveIntensity: 0.5
}),
axle: material(0xb9c9d4, { metalness: 0.75, roughness: 0.34 }),
solar: material(0x123d70, {
emissive: 0x061a35,
emissiveIntensity: 0.5,
metalness: 0.5,
roughness: 0.36
}),
solarBright: material(0x216db2, {
emissive: 0x062b58,
emissiveIntensity: 0.7,
metalness: 0.45,
roughness: 0.3
})
};
function addBox(
parent,
size,
position,
boxMaterial,
rotation = { x: 0, y: 0, z: 0 }
) {
const mesh = new THREE.Mesh(cubeGeometry, boxMaterial);
mesh.scale.set(size[0], size[1], size[2]);
mesh.position.set(position[0], position[1], position[2]);
mesh.rotation.set(rotation.x, rotation.y, rotation.z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// -------------------------------------------------------------------------
// Voxel star field
// -------------------------------------------------------------------------
const starCount = 240;
const starGeometry = new THREE.BoxGeometry(0.045, 0.045, 0.045);
const starMaterial = new THREE.MeshBasicMaterial({ color: 0xffffff });
const stars = new THREE.InstancedMesh(starGeometry, starMaterial, starCount);
const starDummy = new THREE.Object3D();
const starColor = new THREE.Color();
for (let i = 0; i < starCount; i++) {
const radius = 18 + Math.random() * 42;
const azimuth = Math.random() * Math.PI * 2;
const polar = Math.acos(THREE.MathUtils.clamp(
2 * Math.random() - 1,
-1,
1
));
starDummy.position.set(
radius * Math.sin(polar) * Math.cos(azimuth),
radius * Math.cos(polar),
radius * Math.sin(polar) * Math.sin(azimuth)
);
const size = 0.35 + Math.random() * 1.8;
starDummy.scale.setScalar(size);
starDummy.rotation.set(
Math.random() * Math.PI,
Math.random() * Math.PI,
Math.random() * Math.PI
);
starDummy.updateMatrix();
stars.setMatrixAt(i, starDummy.matrix);
starColor.setHSL(0.52 + Math.random() * 0.12, 0.25, 0.72 + Math.random() * 0.25);
stars.setColorAt(i, starColor);
}
stars.instanceMatrix.needsUpdate = true;
if (stars.instanceColor) stars.instanceColor.needsUpdate = true;
stars.frustumCulled = false;
scene.add(stars);
// -------------------------------------------------------------------------
// Spacecraft: open, voxel-built cutaway body
// -------------------------------------------------------------------------
const bodyRoot = new THREE.Group();
scene.add(bodyRoot);
// Bottom deck.
for (let x = 0; x < 6; x++) {
for (let z = 0; z < 6; z++) {
const isAccent = (x + z) % 7 === 0;
addBox(
bodyRoot,
[0.5, 0.32, 0.5],
[(x - 2.5) * 0.52, -1.32, (z - 2.5) * 0.52],
isAccent ? materials.bodyBlue : materials.bodyDark
);
}
}
// Rear wall, made from individual blocks.
for (let x = 0; x < 6; x++) {
for (let y = 0; y < 5; y++) {
const isAccent = (x + y) % 8 === 0;
addBox(
bodyRoot,
[0.5, 0.48, 0.3],
[(x - 2.5) * 0.52, (y - 2) * 0.48, -1.55],
isAccent ? materials.bodyLight : materials.body
);
}
}
// Corner columns.
for (const x of [-1.55, 1.55]) {
for (const z of [-1.55, 1.55]) {
addBox(
bodyRoot,
[0.34, 2.75, 0.34],
[x, 0, z],
materials.bodyDark
);
addBox(
bodyRoot,
[0.45, 0.28, 0.45],
[x, 1.33, z],
materials.orange
);
}
}
// Top rails; the front remains open for a cutaway view.
for (const z of [-1.55, 1.55]) {
addBox(bodyRoot, [3.4, 0.24, 0.28], [0, 1.3, z], materials.bodyLight);
}
for (const x of [-1.55, 1.55]) {
addBox(bodyRoot, [0.28, 0.24, 3.4], [x, 1.3, 0], materials.bodyLight);
}
// Side ribs and front cutaway markers.
for (const x of [-1.68, 1.68]) {
for (const z of [-0.9, 0, 0.9]) {
addBox(
bodyRoot,
[0.18, 1.5, 0.32],
[x, -0.05, z],
materials.bodyBlue
);
}
}
for (const x of [-1.55, 1.55]) {
addBox(bodyRoot, [0.38, 0.36, 0.38], [x, -0.8, 1.55], materials.orange);
}
// Static flywheel axle.
addBox(bodyRoot, [0.15, 3.0, 0.15], [0, 0, 0], materials.axle);
addBox(bodyRoot, [0.5, 0.16, 0.5], [0, 1.5, 0], materials.bodyLight);
addBox(bodyRoot, [0.5, 0.16, 0.5], [0, -1.5, 0], materials.bodyLight);
// Voxel solar-panel booms.
for (const side of [-1, 1]) {
addBox(
bodyRoot,
[1.2, 0.14, 0.18],
[side * 2.15, -0.28, 0],
materials.axle
);
// Four columns by five rows of square solar cells.
for (let column = 0; column < 4; column++) {
for (let row = 0; row < 5; row++) {
const bright = (column * 3 + row) % 6 === 0;
addBox(
bodyRoot,
[0.43, 0.08, 0.43],
[
side * (2.68 + column * 0.48),
-0.3,
(row - 2) * 0.48
],
bright ? materials.solarBright : materials.solar
);
}
}
// Outer frame blocks.
for (const z of [-1.22, 1.22]) {
addBox(
bodyRoot,
[2.0, 0.12, 0.08],
[side * 3.38, -0.3, z],
materials.bodyBlue
);
}
}
// -------------------------------------------------------------------------
// Internal flywheel: stacked voxel rings, spokes, hub, and rotation marker
// -------------------------------------------------------------------------
const wheelSpin = new THREE.Group();
bodyRoot.add(wheelSpin);
const wheelRadius = 1.17;
for (const side of [-1, 1]) {
const blockCount = 18;
for (let i = 0; i < blockCount; i++) {
const angle = (i / blockCount) * Math.PI * 2;
const isMarker = side === 1 && i === 0;
addBox(
wheelSpin,
[0.34, 0.24, 0.3],
[
Math.cos(angle) * wheelRadius,
side * 0.28,
Math.sin(angle) * wheelRadius
],
isMarker ? materials.red : materials.gold,
{ y: -angle }
);
}
}
// Four radial spokes.
for (let i = 0; i < 4; i++) {
const angle = i * Math.PI * 0.5;
addBox(
wheelSpin,
[2.35, 0.11, 0.13],
[
Math.cos(angle) * 1.08,
0,
Math.sin(angle) * 1.08
],
materials.goldDark,
{ y: -angle }
);
}
// Hub made from a central cube and smaller corner blocks.
addBox(wheelSpin, [0.72, 0.72, 0.72], [0, 0, 0], materials.gold);
addBox(wheelSpin, [0.83, 0.16, 0.83], [0, 0.39, 0], materials.goldDark);
for (const x of [-0.28, 0.28]) {
for (const z of [-0.28, 0.28]) {
addBox(
wheelSpin,
[0.2, 0.22, 0.2],
[x, 0.43, z],
materials.orange
);
}
}
// A bright index block makes the wheel's rotation easy to see.
addBox(
wheelSpin,
[0.24, 0.34, 0.48],
[wheelRadius, 0.48, 0],
materials.red,
{ y: Math.PI * 0.5 }
);
// -------------------------------------------------------------------------
// Voxel angular-momentum arrows
// -------------------------------------------------------------------------
function createVoxelArrow(color) {
const group = new THREE.Group();
const arrowMaterial = material(color, {
emissive: color,
emissiveIntensity: 0.38,
metalness: 0.18,
roughness: 0.48
});
addBox(group, [0.13, 0.72, 0.13], [0, 0.36, 0], arrowMaterial);
addBox(group, [0.46, 0.12, 0.46], [0, 0.76, 0], arrowMaterial);
addBox(group, [0.32, 0.12, 0.32], [0, 0.87, 0], arrowMaterial);
addBox(group, [0.17, 0.12, 0.17], [0, 0.98, 0], arrowMaterial);
scene.add(group);
return group;
}
const arrowBaseY = -1.65;
const bodyArrow = createVoxelArrow(0x48dcff);
const wheelArrow = createVoxelArrow(0xffb547);
bodyArrow.position.set(-1.95, arrowBaseY, 1.12);
wheelArrow.position.set(1.95, arrowBaseY, 1.12);
function updateArrow(arrow, momentum) {
const magnitude = Math.abs(momentum);
if (magnitude < 0.001) {
arrow.visible = false;
return 0;
}
arrow.visible = true;
const length = Math.min(2.85, Math.max(0.24, magnitude * 0.32));
arrow.scale.set(1, length, 1);
arrow.rotation.z = momentum >= 0 ? 0 : Math.PI;
return momentum >= 0 ? length : -length;
}
// -------------------------------------------------------------------------
// Simplified angular-momentum model
// -------------------------------------------------------------------------
const state = {
wheelInertia: 0.42,
bodyInertia: 6.4,
wheelOmega: 14,
targetOmega: 14,
bodyOmega: -0.92,
totalMomentum: 0,
bodyAngle: 0,
relativeWheelAngle: 0,
paused: false,
maxMotorAcceleration: 2.1
};
function signed(value, decimals = 2) {
const rounded = Math.abs(value) < 0.0005 ? 0 : value;
return `${rounded >= 0 ? "+" : ""}${rounded.toFixed(decimals)}`;
}
function syncBodyOmega() {
state.bodyOmega =
(state.totalMomentum - state.wheelInertia * state.wheelOmega) /
state.bodyInertia;
}
function resetSimulation() {
state.wheelOmega = 14;
state.targetOmega = 14;
state.totalMomentum = 0;
state.bodyInertia = 6.4;
state.bodyAngle = 0;
state.relativeWheelAngle = 0;
state.paused = false;
syncBodyOmega();
ui.flywheel.value = "14";
ui.inertia.value = "6.4";
ui.pauseButton.textContent = "PAUSE";
ui.status.textContent = "COUNTER-ROTATING";
ui.status.style.color = "var(--green)";
}
syncBodyOmega();
// -------------------------------------------------------------------------
// UI events
// -------------------------------------------------------------------------
ui.flywheel.addEventListener("input", () => {
state.targetOmega = Number(ui.flywheel.value);
ui.targetValue.textContent = `${signed(state.targetOmega, 1)} rad/s`;
});
ui.inertia.addEventListener("input", () => {
state.bodyInertia = Number(ui.inertia.value);
syncBodyOmega();
ui.inertiaValue.textContent =
`${state.bodyInertia.toFixed(1)} kg·m²`;
});
ui.pauseButton.addEventListener("click", () => {
state.paused = !state.paused;
ui.pauseButton.textContent = state.paused ? "RESUME" : "PAUSE";
ui.status.textContent = state.paused ? "PAUSED" : "COUNTER-ROTATING";
ui.status.style.color = state.paused ? "var(--orange)" : "var(--green)";
});
ui.brakeButton.addEventListener("click", () => {
state.targetOmega = 0;
ui.flywheel.value = "0";
ui.targetValue.textContent = "0.0 rad/s";
});
ui.reverseButton.addEventListener("click", () => {
state.targetOmega = state.targetOmega === 0 ? 14 : -state.targetOmega;
ui.flywheel.value = String(state.targetOmega);
ui.targetValue.textContent = `${signed(state.targetOmega, 1)} rad/s`;
});
ui.resetButton.addEventListener("click", resetSimulation);
// -------------------------------------------------------------------------
// Custom orbit camera controls; no external control modules are used
// -------------------------------------------------------------------------
let dragging = false;
let previousX = 0;
let previousY = 0;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - previousX;
const deltaY = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
cameraState.theta -= deltaX * 0.007;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi + deltaY * 0.006,
0.3,
1.42
);
updateCamera();
});
function stopDragging(event) {
dragging = false;
if (event.pointerId !== undefined && canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", stopDragging);
canvas.addEventListener("pointercancel", stopDragging);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius + event.deltaY * 0.008,
7,
18
);
updateCamera();
},
{ passive: false }
);
canvas.addEventListener("dblclick", () => {
cameraState.theta = 0.68;
cameraState.phi = 1.02;
cameraState.radius = 11.2;
updateCamera();
});
// -------------------------------------------------------------------------
// HTML label projection
// -------------------------------------------------------------------------
const projected = new THREE.Vector3();
function positionWorldLabel(element, x, y, z) {
projected.set(x, y, z).project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
Math.abs(projected.x) < 1.25 &&
Math.abs(projected.y) < 1.25;
element.style.display = visible ? "block" : "none";
if (!visible) return;
element.style.left =
`${(projected.x * 0.5 + 0.5) * window.innerWidth}px`;
element.style.top =
`${(-projected.y * 0.5 + 0.5) * window.innerHeight}px`;
}
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
function animate() {
const delta = Math.min(clock.getDelta(), 0.05);
if (!state.paused) {
const difference = state.targetOmega - state.wheelOmega;
const maximumChange = state.maxMotorAcceleration * delta;
state.wheelOmega += THREE.MathUtils.clamp(
difference,
-maximumChange,
maximumChange
);
// In this isolated one-axis model, changing the wheel momentum
// requires an equal and opposite change in body momentum.
syncBodyOmega();
state.bodyAngle += state.bodyOmega * delta;
// The wheel is a child of the rotating body. Its relative angle is
// therefore wheel absolute angle minus body absolute angle.
state.relativeWheelAngle +=
(state.wheelOmega - state.bodyOmega) * delta;
}
bodyRoot.rotation.y = state.bodyAngle;
wheelSpin.rotation.y = state.relativeWheelAngle;
const wheelMomentum = state.wheelInertia * state.wheelOmega;
const bodyMomentum = state.bodyInertia * state.bodyOmega;
const totalMomentum = wheelMomentum + bodyMomentum;
const bodyArrowLength = updateArrow(bodyArrow, bodyMomentum);
const wheelArrowLength = updateArrow(wheelArrow, wheelMomentum);
// Telemetry.
ui.wheelOmega.textContent = signed(state.wheelOmega);
ui.bodyOmega.textContent = signed(state.bodyOmega);
ui.wheelL.textContent = signed(wheelMomentum);
ui.bodyL.textContent = signed(bodyMomentum);
ui.totalL.textContent = signed(totalMomentum);
ui.wheelInertia.textContent = state.wheelInertia.toFixed(2);
const maximumMomentum = 12;
const bodyBarWidth = Math.min(50, (Math.abs(bodyMomentum) / maximumMomentum) * 50);
const wheelBarWidth = Math.min(50, (Math.abs(wheelMomentum) / maximumMomentum) * 50);
ui.bodyBar.style.width = `${bodyBarWidth}%`;
ui.bodyBar.style.transform =
bodyMomentum < 0 ? "translateX(-100%)" : "translateX(0)";
ui.wheelBar.style.width = `${wheelBarWidth}%`;
ui.wheelBar.style.transform =
wheelMomentum < 0 ? "translateX(-100%)" : "translateX(0)";
ui.bodyBarValue.textContent = signed(bodyMomentum);
ui.wheelBarValue.textContent = signed(wheelMomentum);
// Project the momentum-arrow tips into screen space.
positionWorldLabel(
ui.bodyTag,
bodyArrow.position.x,
arrowBaseY + bodyArrowLength,
bodyArrow.position.z
);
positionWorldLabel(
ui.wheelTag,
wheelArrow.position.x,
arrowBaseY + wheelArrowLength,
wheelArrow.position.z
);
ui.bodyTag.querySelector("strong").textContent =
signed(bodyMomentum);
ui.wheelTag.querySelector("strong").textContent =
signed(wheelMomentum);
renderer.render(scene, camera);
}
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight, false);
});
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```