Prompt specification
Build or customize the Three.js 3D scene "Friction threshold exploration tool" 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: Friction threshold exploration tool
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/friction-threshold-exploration-tool-x1384
Scene Description Prompt:
Adjust mass, slope, and friction on a low-poly ramp to explore sticking and sliding without spurious reversal. Three.js from cdnjs in one HTML file.
Working Three.js Starter (friction-threshold-exploration-tool-x1384):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Ramp Lab — Sticking & Sliding</title>
<style>
:root {
color-scheme: dark;
--bg: #07121c;
--panel: rgba(12, 26, 39, 0.91);
--panel-border: rgba(169, 207, 228, 0.15);
--text: #eef8ff;
--muted: #91a9ba;
--accent: #65e6b3;
--accent-2: #ff9b62;
--friction: #ff6f79;
--grid: rgba(255, 255, 255, 0.1);
}
* {
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;
}
#stage {
position: fixed;
inset: 0;
}
#stage canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#stage canvas:active {
cursor: grabbing;
}
.panel {
position: fixed;
z-index: 10;
top: 18px;
left: 18px;
width: min(350px, calc(100vw - 36px));
max-height: calc(100vh - 36px);
overflow: auto;
padding: 20px;
border: 1px solid var(--panel-border);
border-radius: 18px;
background: var(--panel);
box-shadow: 0 22px 70px rgba(0, 0, 0, 0.38);
backdrop-filter: blur(18px);
scrollbar-width: thin;
scrollbar-color: #355066 transparent;
}
.eyebrow {
margin-bottom: 7px;
color: var(--accent);
font-size: 0.7rem;
font-weight: 800;
letter-spacing: 0.17em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: 1.55rem;
line-height: 1.12;
letter-spacing: -0.035em;
}
.intro {
margin: 10px 0 19px;
color: var(--muted);
font-size: 0.84rem;
line-height: 1.5;
}
.control {
margin-top: 15px;
}
.control label {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 12px;
margin-bottom: 9px;
font-size: 0.86rem;
font-weight: 650;
}
.control output {
color: var(--accent);
font-variant-numeric: tabular-nums;
}
.control small {
display: block;
margin-top: 7px;
color: var(--muted);
font-size: 0.7rem;
line-height: 1.35;
}
input[type="range"] {
--fill: 50%;
width: 100%;
height: 6px;
margin: 0;
border-radius: 999px;
appearance: none;
background:
linear-gradient(
90deg,
var(--accent) 0 var(--fill),
rgba(255, 255, 255, 0.12) var(--fill) 100%
);
cursor: pointer;
outline: none;
}
input[type="range"]::-webkit-slider-thumb {
width: 18px;
height: 18px;
border: 3px solid #102535;
border-radius: 50%;
appearance: none;
background: #effff9;
box-shadow: 0 0 0 1px var(--accent);
}
input[type="range"]::-moz-range-thumb {
width: 14px;
height: 14px;
border: 3px solid #102535;
border-radius: 50%;
background: #effff9;
box-shadow: 0 0 0 1px var(--accent);
}
.divider {
height: 1px;
margin: 20px 0;
background: var(--panel-border);
}
.metrics {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 8px;
}
.metric {
min-width: 0;
padding: 11px;
border: 1px solid rgba(255, 255, 255, 0.08);
border-radius: 11px;
background: rgba(255, 255, 255, 0.035);
}
.metric span {
display: block;
margin-bottom: 5px;
color: var(--muted);
font-size: 0.66rem;
font-weight: 750;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.metric strong {
display: block;
overflow: hidden;
font-size: 0.91rem;
font-variant-numeric: tabular-nums;
text-overflow: ellipsis;
white-space: nowrap;
}
#mode[data-state="stick"] {
color: var(--accent);
}
#mode[data-state="slide"] {
color: var(--accent-2);
}
#mode[data-state="end"] {
color: var(--muted);
}
.forces {
margin-top: 17px;
}
.force-heading {
margin-bottom: 10px;
color: var(--muted);
font-size: 0.67rem;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.force-row {
display: grid;
grid-template-columns: 1fr auto;
gap: 4px 10px;
margin-top: 10px;
font-size: 0.76rem;
}
.force-row span {
color: var(--muted);
}
.force-row strong {
font-variant-numeric: tabular-nums;
}
.bar {
grid-column: 1 / -1;
height: 5px;
overflow: hidden;
border-radius: 999px;
background: rgba(255, 255, 255, 0.08);
}
.bar i {
display: block;
width: 0;
height: 100%;
border-radius: inherit;
transition: width 80ms linear;
}
#driveBar {
background: var(--accent);
}
#limitBar {
background: var(--friction);
}
.prediction {
position: relative;
margin-top: 17px;
padding: 12px 12px 12px 30px;
border: 1px solid rgba(101, 230, 179, 0.16);
border-radius: 11px;
background: rgba(101, 230, 179, 0.06);
color: #cbeee1;
font-size: 0.75rem;
line-height: 1.45;
}
.prediction::before {
position: absolute;
top: 15px;
left: 12px;
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--accent);
content: "";
box-shadow: 0 0 12px rgba(101, 230, 179, 0.8);
}
.actions {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px;
margin-top: 17px;
}
button {
min-height: 41px;
border: 1px solid rgba(255, 255, 255, 0.12);
border-radius: 10px;
background: rgba(255, 255, 255, 0.065);
color: var(--text);
font-size: 0.81rem;
font-weight: 750;
cursor: pointer;
transition:
background 140ms ease,
transform 140ms ease;
}
button:hover {
background: rgba(255, 255, 255, 0.11);
}
button:active {
transform: translateY(1px);
}
button.primary {
border-color: transparent;
background: var(--accent);
color: #092219;
}
button.primary:hover {
background: #7ceec3;
}
.legend {
position: fixed;
z-index: 8;
right: 18px;
bottom: 18px;
display: flex;
flex-wrap: wrap;
gap: 12px;
padding: 10px 13px;
border: 1px solid var(--panel-border);
border-radius: 12px;
background: rgba(9, 22, 34, 0.76);
color: var(--muted);
font-size: 0.7rem;
backdrop-filter: blur(12px);
}
.legend span {
display: flex;
align-items: center;
gap: 6px;
}
.dot {
width: 8px;
height: 8px;
border-radius: 50%;
}
.dot.green {
background: var(--accent);
}
.dot.red {
background: var(--friction);
}
.camera-hint {
position: fixed;
z-index: 8;
right: 18px;
top: 18px;
padding: 9px 12px;
border: 1px solid var(--panel-border);
border-radius: 10px;
background: rgba(9, 22, 34, 0.67);
color: var(--muted);
font-size: 0.68rem;
pointer-events: none;
backdrop-filter: blur(10px);
}
@media (max-width: 600px) {
.panel {
top: 10px;
left: 10px;
width: calc(100vw - 20px);
max-height: calc(100vh - 20px);
padding: 16px;
}
.camera-hint {
display: none;
}
.legend {
right: 10px;
bottom: 10px;
}
.intro {
margin-bottom: 13px;
}
}
</style>
</head>
<body>
<div id="stage"></div>
<aside class="panel">
<div class="eyebrow">Mechanics Lab</div>
<h1>Ramp / Stiction</h1>
<p class="intro">
Tune the block, incline, and surface. Static friction holds the block
until its limit is exceeded; kinetic friction then governs the slide.
</p>
<div class="control">
<label for="mass">
<span>Mass</span>
<output id="massValue">4.0 kg</output>
</label>
<input id="mass" type="range" min="0.5" max="20" step="0.1" value="4">
<small>Mass scales both the required force and friction capacity.</small>
</div>
<div class="control">
<label for="slope">
<span>Ramp slope</span>
<output id="slopeValue">18.0°</output>
</label>
<input id="slope" type="range" min="0" max="45" step="0.5" value="18">
</div>
<div class="control">
<label for="friction">
<span>Static friction μs</span>
<output id="frictionValue">0.35</output>
</label>
<input id="friction" type="range" min="0" max="1.2" step="0.01" value="0.35">
<small id="frictionHint">Kinetic friction μk = 0.29</small>
</div>
<div class="divider"></div>
<div class="metrics">
<div class="metric">
<span>State</span>
<strong id="mode" data-state="stick">STICKING</strong>
</div>
<div class="metric">
<span>Speed</span>
<strong id="speed">0.00 m/s</strong>
</div>
<div class="metric">
<span>Distance</span>
<strong id="distance">0.00 m</strong>
</div>
<div class="metric">
<span>Net accel.</span>
<strong id="acceleration">0.00 m/s²</strong>
</div>
</div>
<div class="forces">
<div class="force-heading">Force balance along the ramp</div>
<div class="force-row">
<span>Downhill pull</span>
<strong id="driveValue">0.0 N</strong>
<div class="bar"><i id="driveBar"></i></div>
</div>
<div class="force-row">
<span>Maximum static friction</span>
<strong id="limitValue">0.0 N</strong>
<div class="bar"><i id="limitBar"></i></div>
</div>
</div>
<div id="prediction" class="prediction">
Static friction balances the downhill pull, so the block stays put.
</div>
<div class="actions">
<button id="toggle" class="primary" type="button">Pause</button>
<button id="reset" type="button">Reset</button>
</div>
</aside>
<div class="camera-hint">Drag to orbit · Scroll to zoom · Space to pause</div>
<div class="legend">
<span><i class="dot green"></i>Downhill pull</span>
<span><i class="dot red"></i>Friction</span>
</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);
// ------------------------------------------------------------
// Configuration and physics state
// ------------------------------------------------------------
const G = 9.81;
const KINETIC_RATIO = 0.82;
const FIXED_DT = 1 / 240;
const RAMP_LENGTH = 10.8;
const RAMP_WIDTH = 2.4;
const START_X = -RAMP_LENGTH / 2 + 1.05;
const END_X = RAMP_LENGTH / 2 - 0.55;
const FLOOR_Y = -4.08;
const config = {
mass: 4,
slopeDeg: 18,
muS: 0.35
};
const simulation = {
x: START_X,
velocity: 0,
state: "stick",
complete: false
};
let running = true;
function physicsSnapshot() {
const theta = THREE.MathUtils.degToRad(config.slopeDeg);
const sin = Math.sin(theta);
const cos = Math.cos(theta);
const muK = config.muS * KINETIC_RATIO;
const normal = config.mass * G * cos;
const drive = config.mass * G * sin;
const staticLimit = config.muS * normal;
const kineticFriction = muK * normal;
// Ratios avoid mass-dependent tolerances and state chatter.
const driveRatio = sin;
const staticRatio = config.muS * cos;
const kineticRatio = muK * cos;
const netRatio = driveRatio - kineticRatio;
return {
theta,
sin,
cos,
muK,
normal,
drive,
staticLimit,
kineticFriction,
driveRatio,
staticRatio,
kineticRatio,
netRatio,
netForce: config.mass * G * netRatio,
acceleration: G * netRatio
};
}
function resetSimulation() {
const p = physicsSnapshot();
simulation.x = START_X;
simulation.velocity = 0;
simulation.complete = false;
simulation.state =
p.driveRatio > p.staticRatio + 1e-10 ? "slide" : "stick";
running = true;
accumulator = 0;
updateUI();
}
function stepPhysics(dt) {
if (!running || simulation.complete) return;
const p = physicsSnapshot();
if (simulation.state === "stick") {
if (p.driveRatio <= p.staticRatio + 1e-10) {
simulation.velocity = 0;
return;
}
// Static friction limit has been exceeded: release downhill.
simulation.state = "slide";
}
if (simulation.state === "slide") {
/*
This model has no uphill impulse. Friction can decelerate the
downhill velocity to zero, but the integration cannot manufacture
an uphill velocity by repeatedly reversing a small signed number.
*/
simulation.velocity += p.acceleration * dt;
if (simulation.velocity < 0) {
simulation.velocity = 0;
if (p.driveRatio <= p.staticRatio + 1e-10) {
simulation.state = "stick";
}
}
simulation.x += simulation.velocity * dt;
if (simulation.x >= END_X) {
simulation.x = END_X;
simulation.complete = true;
simulation.velocity = 0;
running = false;
}
}
}
// ------------------------------------------------------------
// Three.js scene
// ------------------------------------------------------------
const stage = $("stage");
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute(
"aria-label",
"Interactive low-poly ramp simulation"
);
stage.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07121c);
scene.fog = new THREE.Fog(0x07121c, 15, 38);
const camera = new THREE.PerspectiveCamera(
43,
1,
0.1,
80
);
const cameraState = {
target: new THREE.Vector3(0, -0.45, 0),
radius: 14.5,
azimuth: 0.64,
elevation: 0.47
};
function updateCamera() {
const spherical = new THREE.Spherical(
cameraState.radius,
Math.PI / 2 - cameraState.elevation,
cameraState.azimuth
);
camera.position.setFromSpherical(spherical).add(cameraState.target);
camera.lookAt(cameraState.target);
}
updateCamera();
const hemisphere = new THREE.HemisphereLight(0xc7ebff, 0x17222d, 2.0);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xffffff, 3.1);
sun.position.set(5, 11, 7);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -10;
sun.shadow.camera.right = 10;
sun.shadow.camera.top = 10;
sun.shadow.camera.bottom = -10;
sun.shadow.camera.near = 0.5;
sun.shadow.camera.far = 35;
sun.shadow.normalBias = 0.025;
scene.add(sun);
const rim = new THREE.DirectionalLight(0x6bbcff, 1.2);
rim.position.set(-8, 4, -7);
scene.add(rim);
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(45, 35),
new THREE.MeshStandardMaterial({
color: 0x0d1d29,
roughness: 1,
metalness: 0,
flatShading: true
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = FLOOR_Y;
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(36, 36, 0x385064, 0x25394a);
grid.position.y = FLOOR_Y + 0.008;
grid.material.transparent = true;
grid.material.opacity = 0.24;
scene.add(grid);
// Low-poly background forms.
const mountainGeometry = new THREE.ConeGeometry(1, 1, 5);
const mountainMaterial = new THREE.MeshStandardMaterial({
color: 0x112a3a,
roughness: 1,
flatShading: true
});
[
[-9.5, 2.8, -9, 2.8],
[-6.5, 1.8, -11, 2.0],
[8.5, 2.6, -10, 2.7],
[11.5, 1.7, -7, 1.8],
[-12, 1.4, -5, 1.5]
].forEach(([x, z, sx, sy]) => {
const mountain = new THREE.Mesh(mountainGeometry, mountainMaterial);
mountain.position.set(x, FLOOR_Y + sy / 2, z);
mountain.scale.set(sx, sy, sx);
mountain.receiveShadow = true;
scene.add(mountain);
});
// Ramp and block share this rotated group.
const rampGroup = new THREE.Group();
scene.add(rampGroup);
const rampMaterial = new THREE.MeshStandardMaterial({
color: 0x385168,
roughness: 0.82,
metalness: 0.05,
flatShading: true
});
const slab = new THREE.Mesh(
new THREE.BoxGeometry(RAMP_LENGTH, 0.34, RAMP_WIDTH),
rampMaterial
);
slab.position.y = -0.17;
slab.castShadow = true;
slab.receiveShadow = true;
rampGroup.add(slab);
const edgeMaterial = new THREE.MeshStandardMaterial({
color: 0x718ca1,
roughness: 0.7,
metalness: 0.12
});
[-1, 1].forEach((side) => {
const edge = new THREE.Mesh(
new THREE.BoxGeometry(RAMP_LENGTH, 0.065, 0.065),
edgeMaterial
);
edge.position.set(0, 0.025, side * (RAMP_WIDTH / 2 - 0.035));
edge.castShadow = true;
rampGroup.add(edge);
});
const tickGeometry = new THREE.BoxGeometry(
0.026,
0.022,
RAMP_WIDTH - 0.2
);
const tickMaterial = new THREE.MeshStandardMaterial({
color: 0x587185,
roughness: 0.9
});
for (let x = -4.5; x <= 4.5; x += 1) {
const tick = new THREE.Mesh(tickGeometry, tickMaterial);
tick.position.set(x, 0.014, 0);
rampGroup.add(tick);
}
const startMarker = new THREE.Mesh(
new THREE.BoxGeometry(0.085, 0.035, RAMP_WIDTH - 0.13),
new THREE.MeshStandardMaterial({
color: 0x65e6b3,
emissive: 0x174d3c,
emissiveIntensity: 0.55
})
);
startMarker.position.set(START_X - 0.25, 0.025, 0);
rampGroup.add(startMarker);
const endMarker = new THREE.Mesh(
new THREE.BoxGeometry(0.085, 0.035, RAMP_WIDTH - 0.13),
new THREE.MeshStandardMaterial({
color: 0xff9b62,
emissive: 0x5b2817,
emissiveIntensity: 0.45
})
);
endMarker.position.set(END_X + 0.23, 0.025, 0);
rampGroup.add(endMarker);
const blockGroup = new THREE.Group();
rampGroup.add(blockGroup);
const block = new THREE.Mesh(
new THREE.BoxGeometry(0.88, 0.78, 0.92),
new THREE.MeshStandardMaterial({
color: 0xff985b,
roughness: 0.55,
metalness: 0.08,
flatShading: true
})
);
block.position.y = 0.39;
block.castShadow = true;
block.receiveShadow = true;
blockGroup.add(block);
const blockBand = new THREE.Mesh(
new THREE.BoxGeometry(0.895, 0.115, 0.935),
new THREE.MeshStandardMaterial({
color: 0xffe0a3,
roughness: 0.7
})
);
blockBand.position.y = 0.46;
blockBand.castShadow = true;
blockGroup.add(blockBand);
const topInset = new THREE.Mesh(
new THREE.BoxGeometry(0.46, 0.035, 0.5),
new THREE.MeshStandardMaterial({
color: 0xb94925,
roughness: 0.8
})
);
topInset.position.y = 0.797;
topInset.castShadow = true;
blockGroup.add(topInset);
// Force vectors live in ramp-local coordinates.
const gravityArrow = new THREE.ArrowHelper(
new THREE.Vector3(1, 0, 0),
new THREE.Vector3(),
1,
0x65e6b3,
0.25,
0.13
);
gravityArrow.position.set(START_X, 1.08, 0.72);
rampGroup.add(gravityArrow);
const frictionArrow = new THREE.ArrowHelper(
new THREE.Vector3(-1, 0, 0),
new THREE.Vector3(),
1,
0xff6f79,
0.25,
0.13
);
frictionArrow.position.set(START_X, 1.08, -0.72);
rampGroup.add(frictionArrow);
[gravityArrow, frictionArrow].forEach((arrow) => {
arrow.line.material.depthTest = false;
arrow.cone.material.depthTest = false;
arrow.line.renderOrder = 10;
arrow.cone.renderOrder = 10;
});
// Vertical supports remain vertical while the ramp rotates.
const supportGeometry = new THREE.BoxGeometry(0.18, 1, 0.18);
const supportMaterial = new THREE.MeshStandardMaterial({
color: 0x263c4e,
roughness: 0.86,
metalness: 0.08
});
const supports = [-3.1, 3.1].map(() => {
const support = new THREE.Mesh(supportGeometry, supportMaterial);
support.castShadow = true;
support.receiveShadow = true;
scene.add(support);
return support;
});
function updateSupports(theta) {
[-3.1, 3.1].forEach((localX, index) => {
const top = new THREE.Vector3(localX, -0.24, 0);
top.applyAxisAngle(new THREE.Vector3(0, 0, 1), -theta);
const height = Math.max(0.08, top.y - FLOOR_Y);
supports[index].position.set(
top.x,
FLOOR_Y + height / 2,
top.z
);
supports[index].scale.set(1, height, 1);
});
}
// ------------------------------------------------------------
// Direct camera controls — no addons
// ------------------------------------------------------------
let dragging = false;
let previousPointerX = 0;
let previousPointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
if (event.pointerType === "mouse" && event.button !== 0) return;
dragging = true;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - previousPointerX;
const dy = event.clientY - previousPointerY;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
cameraState.azimuth -= dx * 0.006;
cameraState.elevation = THREE.MathUtils.clamp(
cameraState.elevation + dy * 0.005,
0.1,
1.18
);
updateCamera();
});
function endDrag(event) {
dragging = false;
if (
event.pointerType !== "mouse" &&
renderer.domElement.hasPointerCapture(event.pointerId)
) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
8,
21
);
updateCamera();
},
{ passive: false }
);
// ------------------------------------------------------------
// UI
// ------------------------------------------------------------
function fillRange(input) {
const min = Number(input.min);
const max = Number(input.max);
const value = Number(input.value);
const percent = ((value - min) / (max - min)) * 100;
input.style.setProperty("--fill", `${percent}%`);
}
function updateUI() {
const p = physicsSnapshot();
$("massValue").textContent = `${config.mass.toFixed(1)} kg`;
$("slopeValue").textContent = `${config.slopeDeg.toFixed(1)}°`;
$("frictionValue").textContent = config.muS.toFixed(2);
$("frictionHint").textContent =
`Kinetic friction μk = ${p.muK.toFixed(2)}`;
$("speed").textContent = `${simulation.velocity.toFixed(2)} m/s`;
$("distance").textContent =
`${Math.max(0, simulation.x - START_X).toFixed(2)} m`;
const displayedAcceleration =
simulation.state === "stick" ? 0 : p.acceleration;
$("acceleration").textContent =
`${displayedAcceleration >= 0 ? "+" : ""}${displayedAcceleration.toFixed(2)} m/s²`;
const mode = $("mode");
if (simulation.complete) {
mode.textContent = "AT END";
mode.dataset.state = "end";
} else if (simulation.state === "stick") {
mode.textContent = "STICKING";
mode.dataset.state = "stick";
} else {
mode.textContent = "SLIDING ↓";
mode.dataset.state = "slide";
}
$("driveValue").textContent = `${p.drive.toFixed(1)} N`;
$("limitValue").textContent = `${p.staticLimit.toFixed(1)} N`;
const forceScale = Math.max(
p.drive,
p.staticLimit,
p.kineticFriction,
1
);
$("driveBar").style.width =
`${(p.drive / forceScale) * 100}%`;
$("limitBar").style.width =
`${(p.staticLimit / forceScale) * 100}%`;
let prediction;
if (simulation.complete) {
prediction =
"The block reached the lower edge. Reset to run the experiment again.";
} else if (simulation.state === "stick") {
if (p.driveRatio > p.staticRatio + 1e-10) {
prediction =
"The static-friction limit has been exceeded; the block is releasing downhill.";
} else {
prediction =
"Static friction balances the downhill pull, so the block stays put.";
}
} else if (p.netRatio < -1e-10) {
prediction =
"Kinetic friction exceeds the downhill pull: the block slows and stops without reversing.";
} else {
prediction =
"The net force is downhill, so the block accelerates toward the lower edge.";
}
$("prediction").textContent = prediction;
$("toggle").textContent = simulation.complete
? "Replay"
: running
? "Pause"
: "Play";
}
function handleConfigChange() {
fillRange($("mass"));
fillRange($("slope"));
fillRange($("friction"));
updateUI();
}
$("mass").addEventListener("input", (event) => {
config.mass = Number(event.target.value);
handleConfigChange();
});
$("slope").addEventListener("input", (event) => {
config.slopeDeg = Number(event.target.value);
handleConfigChange();
});
$("friction").addEventListener("input", (event) => {
config.muS = Number(event.target.value);
handleConfigChange();
});
$("toggle").addEventListener("click", () => {
if (simulation.complete) {
resetSimulation();
} else {
running = !running;
accumulator = 0;
updateUI();
}
});
$("reset").addEventListener("click", resetSimulation);
window.addEventListener("keydown", (event) => {
const target = event.target;
if (
target instanceof HTMLInputElement ||
target instanceof HTMLButtonElement
) {
return;
}
if (event.code === "Space") {
event.preventDefault();
$("toggle").click();
}
if (event.key.toLowerCase() === "r") {
resetSimulation();
}
});
// ------------------------------------------------------------
// Rendering and fixed-step loop
// ------------------------------------------------------------
function updateVisuals() {
const p = physicsSnapshot();
const theta = p.theta;
rampGroup.rotation.z = -theta;
blockGroup.position.x = simulation.x;
gravityArrow.position.x = simulation.x;
frictionArrow.position.x = simulation.x;
const gravityLength = p.driveRatio * 1.65;
const frictionRatio =
simulation.state === "stick"
? p.driveRatio
: p.kineticRatio;
const frictionLength = frictionRatio * 1.65;
gravityArrow.visible = gravityLength > 0.025;
frictionArrow.visible = frictionLength > 0.025;
if (gravityArrow.visible) {
gravityArrow.setLength(
gravityLength,
Math.min(0.27, gravityLength * 0.32),
Math.min(0.15, gravityLength * 0.18)
);
}
if (frictionArrow.visible) {
frictionArrow.setLength(
frictionLength,
Math.min(0.27, frictionLength * 0.32),
Math.min(0.15, frictionLength * 0.18)
);
}
updateSupports(theta);
}
function resize() {
const width = stage.clientWidth;
const height = stage.clientHeight;
renderer.setSize(width, height, false);
camera.aspect = width / Math.max(1, height);
camera.updateProjectionMatrix();
}
new ResizeObserver(resize).observe(stage);
resize();
let accumulator = 0;
let previousTime = performance.now();
function animate(now) {
const frameTime = Math.min((now - previousTime) / 1000, 0.1);
previousTime = now;
if (running) {
accumulator += frameTime;
while (accumulator >= FIXED_DT) {
stepPhysics(FIXED_DT);
accumulator -= FIXED_DT;
}
}
updateVisuals();
updateUI();
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
handleConfigChange();
resetSimulation();
requestAnimationFrame(animate);
</script>
</body>
</html>
```