Prompt specification
Build or customize the Three.js 3D scene "Block on adjustable incline" 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: Block on adjustable incline
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/block-on-adjustable-incline-x1381
Scene Description Prompt:
A low-poly block on an adjustable ramp, with force arrows and a stated friction model. Three.js from cdnjs, one HTML file.
Working Three.js Starter (block-on-adjustable-incline-x1381):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Ramp Lab — Three.js Force Simulation</title>
<style>
:root {
color-scheme: dark;
--panel: rgba(13, 28, 32, 0.92);
--panel-soft: rgba(255, 255, 255, 0.06);
--border: rgba(255, 255, 255, 0.11);
--text: #f4f7f5;
--muted: #a9b8b7;
--accent: #80c7b9;
--gravity: #ef765f;
--normal: #45c2aa;
--friction: #f1cb67;
--pull: #aa8be5;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #dce9e6;
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input,
select {
font: inherit;
}
#viewport {
position: fixed;
inset: 0;
}
#viewport canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
}
.brand {
position: fixed;
top: 22px;
left: 24px;
z-index: 5;
display: flex;
align-items: center;
gap: 12px;
color: #183238;
pointer-events: none;
user-select: none;
}
.brand-mark {
display: grid;
width: 42px;
height: 42px;
place-items: center;
border: 1px solid rgba(24, 50, 56, 0.25);
border-radius: 12px;
background: rgba(255, 255, 255, 0.58);
backdrop-filter: blur(12px);
box-shadow: 0 10px 30px rgba(40, 69, 73, 0.1);
}
.brand-mark::before {
width: 19px;
height: 8px;
content: "";
border-radius: 3px;
background: #315d5e;
transform: rotate(-17deg);
box-shadow: 5px -5px 0 -1px #eea15e;
}
.brand-copy strong,
.brand-copy span {
display: block;
}
.brand-copy strong {
margin-bottom: 2px;
font-size: 15px;
letter-spacing: 0.02em;
}
.brand-copy span {
color: rgba(24, 50, 56, 0.62);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.19em;
text-transform: uppercase;
}
.panel {
position: fixed;
z-index: 10;
top: 16px;
right: 16px;
bottom: 16px;
width: 350px;
padding: 22px;
overflow-y: auto;
color: var(--text);
border: 1px solid var(--border);
border-radius: 24px;
background: var(--panel);
box-shadow: 0 24px 70px rgba(16, 35, 39, 0.24);
backdrop-filter: blur(22px);
scrollbar-width: thin;
scrollbar-color: rgba(255, 255, 255, 0.18) transparent;
}
.eyebrow {
margin-bottom: 8px;
color: var(--accent);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: 31px;
line-height: 1;
letter-spacing: -0.045em;
}
.intro {
margin: 10px 0 20px;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.status {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 18px;
padding: 12px 13px;
border: 1px solid var(--border);
border-radius: 14px;
background: var(--panel-soft);
}
.status-main {
display: flex;
min-width: 0;
align-items: center;
gap: 9px;
}
.status-dot {
width: 8px;
height: 8px;
flex: 0 0 auto;
border-radius: 50%;
background: var(--normal);
box-shadow: 0 0 0 5px rgba(69, 194, 170, 0.1);
}
#statusText {
overflow: hidden;
font-size: 12px;
font-weight: 750;
text-overflow: ellipsis;
white-space: nowrap;
}
#frictionMode {
padding: 5px 8px;
flex: 0 0 auto;
color: var(--friction);
border: 1px solid rgba(241, 203, 103, 0.22);
border-radius: 999px;
background: rgba(241, 203, 103, 0.08);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.section-title {
margin: 18px 0 11px;
color: #dce6e4;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.control {
margin-bottom: 14px;
}
.control-header {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 12px;
margin-bottom: 7px;
}
.control label {
color: #d9e3e1;
font-size: 11px;
font-weight: 650;
}
.control output {
color: var(--accent);
font-variant-numeric: tabular-nums;
font-size: 11px;
font-weight: 800;
}
input[type="range"] {
width: 100%;
height: 4px;
margin: 5px 0;
cursor: pointer;
border-radius: 999px;
outline: none;
appearance: none;
background: linear-gradient(
90deg,
var(--accent) 0%,
var(--accent) var(--fill, 50%),
rgba(255, 255, 255, 0.12) var(--fill, 50%),
rgba(255, 255, 255, 0.12) 100%
);
}
input[type="range"]::-webkit-slider-thumb {
width: 15px;
height: 15px;
cursor: grab;
border: 3px solid #173138;
border-radius: 50%;
appearance: none;
background: #b9e7dd;
box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.35);
}
input[type="range"]::-moz-range-thumb {
width: 11px;
height: 11px;
cursor: grab;
border: 3px solid #173138;
border-radius: 50%;
background: #b9e7dd;
box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.35);
}
select {
width: 100%;
padding: 10px 11px;
color: var(--text);
border: 1px solid var(--border);
border-radius: 10px;
outline: none;
background: rgba(255, 255, 255, 0.07);
font-size: 11px;
}
select option {
color: #172d31;
background: #edf4f2;
}
.metrics {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 7px;
margin-top: 15px;
}
.metric {
min-width: 0;
padding: 10px 8px;
border: 1px solid var(--border);
border-radius: 11px;
background: rgba(255, 255, 255, 0.045);
}
.metric span,
.metric strong {
display: block;
}
.metric span {
margin-bottom: 4px;
overflow: hidden;
color: var(--muted);
font-size: 8px;
font-weight: 750;
letter-spacing: 0.08em;
text-overflow: ellipsis;
text-transform: uppercase;
white-space: nowrap;
}
.metric strong {
overflow: hidden;
font-size: 11px;
font-variant-numeric: tabular-nums;
text-overflow: ellipsis;
white-space: nowrap;
}
.model {
padding: 13px;
border: 1px solid rgba(128, 199, 185, 0.19);
border-radius: 13px;
background: rgba(128, 199, 185, 0.06);
}
.model strong {
display: block;
margin-bottom: 7px;
color: var(--accent);
font-size: 10px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.model p {
margin: 0;
color: #b9c8c6;
font-size: 10px;
line-height: 1.55;
}
.model code {
color: #e4ece9;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
.buttons {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 16px;
}
button {
min-height: 38px;
cursor: pointer;
color: var(--text);
border: 1px solid var(--border);
border-radius: 10px;
background: rgba(255, 255, 255, 0.07);
font-size: 10px;
font-weight: 750;
transition:
background 160ms ease,
transform 160ms ease,
border-color 160ms ease;
}
button:hover {
border-color: rgba(255, 255, 255, 0.22);
background: rgba(255, 255, 255, 0.11);
}
button:active {
transform: translateY(1px);
}
button.primary {
color: #173137;
border-color: transparent;
background: var(--accent);
}
button.primary:hover {
background: #a0ddd1;
}
.view-button {
grid-column: 1 / -1;
}
.legend {
position: fixed;
z-index: 5;
bottom: 22px;
left: 24px;
display: flex;
flex-wrap: wrap;
gap: 8px;
max-width: 500px;
pointer-events: none;
user-select: none;
}
.legend-item {
display: flex;
align-items: center;
gap: 7px;
padding: 7px 10px;
color: #284247;
border: 1px solid rgba(24, 50, 56, 0.13);
border-radius: 999px;
background: rgba(255, 255, 255, 0.66);
backdrop-filter: blur(12px);
box-shadow: 0 8px 25px rgba(37, 66, 70, 0.08);
font-size: 9px;
font-weight: 750;
}
.legend-line {
width: 14px;
height: 3px;
border-radius: 2px;
}
.hint {
position: fixed;
z-index: 4;
right: 390px;
bottom: 25px;
color: rgba(24, 50, 56, 0.58);
font-size: 10px;
font-weight: 700;
pointer-events: none;
user-select: none;
}
#loading {
position: fixed;
z-index: 30;
inset: 0;
display: grid;
place-items: center;
color: #254247;
background: #dce9e6;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
transition: opacity 300ms ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 760px) {
.brand {
top: 14px;
left: 14px;
}
.brand-copy {
display: none;
}
.panel {
top: auto;
right: 10px;
bottom: 10px;
left: 10px;
width: auto;
max-height: 53vh;
padding: 17px;
border-radius: 19px;
}
h1 {
font-size: 25px;
}
.intro {
margin-bottom: 13px;
}
.legend,
.hint {
display: none;
}
}
</style>
</head>
<body>
<div id="viewport"></div>
<div class="brand">
<div class="brand-mark"></div>
<div class="brand-copy">
<strong>Ramp Lab</strong>
<span>Interactive mechanics / 01</span>
</div>
</div>
<aside class="panel" aria-label="Simulation controls">
<div class="eyebrow">Force & friction study</div>
<h1>Ramp Lab</h1>
<p class="intro">
Adjust the incline and surface properties. The applied pull acts parallel
to the ramp and is included in the friction calculation.
</p>
<div class="status">
<div class="status-main">
<span class="status-dot" id="statusDot"></span>
<span id="statusText">Static equilibrium</span>
</div>
<span id="frictionMode">Static</span>
</div>
<div class="section-title">Ramp geometry</div>
<div class="control">
<div class="control-header">
<label for="angle">Ramp angle</label>
<output id="angleOut" for="angle">16°</output>
</div>
<input id="angle" type="range" min="0" max="38" step="1" value="16" />
</div>
<div class="section-title">Dry friction</div>
<div class="control">
<div class="control-header">
<label for="muS">Static coefficient, μs</label>
<output id="muSOut" for="muS">0.30</output>
</div>
<input id="muS" type="range" min="0" max="1" step="0.01" value="0.30" />
</div>
<div class="control">
<div class="control-header">
<label for="muK">Kinetic coefficient, μk</label>
<output id="muKOut" for="muK">0.24</output>
</div>
<input id="muK" type="range" min="0" max="0.30" step="0.01" value="0.24" />
</div>
<div class="section-title">Applied force</div>
<div class="control">
<div class="control-header">
<label for="pull">Parallel pull</label>
<output id="pullOut" for="pull">0 N</output>
</div>
<input id="pull" type="range" min="0" max="180" step="1" value="0" />
</div>
<div class="control">
<div class="control-header">
<label for="direction">Pull direction</label>
</div>
<select id="direction">
<option value="down">Down the slope</option>
<option value="up">Up the slope</option>
</select>
</div>
<div class="metrics">
<div class="metric">
<span>Normal</span>
<strong id="normalValue">113.2 N</strong>
</div>
<div class="metric">
<span>Friction</span>
<strong id="frictionValue">32.5 N ↑</strong>
</div>
<div class="metric">
<span>Ramp net</span>
<strong id="netValue">0.0 N</strong>
</div>
</div>
<div class="section-title">Friction model</div>
<div class="model">
<strong>Coulomb dry friction</strong>
<p>
The pull is parallel to the ramp, so
<code>N = mg cos(θ)</code>. At rest, static friction balances the
tangential drive when
<code>|mg sin(θ) ± F| ≤ μsN</code>. Otherwise the block slides with
<code>|fk| = μkN</code> opposing motion. The block is treated as a
non-rotating point mass.
</p>
</div>
<div class="buttons">
<button id="pauseButton" class="primary" type="button">Pause motion</button>
<button id="resetButton" type="button">Reset block</button>
<button id="viewButton" class="view-button" type="button">Reset camera</button>
</div>
</aside>
<div class="legend" aria-label="Force color legend">
<div class="legend-item">
<span class="legend-line" style="background: var(--gravity)"></span>
Gravity
</div>
<div class="legend-item">
<span class="legend-line" style="background: var(--normal)"></span>
Normal
</div>
<div class="legend-item">
<span class="legend-line" style="background: var(--friction)"></span>
Friction
</div>
<div class="legend-item">
<span class="legend-line" style="background: var(--pull)"></span>
Applied pull
</div>
</div>
<div class="hint">Drag to orbit · Scroll to zoom · Double-click to reset view</div>
<div id="loading">Building ramp…</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const viewport = document.getElementById("viewport");
const loading = document.getElementById("loading");
// ---------------------------------------------------------------------
// Scene and renderer
// ---------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdce9e6);
scene.fog = new THREE.Fog(0xdce9e6, 23, 43);
const camera = new THREE.PerspectiveCamera(
42,
innerWidth / innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.05;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
viewport.appendChild(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xf5fffc, 0x5f7470, 2.0);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff2dd, 3.1);
sun.position.set(-5, 12, 8);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -15;
sun.shadow.camera.right = 15;
sun.shadow.camera.top = 13;
sun.shadow.camera.bottom = -10;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 35;
sun.shadow.bias = -0.0005;
sun.shadow.normalBias = 0.025;
sun.target.position.set(-2, 0, 0);
scene.add(sun, sun.target);
const rim = new THREE.DirectionalLight(0x9ddbd0, 0.65);
rim.position.set(7, 5, -9);
scene.add(rim);
// ---------------------------------------------------------------------
// Ground
// ---------------------------------------------------------------------
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(60, 40),
new THREE.MeshStandardMaterial({
color: 0xd4e1dc,
roughness: 0.94,
metalness: 0
})
);
ground.rotation.x = -Math.PI / 2;
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(40, 40, 0x789b94, 0x9bb5ae);
grid.position.y = 0.008;
grid.material.transparent = true;
grid.material.opacity = 0.23;
scene.add(grid);
// ---------------------------------------------------------------------
// Ramp, support, hinge, and block
// ---------------------------------------------------------------------
const RAMP_LENGTH = 10;
const RAMP_WIDTH = 4;
const RAMP_THICKNESS = 0.38;
const LOWER_X = 3.2;
const BLOCK_WIDTH = 1.2;
const BLOCK_HEIGHT = 1;
const BLOCK_DEPTH = 1.1;
const rampPivot = new THREE.Group();
scene.add(rampPivot);
const rampMaterial = new THREE.MeshStandardMaterial({
color: 0x315d5e,
roughness: 0.78,
metalness: 0.04,
flatShading: true
});
const rampGeometry = new THREE.BoxGeometry(
RAMP_LENGTH,
RAMP_THICKNESS,
RAMP_WIDTH
);
const ramp = new THREE.Mesh(rampGeometry, rampMaterial);
ramp.position.set(-RAMP_LENGTH / 2, 0, 0);
ramp.castShadow = true;
ramp.receiveShadow = true;
rampPivot.add(ramp);
const rampEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(rampGeometry),
new THREE.LineBasicMaterial({
color: 0x183b3d,
transparent: true,
opacity: 0.75
})
);
rampEdges.position.copy(ramp.position);
rampPivot.add(rampEdges);
const trimMaterial = new THREE.MeshStandardMaterial({
color: 0x24494a,
roughness: 0.85
});
for (const z of [-RAMP_WIDTH / 2 + 0.06, RAMP_WIDTH / 2 - 0.06]) {
const trim = new THREE.Mesh(
new THREE.BoxGeometry(RAMP_LENGTH, 0.08, 0.1),
trimMaterial
);
trim.position.set(-RAMP_LENGTH / 2, RAMP_THICKNESS / 2 + 0.025, z);
trim.castShadow = true;
rampPivot.add(trim);
}
const slatMaterial = new THREE.MeshStandardMaterial({
color: 0x6f9690,
roughness: 0.86
});
for (let i = 0; i < 16; i++) {
const x = -RAMP_LENGTH + 0.35 + (i * (RAMP_LENGTH - 0.7)) / 15;
const slat = new THREE.Mesh(
new THREE.BoxGeometry(0.035, 0.014, RAMP_WIDTH - 0.28),
slatMaterial
);
slat.position.set(x, RAMP_THICKNESS / 2 + 0.009, 0);
rampPivot.add(slat);
}
const hinge = new THREE.Mesh(
new THREE.CylinderGeometry(0.24, 0.24, RAMP_WIDTH + 0.32, 12),
new THREE.MeshStandardMaterial({
color: 0x18383a,
roughness: 0.55,
metalness: 0.32
})
);
hinge.rotation.x = Math.PI / 2;
hinge.castShadow = true;
scene.add(hinge);
const support = new THREE.Mesh(
new THREE.CylinderGeometry(0.12, 0.17, 1, 8),
new THREE.MeshStandardMaterial({
color: 0x668b84,
roughness: 0.65,
metalness: 0.18
})
);
support.castShadow = true;
scene.add(support);
const supportFoot = new THREE.Mesh(
new THREE.BoxGeometry(0.85, 0.12, 0.85),
new THREE.MeshStandardMaterial({
color: 0x274a4b,
roughness: 0.8
})
);
supportFoot.castShadow = true;
supportFoot.receiveShadow = true;
scene.add(supportFoot);
const block = new THREE.Group();
rampPivot.add(block);
const blockMaterial = new THREE.MeshStandardMaterial({
color: 0xeea15e,
roughness: 0.7,
metalness: 0.02,
flatShading: true
});
const blockBody = new THREE.Mesh(
new THREE.BoxGeometry(BLOCK_WIDTH, BLOCK_HEIGHT, BLOCK_DEPTH),
blockMaterial
);
blockBody.castShadow = true;
blockBody.receiveShadow = true;
block.add(blockBody);
const blockEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(blockBody.geometry),
new THREE.LineBasicMaterial({
color: 0x704529,
transparent: true,
opacity: 0.72
})
);
block.add(blockEdges);
const strapMaterial = new THREE.MeshStandardMaterial({
color: 0x405455,
roughness: 0.75
});
const strap = new THREE.Mesh(
new THREE.BoxGeometry(BLOCK_WIDTH + 0.035, 0.15, BLOCK_DEPTH + 0.035),
strapMaterial
);
strap.castShadow = true;
block.add(strap);
const topPlate = new THREE.Mesh(
new THREE.BoxGeometry(0.72, 0.035, 0.72),
new THREE.MeshStandardMaterial({
color: 0xf5bd82,
roughness: 0.72
})
);
topPlate.position.y = BLOCK_HEIGHT / 2 + 0.021;
topPlate.castShadow = true;
block.add(topPlate);
// ---------------------------------------------------------------------
// Force arrows and canvas labels
// ---------------------------------------------------------------------
function roundedRectPath(ctx, x, y, width, height, radius) {
const r = Math.min(radius, width / 2, height / 2);
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.lineTo(x + width - r, y);
ctx.quadraticCurveTo(x + width, y, x + width, y + r);
ctx.lineTo(x + width, y + height - r);
ctx.quadraticCurveTo(
x + width,
y + height,
x + width - r,
y + height
);
ctx.lineTo(x + r, y + height);
ctx.quadraticCurveTo(x, y + height, x, y + height - r);
ctx.lineTo(x, y + r);
ctx.quadraticCurveTo(x, y, x + r, y);
ctx.closePath();
}
function createLabel(color) {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 96;
const ctx = canvas.getContext("2d");
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
texture.magFilter = THREE.LinearFilter;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
})
);
sprite.scale.set(2.12, 0.4, 1);
sprite.renderOrder = 30;
scene.add(sprite);
let lastText = "";
return {
sprite,
set(name, value, visible = true) {
sprite.visible = visible;
if (!visible) return;
const text = `${name.toUpperCase()} ${Math.abs(value).toFixed(1)} N`;
if (text === lastText) return;
lastText = text;
ctx.clearRect(0, 0, canvas.width, canvas.height);
roundedRectPath(ctx, 6, 8, 500, 80, 18);
ctx.fillStyle = "rgba(15, 31, 35, 0.88)";
ctx.fill();
ctx.strokeStyle = color;
ctx.lineWidth = 3;
ctx.stroke();
ctx.fillStyle = "#f5f8f7";
ctx.font = "700 27px system-ui, sans-serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, 256, 49);
texture.needsUpdate = true;
}
};
}
function createForceArrow(color) {
const helper = new THREE.ArrowHelper(
new THREE.Vector3(0, 1, 0),
new THREE.Vector3(),
1,
color,
0.25,
0.15
);
helper.line.material.depthTest = false;
helper.line.material.depthWrite = false;
helper.cone.material.depthTest = false;
helper.cone.material.depthWrite = false;
helper.line.renderOrder = 20;
helper.cone.renderOrder = 20;
scene.add(helper);
return {
helper,
label: createLabel(color)
};
}
const gravityArrow = createForceArrow(0xef765f);
const normalArrow = createForceArrow(0x45c2aa);
const frictionArrow = createForceArrow(0xf1cb67);
const pullArrow = createForceArrow(0xaa8be5);
const arrowDirection = new THREE.Vector3();
const arrowTip = new THREE.Vector3();
function updateArrow(arrow, origin, direction, force, name) {
const magnitude = Math.abs(force);
const visible = magnitude > 0.05;
arrow.helper.visible = visible;
arrow.label.sprite.visible = visible;
if (!visible) return;
arrowDirection.copy(direction).normalize();
const length = THREE.MathUtils.clamp(
0.4 + (magnitude / 100) * 1.2,
0.45,
2.45
);
arrow.helper.position.copy(origin);
arrow.helper.setDirection(arrowDirection);
arrow.helper.setLength(
length,
Math.min(0.3, length * 0.28),
Math.min(0.18, length * 0.14)
);
arrowTip.copy(origin).addScaledVector(arrowDirection, length);
arrow.label.sprite.position.copy(arrowTip);
arrow.label.sprite.position.y += 0.25;
arrow.label.set(name, magnitude, true);
}
// ---------------------------------------------------------------------
// Controls and physics configuration
// ---------------------------------------------------------------------
const angleInput = document.getElementById("angle");
const muSInput = document.getElementById("muS");
const muKInput = document.getElementById("muK");
const pullInput = document.getElementById("pull");
const directionInput = document.getElementById("direction");
const angleOut = document.getElementById("angleOut");
const muSOut = document.getElementById("muSOut");
const muKOut = document.getElementById("muKOut");
const pullOut = document.getElementById("pullOut");
const statusText = document.getElementById("statusText");
const frictionMode = document.getElementById("frictionMode");
const statusDot = document.getElementById("statusDot");
const normalValue = document.getElementById("normalValue");
const frictionValue = document.getElementById("frictionValue");
const netValue = document.getElementById("netValue");
const pauseButton = document.getElementById("pauseButton");
const resetButton = document.getElementById("resetButton");
const viewButton = document.getElementById("viewButton");
const config = {
angle: Number(angleInput.value),
muS: Number(muSInput.value),
muK: Number(muKInput.value),
pull: Number(pullInput.value),
direction: directionInput.value
};
const MASS = 12;
const GRAVITY = 9.81;
const FIXED_STEP = 1 / 120;
let positionOnRamp = 2.6;
let velocity = 0;
let normalForce = 0;
let driveForce = 0;
let frictionForce = 0;
let netForce = 0;
let motionMode = "static";
let paused = false;
let accumulator = 0;
function paintRange(input) {
const percent =
((Number(input.value) - Number(input.min)) /
(Number(input.max) - Number(input.min))) *
100;
input.style.setProperty("--fill", `${percent}%`);
}
function updateControlLabels() {
angleOut.textContent = `${config.angle}°`;
muSOut.textContent = config.muS.toFixed(2);
muKOut.textContent = config.muK.toFixed(2);
pullOut.textContent = `${config.pull} N`;
paintRange(angleInput);
paintRange(muSInput);
paintRange(muKInput);
paintRange(pullInput);
}
angleInput.addEventListener("input", () => {
config.angle = Number(angleInput.value);
updateControlLabels();
});
muSInput.addEventListener("input", () => {
config.muS = Number(muSInput.value);
if (config.muK > config.muS) {
config.muK = config.muS;
muKInput.value = config.muK;
}
muKInput.max = config.muS.toFixed(2);
updateControlLabels();
});
muKInput.addEventListener("input", () => {
config.muK = Number(muKInput.value);
updateControlLabels();
});
pullInput.addEventListener("input", () => {
config.pull = Number(pullInput.value);
updateControlLabels();
});
directionInput.addEventListener("change", () => {
config.direction = directionInput.value;
});
pauseButton.addEventListener("click", () => {
paused = !paused;
pauseButton.textContent = paused ? "Resume motion" : "Pause motion";
});
resetButton.addEventListener("click", () => {
positionOnRamp = 2.6;
velocity = 0;
frictionForce = 0;
netForce = 0;
motionMode = "static";
});
updateControlLabels();
// ---------------------------------------------------------------------
// Physics: Coulomb static and kinetic friction
// ---------------------------------------------------------------------
function stepPhysics(dt) {
const angle = THREE.MathUtils.degToRad(config.angle);
const sin = Math.sin(angle);
const cos = Math.cos(angle);
normalForce = MASS * GRAVITY * cos;
const signedPull =
config.pull * (config.direction === "down" ? 1 : -1);
// Positive values point down the slope.
driveForce = MASS * GRAVITY * sin + signedPull;
const staticLimit = config.muS * normalForce;
if (Math.abs(velocity) < 0.012) {
if (Math.abs(driveForce) <= staticLimit) {
velocity = 0;
frictionForce = -driveForce;
motionMode = "static";
} else {
const direction = Math.sign(driveForce);
frictionForce = -direction * config.muK * normalForce;
velocity +=
((driveForce + frictionForce) / MASS) * dt;
motionMode = "sliding";
}
} else {
frictionForce = -Math.sign(velocity) * config.muK * normalForce;
velocity += ((driveForce + frictionForce) / MASS) * dt;
motionMode = "sliding";
}
positionOnRamp += velocity * dt;
const minimum = 0.75;
const maximum = RAMP_LENGTH - 0.75;
if (positionOnRamp <= minimum) {
positionOnRamp = minimum;
velocity = Math.max(0, velocity);
}
if (positionOnRamp >= maximum) {
positionOnRamp = maximum;
velocity = Math.min(0, velocity);
}
netForce = driveForce + frictionForce;
}
// ---------------------------------------------------------------------
// Transform the ramp and update its support
// ---------------------------------------------------------------------
const supportAttachment = new THREE.Vector3();
function updateRampTransform() {
const angle = THREE.MathUtils.degToRad(config.angle);
rampPivot.rotation.z = -angle;
rampPivot.position.set(
LOWER_X,
(RAMP_THICKNESS / 2) * Math.cos(angle) + 0.025,
0
);
block.position.set(
-RAMP_LENGTH + positionOnRamp,
RAMP_THICKNESS / 2 + BLOCK_HEIGHT / 2 + 0.012,
0
);
rampPivot.updateMatrixWorld(true);
hinge.position.set(
LOWER_X,
(RAMP_THICKNESS / 2) * Math.cos(angle) + 0.025,
0
);
supportAttachment.set(
-RAMP_LENGTH + 0.65,
-RAMP_THICKNESS / 2,
0
);
rampPivot.localToWorld(supportAttachment);
const supportHeight = Math.max(0.02, supportAttachment.y - 0.09);
support.visible = supportHeight > 0.12;
support.position.set(
supportAttachment.x,
0.09 + supportHeight / 2,
0
);
support.scale.y = supportHeight;
supportFoot.position.set(supportAttachment.x, 0.06, 0);
}
// ---------------------------------------------------------------------
// Custom camera controls — no addons
// ---------------------------------------------------------------------
const cameraTarget = new THREE.Vector3(-1.25, 1.85, 0);
let cameraAzimuth = 0.62;
let cameraPolar = 1.02;
let cameraRadius = 16.2;
let dragging = false;
let previousPointerX = 0;
let previousPointerY = 0;
function updateCamera() {
const sinPolar = Math.sin(cameraPolar);
camera.position.set(
cameraTarget.x + cameraRadius * sinPolar * Math.sin(cameraAzimuth),
cameraTarget.y + cameraRadius * Math.cos(cameraPolar),
cameraTarget.z + cameraRadius * sinPolar * Math.cos(cameraAzimuth)
);
camera.lookAt(cameraTarget);
}
function resetCamera() {
cameraAzimuth = 0.62;
cameraPolar = 1.02;
cameraRadius = 16.2;
cameraTarget.set(-1.25, 1.85, 0);
updateCamera();
}
renderer.domElement.addEventListener("pointerdown", (event) => {
if (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 deltaX = event.clientX - previousPointerX;
const deltaY = event.clientY - previousPointerY;
cameraAzimuth -= deltaX * 0.005;
cameraPolar = THREE.MathUtils.clamp(
cameraPolar - deltaY * 0.005,
0.38,
1.43
);
previousPointerX = event.clientX;
previousPointerY = event.clientY;
});
function endDrag(event) {
dragging = false;
if (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();
cameraRadius = THREE.MathUtils.clamp(
cameraRadius + event.deltaY * 0.012,
9,
25
);
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetCamera);
viewButton.addEventListener("click", resetCamera);
window.addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
});
// ---------------------------------------------------------------------
// Force visualization
// ---------------------------------------------------------------------
const worldGravity = new THREE.Vector3(0, -1, 0);
const worldDownSlope = new THREE.Vector3();
const worldNormal = new THREE.Vector3();
const worldFriction = new THREE.Vector3();
const worldPull = new THREE.Vector3();
const blockWorld = new THREE.Vector3();
const frontOffset = new THREE.Vector3(0, 0, 0.76);
function updateForces() {
const angle = THREE.MathUtils.degToRad(config.angle);
worldDownSlope.set(Math.cos(angle), -Math.sin(angle), 0);
worldNormal.set(Math.sin(angle), Math.cos(angle), 0);
rampPivot.updateMatrixWorld(true);
block.getWorldPosition(blockWorld);
const gravityOrigin = blockWorld.clone().add(frontOffset);
updateArrow(
gravityArrow,
gravityOrigin,
worldGravity,
MASS * GRAVITY,
"Gravity"
);
const normalOrigin = blockWorld
.clone()
.addScaledVector(worldNormal, 0.54)
.add(frontOffset);
updateArrow(
normalArrow,
normalOrigin,
worldNormal,
normalForce,
"Normal"
);
worldFriction.copy(worldDownSlope).multiplyScalar(Math.sign(frictionForce));
const frictionOrigin = blockWorld
.clone()
.addScaledVector(worldNormal, -0.27)
.addScaledVector(worldDownSlope, -0.12)
.add(frontOffset);
updateArrow(
frictionArrow,
frictionOrigin,
worldFriction,
frictionForce,
"Friction"
);
const pullSign = config.direction === "down" ? 1 : -1;
worldPull.copy(worldDownSlope).multiplyScalar(pullSign);
const pullOrigin = blockWorld
.clone()
.addScaledVector(worldNormal, 0.16)
.addScaledVector(worldDownSlope, 0.58)
.add(frontOffset);
updateArrow(
pullArrow,
pullOrigin,
worldPull,
config.pull,
"Pull"
);
}
// ---------------------------------------------------------------------
// Readouts
// ---------------------------------------------------------------------
function updateReadouts() {
normalValue.textContent = `${normalForce.toFixed(1)} N`;
const frictionDirection = frictionForce < 0 ? "↑" : "↓";
frictionValue.textContent = `${Math.abs(frictionForce).toFixed(1)} N ${frictionDirection}`;
const netDirection = netForce > 0.05 ? "↓" : netForce < -0.05 ? "↑" : "—";
netValue.textContent = `${Math.abs(netForce).toFixed(1)} N ${netDirection}`;
frictionMode.textContent = motionMode === "static" ? "Static" : "Kinetic";
if (paused) {
statusText.textContent = "Simulation paused";
statusDot.style.background = "#f1cb67";
statusDot.style.boxShadow = "0 0 0 5px rgba(241,203,103,.1)";
return;
}
const atLowerStop =
positionOnRamp >= RAMP_LENGTH - 0.751 && driveForce > 0;
const atUpperStop =
positionOnRamp <= 0.751 && driveForce < 0;
if (atLowerStop) {
statusText.textContent = "Stopped at lower end";
} else if (atUpperStop) {
statusText.textContent = "Stopped at upper end";
} else if (Math.abs(velocity) > 0.03) {
statusText.textContent =
velocity > 0 ? "Sliding down the slope" : "Sliding up the slope";
} else if (
motionMode === "static" &&
Math.abs(driveForce) <= config.muS * normalForce
) {
statusText.textContent = "Static equilibrium";
} else {
statusText.textContent = "Sliding / ready to slide";
}
statusDot.style.background =
motionMode === "static" ? "#45c2aa" : "#f1cb67";
statusDot.style.boxShadow =
motionMode === "static"
? "0 0 0 5px rgba(69,194,170,.1)"
: "0 0 0 5px rgba(241,203,103,.1)";
}
// ---------------------------------------------------------------------
// Animation loop
// ---------------------------------------------------------------------
const clock = new THREE.Clock();
let uiTimer = 0;
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
if (!paused) {
accumulator += delta;
while (accumulator >= FIXED_STEP) {
stepPhysics(FIXED_STEP);
accumulator -= FIXED_STEP;
}
} else {
accumulator = 0;
}
updateRampTransform();
updateForces();
updateCamera();
uiTimer += delta;
if (uiTimer >= 0.08) {
updateReadouts();
uiTimer = 0;
}
renderer.render(scene, camera);
}
updateRampTransform();
updateCamera();
updateReadouts();
animate();
loading.classList.add("hidden");
setTimeout(() => loading.remove(), 350);
</script>
</body>
</html>
```