Prompt specification
Build or customize the Three.js 3D scene "Gliding puck with force-arrow stand" 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: Gliding puck with force-arrow stand
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/gliding-puck-with-force-arrow-stand-x1401
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows inertia and motion under applied forces. Model a puck on a marked horizontal track with independent velocity and force arrows; keep constant velocity when the displayed net horizontal force is zero. Low-poly.
Working Three.js Starter (gliding-puck-with-force-arrow-stand-x1401):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Inertia & Applied Force Lab</title>
<style>
:root {
--bg: #07101d;
--panel: rgba(10, 19, 34, 0.86);
--panel-border: rgba(153, 188, 220, 0.17);
--text: #eef7ff;
--muted: #91a7bd;
--cyan: #5ee7f7;
--orange: #ffad55;
--purple: #a78bfa;
--green: #66e3a4;
--red: #ff7185;
}
* {
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;
}
button,
input {
font: inherit;
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
outline: none;
}
#hud {
position: fixed;
inset: 0;
z-index: 2;
pointer-events: none;
}
.panel {
pointer-events: auto;
background: var(--panel);
border: 1px solid var(--panel-border);
border-radius: 18px;
box-shadow: 0 18px 55px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(16px);
-webkit-backdrop-filter: blur(16px);
}
#title-panel {
position: absolute;
top: 22px;
left: 22px;
width: min(440px, calc(100vw - 44px));
padding: 19px 21px 18px;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 8px;
color: var(--cyan);
font-size: 0.72rem;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 19px;
height: 2px;
background: var(--cyan);
border-radius: 2px;
box-shadow: 0 0 12px var(--cyan);
}
h1 {
margin: 0;
font-size: clamp(1.55rem, 3vw, 2.3rem);
line-height: 1.04;
letter-spacing: -0.04em;
}
.subtitle {
margin: 10px 0 0;
max-width: 390px;
color: var(--muted);
font-size: 0.9rem;
line-height: 1.45;
}
#metrics {
position: absolute;
top: 22px;
right: 22px;
width: 286px;
padding: 17px;
}
.metric-label {
color: var(--muted);
font-size: 0.69rem;
font-weight: 750;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.metric-main {
display: flex;
align-items: flex-end;
justify-content: space-between;
gap: 14px;
margin-top: 5px;
}
#net-force {
font-size: 2rem;
font-weight: 850;
letter-spacing: -0.045em;
line-height: 1;
color: var(--orange);
}
.badge {
padding: 5px 8px;
border: 1px solid currentColor;
border-radius: 999px;
font-size: 0.64rem;
font-weight: 850;
letter-spacing: 0.08em;
white-space: nowrap;
}
.badge.coasting {
color: var(--green);
background: rgba(102, 227, 164, 0.08);
}
.badge.active {
color: var(--orange);
background: rgba(255, 173, 85, 0.08);
}
.badge.paused {
color: var(--muted);
background: rgba(145, 167, 189, 0.08);
}
.metric-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px;
margin-top: 16px;
}
.metric-box {
min-width: 0;
padding: 10px;
border: 1px solid rgba(153, 188, 220, 0.11);
border-radius: 11px;
background: rgba(255, 255, 255, 0.025);
}
.metric-box span {
display: block;
margin-bottom: 4px;
color: var(--muted);
font-size: 0.65rem;
font-weight: 700;
text-transform: uppercase;
letter-spacing: 0.08em;
}
.metric-box strong {
font-size: 1rem;
font-variant-numeric: tabular-nums;
}
#controls {
position: absolute;
left: 22px;
bottom: 22px;
width: min(410px, calc(100vw - 44px));
padding: 18px;
}
.control-heading {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 15px;
}
.control-heading strong {
font-size: 0.78rem;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.mass-chip {
padding: 5px 8px;
color: var(--muted);
border: 1px solid rgba(153, 188, 220, 0.15);
border-radius: 999px;
font-size: 0.72rem;
font-weight: 700;
}
.force-control {
display: block;
margin-top: 13px;
}
.force-control > span {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 9px;
font-size: 0.83rem;
}
.force-control b {
font-weight: 700;
}
.force-control output {
color: var(--orange);
font-variant-numeric: tabular-nums;
font-weight: 800;
}
#left-force + output,
#left-force-label + output {
color: var(--purple);
}
input[type="range"] {
--pct: 0%;
width: 100%;
height: 6px;
margin: 0;
border-radius: 999px;
appearance: none;
-webkit-appearance: none;
background: linear-gradient(
to right,
var(--range-color, var(--orange)) 0%,
var(--range-color, var(--orange)) var(--pct),
#26354b var(--pct),
#26354b 100%
);
cursor: pointer;
}
#left-force {
--range-color: var(--purple);
}
input[type="range"]::-webkit-slider-thumb {
width: 18px;
height: 18px;
border: 3px solid #0c1728;
border-radius: 50%;
appearance: none;
-webkit-appearance: none;
background: var(--range-color, var(--orange));
box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.18), 0 3px 10px rgba(0, 0, 0, 0.35);
}
input[type="range"]::-moz-range-thumb {
width: 14px;
height: 14px;
border: 3px solid #0c1728;
border-radius: 50%;
background: var(--range-color, var(--orange));
box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.18);
}
.button-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 17px;
}
button {
min-height: 37px;
padding: 8px 11px;
border: 1px solid rgba(153, 188, 220, 0.18);
border-radius: 10px;
background: rgba(255, 255, 255, 0.045);
color: var(--text);
font-size: 0.78rem;
font-weight: 750;
cursor: pointer;
transition: background 150ms ease, border-color 150ms ease, transform 100ms ease;
}
button:hover {
background: rgba(255, 255, 255, 0.09);
border-color: rgba(153, 188, 220, 0.32);
}
button:active {
transform: translateY(1px);
}
button.primary {
border-color: rgba(94, 231, 247, 0.35);
background: rgba(94, 231, 247, 0.12);
color: #baf8ff;
}
button.coast {
border-color: rgba(102, 227, 164, 0.32);
background: rgba(102, 227, 164, 0.09);
color: #b5f8d7;
}
.formula {
display: flex;
align-items: center;
justify-content: space-between;
gap: 15px;
margin-top: 14px;
padding: 10px 12px;
border: 1px solid rgba(153, 188, 220, 0.1);
border-radius: 10px;
background: rgba(2, 8, 17, 0.28);
}
.formula span {
color: var(--muted);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 0.78rem;
}
.formula strong {
color: var(--cyan);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 0.9rem;
font-variant-numeric: tabular-nums;
}
#insight {
min-height: 38px;
margin: 11px 1px 0;
color: #b7c8d8;
font-size: 0.76rem;
line-height: 1.45;
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 12px;
margin-top: 12px;
padding-top: 11px;
border-top: 1px solid rgba(153, 188, 220, 0.1);
color: var(--muted);
font-size: 0.68rem;
}
.legend-item {
display: flex;
align-items: center;
gap: 6px;
}
.swatch {
width: 17px;
height: 3px;
border-radius: 3px;
}
.swatch.force {
background: var(--orange);
box-shadow: 0 0 8px rgba(255, 173, 85, 0.65);
}
.swatch.velocity {
background: var(--cyan);
box-shadow: 0 0 8px rgba(94, 231, 247, 0.65);
}
#camera-help {
position: absolute;
right: 22px;
bottom: 22px;
display: flex;
align-items: center;
gap: 13px;
padding: 10px 13px;
color: var(--muted);
font-size: 0.73rem;
}
#camera-help label {
display: flex;
align-items: center;
gap: 6px;
color: var(--text);
cursor: pointer;
}
#camera-help input {
accent-color: var(--cyan);
}
.vector-label {
position: absolute;
z-index: 4;
padding: 6px 9px;
border: 1px solid currentColor;
border-radius: 8px;
background: rgba(6, 13, 24, 0.88);
box-shadow: 0 7px 18px rgba(0, 0, 0, 0.25);
font-size: 0.69rem;
font-weight: 850;
letter-spacing: 0.055em;
white-space: nowrap;
pointer-events: none;
transform: translate(-50%, -115%);
transition: opacity 80ms linear;
}
#force-label {
color: var(--orange);
}
#velocity-label {
color: var(--cyan);
}
.vector-label.muted {
opacity: 0.64;
}
@media (max-width: 820px) {
#title-panel {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 14px 16px;
}
.subtitle {
display: none;
}
#metrics {
top: 119px;
right: 12px;
width: 228px;
padding: 12px;
}
#net-force {
font-size: 1.55rem;
}
.metric-grid {
margin-top: 10px;
}
.metric-box {
padding: 8px;
}
#controls {
left: 12px;
bottom: 12px;
width: calc(100vw - 24px);
max-height: 48vh;
overflow-y: auto;
padding: 14px;
}
#camera-help {
display: none;
}
.subtitle,
.legend {
font-size: 0.68rem;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive three-dimensional inertia and applied force simulation"></canvas>
<div id="hud">
<header id="title-panel" class="panel">
<div class="eyebrow">Mechanics Lab · Inertia</div>
<h1>Applied Force & Motion</h1>
<p class="subtitle">
Compare the orange force arrow with the cyan velocity arrow.
With zero net horizontal force, acceleration is zero and velocity stays constant.
</p>
</header>
<section id="metrics" class="panel" aria-live="polite">
<div class="metric-label">Net horizontal force</div>
<div class="metric-main">
<strong id="net-force">→ 12.0 N</strong>
<span id="status-badge" class="badge active">ACCELERATING</span>
</div>
<div class="metric-grid">
<div class="metric-box">
<span>Acceleration</span>
<strong id="acceleration">+6.00 m/s²</strong>
</div>
<div class="metric-box">
<span>Velocity</span>
<strong id="velocity">+0.0 m/s</strong>
</div>
<div class="metric-box">
<span>Puck mass</span>
<strong>2.0 kg</strong>
</div>
<div class="metric-box">
<span>Model</span>
<strong>No friction</strong>
</div>
</div>
</section>
<aside id="controls" class="panel">
<div class="control-heading">
<strong>Applied forces</strong>
<span class="mass-chip">Mass · 2.0 kg</span>
</div>
<label class="force-control" for="right-force">
<span>
<b>Push right</b>
<output id="right-output" for="right-force">12 N</output>
</span>
<input id="right-force" type="range" min="0" max="40" step="1" value="12">
</label>
<label class="force-control" for="left-force">
<span>
<b>Pull left</b>
<output id="left-output" for="left-force">0 N</output>
</span>
<input id="left-force" type="range" min="0" max="40" step="1" value="0">
</label>
<div class="button-grid">
<button id="play-button" class="primary" type="button">Pause motion</button>
<button id="reset-button" type="button">Reset motion</button>
<button id="coast-button" class="coast" type="button">Coast test</button>
<button id="balance-button" type="button">Balance at 20 N</button>
</div>
<div class="formula">
<span>a = F<sub>net</sub> / m</span>
<strong id="formula-value">+6.00 m/s²</strong>
</div>
<p id="insight">
The force arrow drives acceleration. The velocity arrow shows the puck’s actual motion.
</p>
<div class="legend">
<div class="legend-item"><i class="swatch force"></i> Force · acceleration driver</div>
<div class="legend-item"><i class="swatch velocity"></i> Velocity · motion state</div>
</div>
</aside>
<div id="camera-help" class="panel">
<span>Drag to orbit · Wheel to zoom</span>
<label><input id="follow-puck" type="checkbox" checked> Follow puck</label>
</div>
<div id="force-label" class="vector-label">FORCE → 12.0 N</div>
<div id="velocity-label" class="vector-label">VELOCITY 0.0 m/s</div>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const $ = (id) => document.getElementById(id);
const canvas = $("scene");
const rightForceInput = $("right-force");
const leftForceInput = $("left-force");
const rightOutput = $("right-output");
const leftOutput = $("left-output");
const netForceElement = $("net-force");
const accelerationElement = $("acceleration");
const velocityElement = $("velocity");
const statusBadge = $("status-badge");
const formulaValue = $("formula-value");
const insight = $("insight");
const playButton = $("play-button");
const forceLabel = $("force-label");
const velocityLabel = $("velocity-label");
const followInput = $("follow-puck");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07101d);
scene.fog = new THREE.Fog(0x07101d, 42, 105);
const camera = new THREE.PerspectiveCamera(
43,
window.innerWidth / window.innerHeight,
0.1,
220
);
const hemisphere = new THREE.HemisphereLight(0xa9ddff, 0x172033, 2.1);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffffff, 3.2);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -18;
keyLight.shadow.camera.right = 18;
keyLight.shadow.camera.top = 18;
keyLight.shadow.camera.bottom = -18;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 55;
keyLight.shadow.bias = -0.0003;
scene.add(keyLight);
scene.add(keyLight.target);
const rimLight = new THREE.DirectionalLight(0x65d9ff, 1.1);
rimLight.position.set(-10, 7, -12);
scene.add(rimLight);
const groundMaterial = new THREE.MeshStandardMaterial({
color: 0x0c1625,
roughness: 0.95,
metalness: 0.02
});
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(280, 100),
groundMaterial
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.19;
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(260, 130, 0x28415a, 0x182a3e);
grid.position.y = -0.175;
grid.material.transparent = true;
grid.material.opacity = 0.35;
scene.add(grid);
const trackMaterial = new THREE.MeshStandardMaterial({
color: 0x17283a,
roughness: 0.87,
metalness: 0.05,
flatShading: true
});
const track = new THREE.Mesh(
new THREE.BoxGeometry(240, 0.18, 5),
trackMaterial
);
track.position.y = -0.09;
track.receiveShadow = true;
scene.add(track);
const trackEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(track.geometry),
new THREE.LineBasicMaterial({
color: 0x5e7890,
transparent: true,
opacity: 0.42
})
);
trackEdges.position.copy(track.position);
scene.add(trackEdges);
const railMaterial = new THREE.MeshStandardMaterial({
color: 0x284158,
roughness: 0.75,
metalness: 0.12
});
for (const z of [-2.52, 2.52]) {
const rail = new THREE.Mesh(
new THREE.BoxGeometry(240, 0.11, 0.1),
railMaterial
);
rail.position.set(0, 0.04, z);
rail.castShadow = true;
scene.add(rail);
}
const tickMaterial = new THREE.MeshBasicMaterial({
color: 0x638299,
transparent: true,
opacity: 0.55
});
const majorTickMaterial = new THREE.MeshBasicMaterial({
color: 0x8aabc0,
transparent: true,
opacity: 0.72
});
const centerDashMaterial = new THREE.MeshBasicMaterial({
color: 0x79a5b7,
transparent: true,
opacity: 0.66
});
for (let x = -118; x <= 118; x += 2) {
const isMajor = x % 10 === 0;
const tick = new THREE.Mesh(
new THREE.BoxGeometry(isMajor ? 0.055 : 0.035, 0.018, isMajor ? 4.45 : 3.75),
isMajor ? majorTickMaterial : tickMaterial
);
tick.position.set(x, 0.012, 0);
scene.add(tick);
const dash = new THREE.Mesh(
new THREE.BoxGeometry(0.95, 0.018, 0.045),
centerDashMaterial
);
dash.position.set(x, 0.014, 0);
scene.add(dash);
}
const originMaterial = new THREE.MeshBasicMaterial({
color: 0xffad55,
transparent: true,
opacity: 0.78
});
const originLine = new THREE.Mesh(
new THREE.BoxGeometry(0.075, 0.025, 4.5),
originMaterial
);
originLine.position.set(0, 0.018, 0);
scene.add(originLine);
function createTextSprite(text, color = "#8eacc2") {
const labelCanvas = document.createElement("canvas");
labelCanvas.width = 256;
labelCanvas.height = 64;
const context = labelCanvas.getContext("2d");
context.clearRect(0, 0, 256, 64);
context.fillStyle = color;
context.font = "700 34px system-ui, sans-serif";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(text, 128, 32);
const texture = new THREE.CanvasTexture(labelCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(1.75, 0.44, 1);
return sprite;
}
for (let x = -100; x <= 100; x += 10) {
const label = createTextSprite(`${x} m`);
label.position.set(x, 0.38, 2.15);
scene.add(label);
}
const puck = new THREE.Group();
scene.add(puck);
const puckBodyMaterial = new THREE.MeshStandardMaterial({
color: 0xffa94f,
roughness: 0.48,
metalness: 0.16,
flatShading: true
});
const puckBody = new THREE.Mesh(
new THREE.CylinderGeometry(0.72, 0.78, 0.28, 12, 1, false),
puckBodyMaterial
);
puckBody.position.y = 0.16;
puckBody.castShadow = true;
puckBody.receiveShadow = true;
puck.add(puckBody);
const puckTopMaterial = new THREE.MeshStandardMaterial({
color: 0xffd18a,
roughness: 0.42,
metalness: 0.11,
flatShading: true
});
const puckTop = new THREE.Mesh(
new THREE.CylinderGeometry(0.56, 0.66, 0.08, 12, 1, false),
puckTopMaterial
);
puckTop.position.y = 0.34;
puckTop.castShadow = true;
puck.add(puckTop);
const ringMaterial = new THREE.MeshStandardMaterial({
color: 0xfff0bd,
emissive: 0x6b3d12,
emissiveIntensity: 0.18,
roughness: 0.35,
flatShading: true
});
const topRing = new THREE.Mesh(
new THREE.TorusGeometry(0.49, 0.027, 4, 12),
ringMaterial
);
topRing.rotation.x = Math.PI / 2;
topRing.position.y = 0.39;
puck.add(topRing);
const contactShadow = new THREE.Mesh(
new THREE.CircleGeometry(0.82, 16),
new THREE.MeshBasicMaterial({
color: 0x000000,
transparent: true,
opacity: 0.28,
depthWrite: false
})
);
contactShadow.rotation.x = -Math.PI / 2;
contactShadow.position.y = 0.018;
scene.add(contactShadow);
function createArrow(color) {
const group = new THREE.Group();
const material = new THREE.MeshBasicMaterial({ color });
const shaftGeometry = new THREE.CylinderGeometry(0.037, 0.037, 1, 6);
shaftGeometry.rotateZ(-Math.PI / 2);
const shaft = new THREE.Mesh(shaftGeometry, material);
group.add(shaft);
const headGeometry = new THREE.ConeGeometry(0.13, 0.34, 6);
headGeometry.rotateZ(-Math.PI / 2);
const head = new THREE.Mesh(headGeometry, material);
group.add(head);
scene.add(group);
return { group, shaft, head, color };
}
const forceArrow = createArrow(0xffad55);
const velocityArrow = createArrow(0x5ee7f7);
function updateArrow(arrow, x, y, z, magnitude, visualScale, maxLength) {
const magnitudeAbsolute = Math.abs(magnitude);
if (magnitudeAbsolute < 0.0001) {
arrow.group.visible = false;
return 0;
}
arrow.group.visible = true;
const length = Math.min(maxLength, 0.48 + magnitudeAbsolute * visualScale);
const headLength = Math.min(0.34, length * 0.34);
const shaftLength = Math.max(0.05, length - headLength);
arrow.shaft.scale.set(shaftLength, 1, 1);
arrow.shaft.position.x = shaftLength / 2;
arrow.head.position.x = shaftLength + headLength / 2;
arrow.group.position.set(x, y, z);
arrow.group.rotation.y = magnitude < 0 ? Math.PI : 0;
return length;
}
const MAX_TRAIL_POINTS = 240;
const trailPositions = new Float32Array(MAX_TRAIL_POINTS * 3);
const trailGeometry = new THREE.BufferGeometry();
trailGeometry.setAttribute(
"position",
new THREE.BufferAttribute(trailPositions, 3)
);
trailGeometry.setDrawRange(0, 0);
const trail = new THREE.Line(
trailGeometry,
new THREE.LineBasicMaterial({
color: 0x5ee7f7,
transparent: true,
opacity: 0.42
})
);
trail.frustumCulled = false;
scene.add(trail);
let trailCount = 0;
function clearTrail() {
trailCount = 0;
trailGeometry.setDrawRange(0, 0);
}
function addTrailPoint(x) {
if (trailCount < MAX_TRAIL_POINTS) {
const index = trailCount * 3;
trailPositions[index] = x;
trailPositions[index + 1] = 0.045;
trailPositions[index + 2] = 0.68;
trailCount++;
} else {
trailPositions.copyWithin(0, 3);
const index = (MAX_TRAIL_POINTS - 1) * 3;
trailPositions[index] = x;
trailPositions[index + 1] = 0.045;
trailPositions[index + 2] = 0.68;
}
trailGeometry.attributes.position.needsUpdate = true;
trailGeometry.setDrawRange(0, trailCount);
}
const simulation = {
x: -6,
velocity: 0,
netForce: 12,
mass: 2,
playing: true
};
let rightForce = Number(rightForceInput.value);
let leftForce = Number(leftForceInput.value);
function paintRange(input) {
const minimum = Number(input.min);
const maximum = Number(input.max);
const value = Number(input.value);
const percentage = ((value - minimum) / (maximum - minimum)) * 100;
input.style.setProperty("--pct", `${percentage}%`);
}
function updateNetForce() {
simulation.netForce = rightForce - leftForce;
}
function syncForceInputs() {
rightForceInput.value = String(rightForce);
leftForceInput.value = String(leftForce);
rightOutput.textContent = `${rightForce} N`;
leftOutput.textContent = `${leftForce} N`;
paintRange(rightForceInput);
paintRange(leftForceInput);
updateNetForce();
}
rightForceInput.addEventListener("input", () => {
rightForce = Number(rightForceInput.value);
rightOutput.textContent = `${rightForce} N`;
paintRange(rightForceInput);
updateNetForce();
});
leftForceInput.addEventListener("input", () => {
leftForce = Number(leftForceInput.value);
leftOutput.textContent = `${leftForce} N`;
paintRange(leftForceInput);
updateNetForce();
});
function setPlaying(playing) {
simulation.playing = playing;
playButton.textContent = playing ? "Pause motion" : "Resume motion";
}
playButton.addEventListener("click", () => {
setPlaying(!simulation.playing);
});
$("reset-button").addEventListener("click", () => {
simulation.x = 0;
simulation.velocity = 0;
clearTrail();
cameraTarget.x = 0;
accumulator = 0;
});
$("coast-button").addEventListener("click", () => {
rightForce = 0;
leftForce = 0;
syncForceInputs();
simulation.x = -4;
simulation.velocity = 6;
clearTrail();
cameraTarget.x = simulation.x;
setPlaying(true);
});
$("balance-button").addEventListener("click", () => {
rightForce = 20;
leftForce = 20;
syncForceInputs();
});
function formatSigned(value, digits) {
if (Math.abs(value) < Math.pow(10, -digits) * 0.5) {
return (0).toFixed(digits);
}
const sign = value > 0 ? "+" : "−";
return `${sign}${Math.abs(value).toFixed(digits)}`;
}
function directionGlyph(value) {
if (Math.abs(value) < 0.0001) return "•";
return value > 0 ? "→" : "←";
}
function updateReadouts() {
const force = simulation.netForce;
const acceleration = force / simulation.mass;
const velocity = simulation.velocity;
netForceElement.textContent =
`${directionGlyph(force)} ${Math.abs(force).toFixed(1)} N`;
accelerationElement.textContent =
`${formatSigned(acceleration, 2)} m/s²`;
velocityElement.textContent =
`${formatSigned(velocity, 1)} m/s`;
formulaValue.textContent =
`${formatSigned(acceleration, 2)} m/s²`;
let statusText;
let statusClass;
if (!simulation.playing) {
statusText = "PAUSED";
statusClass = "paused";
insight.textContent =
"Motion is paused. Resume the simulation to apply the displayed forces.";
} else if (Math.abs(force) < 0.0001) {
statusClass = "coasting";
if (Math.abs(velocity) < 0.01) {
statusText = "AT REST";
insight.textContent =
"Net force and velocity are both zero, so the puck remains at rest.";
} else {
statusText = "COASTING";
insight.textContent =
"Zero net force means zero acceleration. With no friction, the puck keeps its current velocity.";
}
} else {
statusClass = "active";
if (Math.abs(velocity) < 0.03) {
statusText = "ACCELERATING";
} else if (Math.sign(force) === Math.sign(velocity)) {
statusText = "SPEEDING UP";
} else {
statusText = "SLOWING DOWN";
}
if (Math.sign(force) !== Math.sign(velocity) && Math.abs(velocity) >= 0.03) {
insight.textContent =
"The orange force opposes the cyan velocity, so the puck slows while its motion still points forward.";
} else {
insight.textContent =
"Force and velocity are independent: the orange arrow sets acceleration while the cyan arrow shows the actual motion.";
}
}
statusBadge.textContent = statusText;
statusBadge.className = `badge ${statusClass}`;
}
const cameraTarget = new THREE.Vector3(0, 0.55, 0);
let cameraAzimuth = 0.48;
let cameraPolar = 1.04;
let cameraRadius = 27;
function updateCamera(dt) {
if (followInput.checked) {
const followAmount = 1 - Math.exp(-dt * 3.4);
cameraTarget.x += (simulation.x - cameraTarget.x) * followAmount;
}
const sinPolar = Math.sin(cameraPolar);
const cosPolar = Math.cos(cameraPolar);
camera.position.set(
cameraTarget.x + cameraRadius * sinPolar * Math.sin(cameraAzimuth),
cameraTarget.y + cameraRadius * cosPolar,
cameraTarget.z + cameraRadius * sinPolar * Math.cos(cameraAzimuth)
);
camera.lookAt(cameraTarget);
keyLight.position.set(cameraTarget.x + 9, 17, 10);
keyLight.target.position.set(cameraTarget.x, 0, 0);
keyLight.target.updateMatrixWorld();
}
let dragging = false;
let previousPointerX = 0;
let previousPointerY = 0;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - previousPointerX;
const deltaY = event.clientY - previousPointerY;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
cameraAzimuth -= deltaX * 0.006;
cameraPolar = THREE.MathUtils.clamp(
cameraPolar + deltaY * 0.005,
0.48,
1.36
);
});
function stopDragging(event) {
dragging = false;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", stopDragging);
canvas.addEventListener("pointercancel", stopDragging);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraRadius = THREE.MathUtils.clamp(
cameraRadius + event.deltaY * 0.018,
16,
48
);
},
{ passive: false }
);
canvas.addEventListener("dblclick", () => {
cameraAzimuth = 0.48;
cameraPolar = 1.04;
cameraRadius = 27;
});
const labelAnchor = new THREE.Vector3();
function placeLabel(element, worldPosition) {
labelAnchor.copy(worldPosition).project(camera);
const visible =
labelAnchor.z > -1 &&
labelAnchor.z < 1 &&
Math.abs(labelAnchor.x) < 1.15 &&
Math.abs(labelAnchor.y) < 1.15;
element.style.left = `${(labelAnchor.x * 0.5 + 0.5) * window.innerWidth}px`;
element.style.top = `${(-labelAnchor.y * 0.5 + 0.5) * window.innerHeight}px`;
element.style.opacity = visible ? "1" : "0";
}
function updateVectorVisuals() {
const forceLength = updateArrow(
forceArrow,
simulation.x,
0.69,
-0.46,
simulation.netForce,
0.105,
5.3
);
const velocityLength = updateArrow(
velocityArrow,
simulation.x,
1.16,
0.46,
simulation.velocity,
0.26,
5.7
);
const forceDirection = simulation.netForce < 0 ? -1 : 1;
const velocityDirection = simulation.velocity < 0 ? -1 : 1;
forceLabel.textContent =
`FORCE ${directionGlyph(simulation.netForce)} ${Math.abs(simulation.netForce).toFixed(1)} N`;
forceLabel.classList.toggle("muted", Math.abs(simulation.netForce) < 0.0001);
velocityLabel.textContent =
`VELOCITY ${directionGlyph(simulation.velocity)} ${Math.abs(simulation.velocity).toFixed(1)} m/s`;
velocityLabel.classList.toggle("muted", Math.abs(simulation.velocity) < 0.0001);
const forceAnchorPosition = new THREE.Vector3(
simulation.x +
(Math.abs(simulation.netForce) < 0.0001 ? 0.8 : forceDirection * forceLength),
0.85,
-0.46
);
const velocityAnchorPosition = new THREE.Vector3(
simulation.x +
(Math.abs(simulation.velocity) < 0.0001 ? 0.8 : velocityDirection * velocityLength),
1.32,
0.46
);
placeLabel(forceLabel, forceAnchorPosition);
placeLabel(velocityLabel, velocityAnchorPosition);
}
const FIXED_TIME_STEP = 1 / 120;
let accumulator = 0;
let trailStepCounter = 0;
let previousTime = performance.now();
function physicsStep(dt) {
// No friction or hidden forces: acceleration comes only from the displayed net force.
const acceleration = simulation.netForce / simulation.mass;
// If net force is exactly zero, acceleration is zero and velocity remains unchanged.
simulation.velocity += acceleration * dt;
simulation.x += simulation.velocity * dt;
trailStepCounter++;
if (trailStepCounter >= 4) {
addTrailPoint(simulation.x);
trailStepCounter = 0;
}
}
function animate(now) {
requestAnimationFrame(animate);
const frameDelta = Math.min((now - previousTime) / 1000, 0.05);
previousTime = now;
if (simulation.playing) {
accumulator += frameDelta;
while (accumulator >= FIXED_TIME_STEP) {
physicsStep(FIXED_TIME_STEP);
accumulator -= FIXED_TIME_STEP;
}
} else {
accumulator = 0;
}
puck.position.x = simulation.x;
contactShadow.position.x = simulation.x;
updateCamera(frameDelta);
updateReadouts();
renderer.render(scene, camera);
updateVectorVisuals();
}
function resize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
}
window.addEventListener("resize", resize);
syncForceInputs();
addTrailPoint(simulation.x);
updateCamera(1);
requestAnimationFrame(animate);
</script>
</body>
</html>
```