Prompt specification
Build or customize the Three.js 3D scene "Toy Tug Platform With Friction" 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: Toy Tug Platform With Friction
Category: Games & Play · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/toy-tug-platform-with-friction-x3674
Scene Description Prompt:
Animate two carts connected by a rope under a stated simplified traction model. Distinguish internal rope tension from external ground forces in the displayed diagram. Use Three.js from cdnjs in a single HTML file, with a 3D cutaway.
Working Three.js Starter (toy-tug-platform-with-friction-x3674):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Two Carts Connected by a Rope — 3D Force Cutaway</title>
<style>
:root {
color-scheme: dark;
--panel: rgba(9, 18, 32, 0.84);
--border: rgba(159, 202, 232, 0.22);
--text: #e8f3ff;
--muted: #9eb2c7;
--internal: #ffad32;
--external: #29d8ff;
--weight: #b5c2d4;
--drive: #ff5ba8;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #07111f;
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
canvas {
position: fixed;
inset: 0;
display: block;
touch-action: none;
}
#labelLayer {
position: fixed;
inset: 0;
z-index: 4;
pointer-events: none;
overflow: hidden;
}
.scene-label {
position: absolute;
left: 0;
top: 0;
transform: translate(-50%, -50%);
padding: 5px 8px;
border: 1px solid rgba(255, 255, 255, 0.18);
border-radius: 7px;
background: rgba(5, 13, 24, 0.88);
box-shadow: 0 4px 18px rgba(0, 0, 0, 0.28);
color: #edf7ff;
font-size: 11px;
font-weight: 700;
letter-spacing: 0.02em;
white-space: nowrap;
pointer-events: none;
}
.force-label.internal {
color: var(--internal);
border-color: rgba(255, 173, 50, 0.8);
background: rgba(53, 28, 5, 0.9);
}
.force-label.external {
color: var(--external);
border-color: rgba(41, 216, 255, 0.78);
background: rgba(3, 31, 43, 0.9);
}
.force-label.weight {
color: var(--weight);
border-color: rgba(181, 194, 212, 0.55);
}
.cart-label {
font-size: 12px;
background: rgba(6, 14, 26, 0.82);
}
.cutaway-label {
max-width: 230px;
text-align: center;
white-space: normal;
line-height: 1.4;
color: #d9edff;
border-color: rgba(41, 216, 255, 0.45);
}
#hud {
position: fixed;
inset: 0;
z-index: 10;
pointer-events: none;
}
.panel {
position: absolute;
border: 1px solid var(--border);
border-radius: 12px;
background: var(--panel);
box-shadow: 0 12px 45px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(12px);
-webkit-backdrop-filter: blur(12px);
}
#titlePanel {
top: 18px;
left: 18px;
width: min(440px, calc(100vw - 36px));
padding: 16px 18px;
}
#titlePanel h1 {
margin: 0;
font-size: clamp(19px, 2vw, 27px);
line-height: 1.12;
letter-spacing: -0.025em;
}
#titlePanel p {
margin: 8px 0 0;
color: var(--muted);
font-size: 12px;
line-height: 1.45;
}
.badges {
display: flex;
flex-wrap: wrap;
gap: 7px;
margin-top: 11px;
}
.badge {
padding: 5px 8px;
border: 1px solid var(--border);
border-radius: 999px;
color: #c8d9e9;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.07em;
text-transform: uppercase;
}
#modelPanel {
top: 157px;
left: 18px;
width: min(440px, calc(100vw - 36px));
max-height: calc(100vh - 295px);
overflow: auto;
padding: 15px 17px;
font-size: 12px;
line-height: 1.48;
color: #c8d6e4;
}
#modelPanel h2,
#legendPanel h2 {
margin: 0 0 8px;
color: #f3f8ff;
font-size: 12px;
letter-spacing: 0.09em;
text-transform: uppercase;
}
#modelPanel p {
margin: 7px 0;
}
.equation {
margin: 9px 0;
padding: 9px 10px;
border-left: 3px solid #6d8ba5;
background: rgba(255, 255, 255, 0.045);
color: #eef8ff;
font-family: "SFMono-Regular", Consolas, monospace;
font-size: 11px;
line-height: 1.55;
white-space: pre-line;
}
.system-note {
padding: 9px 10px;
border: 1px solid rgba(255, 173, 50, 0.28);
border-radius: 8px;
background: rgba(255, 173, 50, 0.07);
color: #ffd89e;
}
#legendPanel {
top: 18px;
right: 18px;
width: 300px;
padding: 15px 16px;
}
.legend-row {
display: grid;
grid-template-columns: 12px 1fr;
gap: 9px;
align-items: start;
margin: 9px 0;
color: #c8d6e4;
font-size: 11px;
line-height: 1.4;
}
.swatch {
width: 12px;
height: 12px;
margin-top: 1px;
border-radius: 3px;
}
.swatch.internal {
background: var(--internal);
box-shadow: 0 0 13px rgba(255, 173, 50, 0.6);
}
.swatch.external {
background: var(--external);
box-shadow: 0 0 13px rgba(41, 216, 255, 0.5);
}
.swatch.weight {
background: var(--weight);
}
.legend-row strong {
display: block;
color: #f1f7ff;
font-size: 11px;
}
#statsPanel {
left: 18px;
bottom: 18px;
width: min(500px, calc(100vw - 36px));
padding: 12px 14px;
}
.stats-grid {
display: grid;
grid-template-columns: repeat(4, minmax(0, 1fr));
gap: 8px;
}
.stat {
padding: 8px 9px;
border: 1px solid rgba(255, 255, 255, 0.08);
border-radius: 8px;
background: rgba(255, 255, 255, 0.035);
}
.stat-label {
color: var(--muted);
font-size: 9px;
font-weight: 750;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.stat-value {
margin-top: 3px;
color: #f5fbff;
font-family: "SFMono-Regular", Consolas, monospace;
font-size: 13px;
font-weight: 700;
}
.stat.internal .stat-value {
color: var(--internal);
}
.stat.external .stat-value {
color: var(--external);
}
#controlPanel {
right: 18px;
bottom: 18px;
padding: 11px 12px;
pointer-events: auto;
}
.button-row {
display: flex;
gap: 7px;
}
button {
border: 1px solid rgba(151, 198, 229, 0.28);
border-radius: 8px;
padding: 8px 10px;
background: rgba(35, 58, 79, 0.88);
color: #eef8ff;
font: inherit;
font-size: 11px;
font-weight: 750;
cursor: pointer;
}
button:hover {
background: rgba(48, 78, 103, 0.96);
}
button:active {
transform: translateY(1px);
}
#cameraHint {
margin-top: 8px;
color: var(--muted);
font-size: 10px;
text-align: center;
}
@media (max-width: 900px) {
#modelPanel {
display: none;
}
#legendPanel {
top: auto;
right: 12px;
bottom: 92px;
width: 250px;
}
#titlePanel {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
}
#statsPanel {
left: 12px;
bottom: 12px;
width: calc(100vw - 24px);
}
#controlPanel {
right: 12px;
bottom: 136px;
}
.stats-grid {
grid-template-columns: repeat(2, minmax(0, 1fr));
}
.scene-label {
font-size: 9px;
}
}
@media (max-width: 560px) {
#legendPanel {
display: none;
}
#controlPanel {
right: 12px;
bottom: 190px;
}
}
</style>
</head>
<body>
<div id="labelLayer"></div>
<div id="hud">
<section id="titlePanel" class="panel">
<h1>Two carts, one rope</h1>
<p>
A 3D cutaway force diagram for a driven cart pulling a second cart.
The camera follows the midpoint while the track scrolls past.
</p>
<div class="badges">
<span class="badge">Massless inextensible rope</span>
<span class="badge">Kinetic Coulomb friction</span>
<span class="badge">Level track</span>
</div>
</section>
<section id="modelPanel" class="panel">
<h2>Stated simplified model</h2>
<p>
Cart masses are <b>m₁ = 2.0 kg</b> and <b>m₂ = 1.5 kg</b>.
Friction coefficients are <b>μ₁ = 0.12</b> and
<b>μ₂ = 0.10</b>. The rope is massless and inextensible, so both
carts have the same acceleration.
</p>
<div class="equation">
m₁a = Fₜ − f₁ − T
m₂a = T − f₂
a = (Fₜ − f₁ − f₂)/(m₁ + m₂)
T = m₂a + f₂
Nᵢ = mᵢg
fᵢ = μᵢNᵢ = μᵢmᵢg
Fₜ(t) = 5.5 + 1.5 sin(0.9t) N
</div>
<p>
The track supplies a rightward tractive force
<b>Fₜ</b> to cart 1. Kinetic friction <b>f₁</b> and <b>f₂</b>
oppose the rightward motion. The carts begin with a small
rightward speed so the kinetic-friction model applies immediately.
</p>
<p class="system-note">
<b>System boundary:</b> cart 1 + rope + cart 2. The orange rope
tension is internal, so its two equal-and-opposite forces cancel
in the total system equation. The cyan forces exerted by the
track/ground are external and do not cancel.
</p>
<p>
The near terrain section and the near cart walls are removed in
the cutaway to expose the track layers, wheels, axles, and force
vectors. Gravity is shown only as context and is omitted from
the horizontal equations.
</p>
</section>
<section id="legendPanel" class="panel">
<h2>Force key</h2>
<div class="legend-row">
<div class="swatch internal"></div>
<div>
<strong>Internal rope tension</strong>
Equal and opposite T arrows act on the two carts.
</div>
</div>
<div class="legend-row">
<div class="swatch external"></div>
<div>
<strong>External ground/track forces</strong>
Cyan: traction Fₜ, kinetic drag f₁/f₂, and normal reactions N₁/N₂.
</div>
</div>
<div class="legend-row">
<div class="swatch weight"></div>
<div>
<strong>Weight</strong>
Gray arrows show each cart’s weight for visual context.
</div>
</div>
</section>
<section id="statsPanel" class="panel">
<div class="stats-grid">
<div class="stat external">
<div class="stat-label">Ground traction</div>
<div id="statTraction" class="stat-value">—</div>
</div>
<div class="stat internal">
<div class="stat-label">Internal tension</div>
<div id="statTension" class="stat-value">—</div>
</div>
<div class="stat">
<div class="stat-label">Acceleration</div>
<div id="statAcceleration" class="stat-value">—</div>
</div>
<div class="stat">
<div class="stat-label">Speed</div>
<div id="statSpeed" class="stat-value">—</div>
</div>
</div>
</section>
<section id="controlPanel" class="panel">
<div class="button-row">
<button id="pauseButton" type="button">Pause</button>
<button id="resetButton" type="button">Reset</button>
<button id="vectorButton" type="button">Hide vectors</button>
</div>
<div id="cameraHint">
Drag: orbit · Shift/right-drag: pan · Wheel/pinch: zoom
</div>
</section>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer, scene, camera
// -------------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07111f);
scene.fog = new THREE.Fog(0x07111f, 25, 78);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
document.body.prepend(renderer.domElement);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
240
);
const cameraControls = {
target: new THREE.Vector3(0, 0.45, 0),
spherical: new THREE.Spherical(),
dragging: null,
pointers: new Map(),
pinchDistance: 0
};
camera.position.set(8.2, 6.0, 10.5);
cameraControls.spherical.setFromVector3(
camera.position.clone().sub(cameraControls.target)
);
const hemi = new THREE.HemisphereLight(0x9edcff, 0x231b24, 2.0);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xffffff, 3.0);
sun.position.set(6, 13, 8);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -16;
sun.shadow.camera.right = 16;
sun.shadow.camera.top = 12;
sun.shadow.camera.bottom = -12;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 45;
sun.shadow.bias = -0.00025;
scene.add(sun);
const cyanRim = new THREE.PointLight(0x29d8ff, 18, 18, 2);
cyanRim.position.set(-5, 4, 5);
scene.add(cyanRim);
const orangeRim = new THREE.PointLight(0xffad32, 12, 14, 2);
orangeRim.position.set(5, 3, 2);
scene.add(orangeRim);
// -------------------------------------------------------------------------
// Shared helpers
// -------------------------------------------------------------------------
const COLORS = {
internal: 0xffad32,
external: 0x29d8ff,
weight: 0xb5c2d4,
cart1: 0x2a79a8,
cart2: 0x7653b7
};
function makeBox(
width,
height,
depth,
material,
x,
y,
z,
parent = scene
) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function addEdges(mesh, color = 0xb9ddff, opacity = 0.22) {
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(mesh.geometry),
new THREE.LineBasicMaterial({
color,
transparent: true,
opacity
})
);
mesh.add(edges);
return edges;
}
const labelLayer = document.getElementById("labelLayer");
const labels = [];
function createLabel(text, className = "") {
const element = document.createElement("div");
element.className = `scene-label ${className}`.trim();
element.innerHTML = text;
labelLayer.appendChild(element);
const label = {
element,
position: new THREE.Vector3(),
visible: true
};
labels.push(label);
return label;
}
function projectLabels() {
const projected = new THREE.Vector3();
for (const label of labels) {
if (!label.visible) {
label.element.style.display = "none";
continue;
}
projected.copy(label.position).project(camera);
if (
projected.z < -1 ||
projected.z > 1 ||
Math.abs(projected.x) > 1.25 ||
Math.abs(projected.y) > 1.25
) {
label.element.style.display = "none";
continue;
}
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
label.element.style.display = "block";
label.element.style.left = `${x}px`;
label.element.style.top = `${y}px`;
}
}
// -------------------------------------------------------------------------
// Cutaway ground and rail
// -------------------------------------------------------------------------
const groundGroup = new THREE.Group();
scene.add(groundGroup);
const groundLength = 120;
const groundDepth = 5.4;
const groundCenterZ = -1.5;
const cutFaceZ = groundCenterZ + groundDepth / 2;
const topsoilMat = new THREE.MeshStandardMaterial({
color: 0x355545,
roughness: 0.94
});
const subsoilMat = new THREE.MeshStandardMaterial({
color: 0x755238,
roughness: 0.98
});
const bedrockMat = new THREE.MeshStandardMaterial({
color: 0x34475b,
roughness: 0.9
});
makeBox(
groundLength, 0.45, groundDepth,
topsoilMat,
0, -0.225, groundCenterZ, groundGroup
);
makeBox(
groundLength, 0.8, groundDepth,
subsoilMat,
0, -0.85, groundCenterZ, groundGroup
);
makeBox(
groundLength, 1.4, groundDepth,
bedrockMat,
0, -1.95, groundCenterZ, groundGroup
);
const stratumLineMat = new THREE.MeshStandardMaterial({
color: 0xb9d2dc,
emissive: 0x203542,
emissiveIntensity: 0.3,
roughness: 0.8
});
makeBox(
groundLength, 0.025, 0.035,
stratumLineMat,
0, -0.45, cutFaceZ + 0.018, groundGroup
);
makeBox(
groundLength, 0.025, 0.035,
stratumLineMat,
0, -1.25, cutFaceZ + 0.018, groundGroup
);
const sleeperMat = new THREE.MeshStandardMaterial({
color: 0x4b3d32,
roughness: 0.92
});
const railMat = new THREE.MeshStandardMaterial({
color: 0xa8bdc8,
metalness: 0.78,
roughness: 0.28
});
for (let x = -groundLength / 2; x <= groundLength / 2; x += 0.8) {
makeBox(
0.24, 0.09, 2.0,
sleeperMat,
x, 0.045, 0, groundGroup
);
}
makeBox(
groundLength, 0.10, 0.13,
railMat,
0, 0.14, -0.70, groundGroup
);
makeBox(
groundLength, 0.10, 0.13,
railMat,
0, 0.14, 0.70, groundGroup
);
// A highlighted line marks the exposed cut face.
const cutLine = new THREE.Line(
new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(-8, 0.012, cutFaceZ + 0.02),
new THREE.Vector3(8, 0.012, cutFaceZ + 0.02)
]),
new THREE.LineDashedMaterial({
color: COLORS.external,
dashSize: 0.28,
gapSize: 0.16,
transparent: true,
opacity: 0.75
})
);
cutLine.computeLineDistances();
groundGroup.add(cutLine);
const cutawayLabel = createLabel(
"<b>3D TERRAIN CUTAWAY</b><br>near ground section removed",
"cutaway-label"
);
// -------------------------------------------------------------------------
// Carts
// -------------------------------------------------------------------------
const ropeLength = 5.2;
const wheelRadius = 0.24;
const wheels = [];
function createCart(index, mass, bodyColor) {
const group = new THREE.Group();
group.position.x = index === 1 ? -ropeLength / 2 : ropeLength / 2;
scene.add(group);
const bodyMat = new THREE.MeshStandardMaterial({
color: bodyColor,
metalness: 0.28,
roughness: 0.48
});
const darkMetal = new THREE.MeshStandardMaterial({
color: 0x18222d,
metalness: 0.58,
roughness: 0.42
});
const hubMat = new THREE.MeshStandardMaterial({
color: 0xb5c7d2,
metalness: 0.72,
roughness: 0.3
});
const base = makeBox(
1.92, 0.18, 1.25,
bodyMat,
0, 0.68, 0, group
);
addEdges(base, 0xe0f5ff, 0.28);
// Far wall remains; the near wall is intentionally omitted.
makeBox(
1.92, 0.74, 0.10,
bodyMat,
0, 1.08, -0.61, group
);
makeBox(
0.10, 0.74, 1.22,
bodyMat,
-0.95, 1.08, 0, group
);
makeBox(
0.10, 0.74, 1.22,
bodyMat,
0.95, 1.08, 0, group
);
// Small structural rails retain the cutaway appearance.
makeBox(
1.86, 0.08, 0.08,
darkMetal,
0, 1.42, -0.61, group
);
makeBox(
0.08, 0.08, 1.18,
darkMetal,
-0.95, 1.42, 0, group
);
makeBox(
0.08, 0.08, 1.18,
darkMetal,
0.95, 1.42, 0, group
);
// Dashed outline marks the removed near-side wall.
const cutOutline = new THREE.Line(
new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(-0.94, 0.79, 0.64),
new THREE.Vector3(-0.94, 1.43, 0.64),
new THREE.Vector3(0.94, 1.43, 0.64),
new THREE.Vector3(0.94, 0.79, 0.64)
]),
new THREE.LineDashedMaterial({
color: 0xffc35c,
dashSize: 0.13,
gapSize: 0.09,
transparent: true,
opacity: 0.78
})
);
cutOutline.computeLineDistances();
group.add(cutOutline);
const wheelGeometry = new THREE.CylinderGeometry(
wheelRadius, wheelRadius, 0.16, 28
);
const hubGeometry = new THREE.CylinderGeometry(0.10, 0.10, 0.18, 20);
for (const x of [-0.62, 0.62]) {
const axle = new THREE.Mesh(
new THREE.CylinderGeometry(0.055, 0.055, 1.58, 14),
darkMetal
);
axle.rotation.x = Math.PI / 2;
axle.position.set(x, 0.43, 0);
axle.castShadow = true;
group.add(axle);
for (const z of [-0.78, 0.78]) {
const wheelGroup = new THREE.Group();
wheelGroup.position.set(x, 0.43, z);
group.add(wheelGroup);
const tire = new THREE.Mesh(wheelGeometry, darkMetal);
tire.rotation.x = Math.PI / 2;
tire.castShadow = true;
wheelGroup.add(tire);
const hub = new THREE.Mesh(hubGeometry, hubMat);
hub.rotation.x = Math.PI / 2;
wheelGroup.add(hub);
wheels.push(wheelGroup);
}
}
const hitchDirection = index === 1 ? 1 : -1;
const hitch = new THREE.Mesh(
new THREE.CylinderGeometry(0.065, 0.065, 0.30, 14),
hubMat
);
hitch.rotation.z = Math.PI / 2;
hitch.position.set(hitchDirection * 1.05, 0.86, 0);
hitch.castShadow = true;
group.add(hitch);
const cartLabel = createLabel(
`CART ${index} · m = ${mass.toFixed(1)} kg`,
"cart-label"
);
cartLabel.position.set(group.position.x, 1.70, -0.55);
return group;
}
const cart1 = createCart(1, 2.0, COLORS.cart1);
const cart2 = createCart(2, 1.5, COLORS.cart2);
// -------------------------------------------------------------------------
// Rope
// -------------------------------------------------------------------------
const ropeStart = new THREE.Vector3(-ropeLength / 2 + 1.05, 0.86, 0);
const ropeEnd = new THREE.Vector3(ropeLength / 2 - 1.05, 0.86, 0);
const ropeControl = new THREE.Vector3(0, 0.43, 0);
const ropeCurve = new THREE.QuadraticBezierCurve3(
ropeStart,
ropeControl,
ropeEnd
);
const ropeMaterial = new THREE.MeshStandardMaterial({
color: 0xff9d25,
emissive: 0x8d3500,
emissiveIntensity: 0.8,
roughness: 0.62,
metalness: 0.04
});
const ropeMesh = new THREE.Mesh(
new THREE.TubeGeometry(ropeCurve, 64, 0.052, 9, false),
ropeMaterial
);
ropeMesh.castShadow = true;
scene.add(ropeMesh);
const ropeBeads = [];
const beadGeometry = new THREE.SphereGeometry(0.035, 10, 8);
const beadMaterial = new THREE.MeshStandardMaterial({
color: 0xffd08a,
emissive: 0xff8a18,
emissiveIntensity: 2.2
});
for (let i = 0; i < 12; i++) {
const bead = new THREE.Mesh(beadGeometry, beadMaterial);
scene.add(bead);
ropeBeads.push(bead);
}
// -------------------------------------------------------------------------
// Force-vector helpers
// -------------------------------------------------------------------------
const forceGroup = new THREE.Group();
scene.add(forceGroup);
const vectorLabels = [];
function createArrow(name, direction, origin, color, initialLength, kind) {
const dir = direction.clone().normalize();
const arrow = new THREE.ArrowHelper(
dir,
origin.clone(),
initialLength,
color,
Math.min(0.24, initialLength * 0.32),
Math.min(0.14, initialLength * 0.18)
);
arrow.line.material.transparent = true;
arrow.line.material.opacity = 0.96;
arrow.line.material.depthTest = false;
arrow.cone.material.transparent = true;
arrow.cone.material.opacity = 0.96;
arrow.cone.material.depthTest = false;
arrow.line.renderOrder = 20;
arrow.cone.renderOrder = 20;
forceGroup.add(arrow);
const label = createLabel("", `force-label ${kind}`);
vectorLabels.push(label);
return {
name,
arrow,
label,
direction: dir,
origin: origin.clone(),
kind
};
}
const tension1 = createArrow(
"T1",
new THREE.Vector3(1, 0, 0),
new THREE.Vector3(-1.48, 1.18, 0),
COLORS.internal,
0.6,
"internal"
);
const tension2 = createArrow(
"T2",
new THREE.Vector3(-1, 0, 0),
new THREE.Vector3(1.48, 1.18, 0),
COLORS.internal,
0.6,
"internal"
);
const traction = createArrow(
"Ft",
new THREE.Vector3(1, 0, 0),
new THREE.Vector3(-3.48, 0.43, 0.82),
COLORS.external,
1.3,
"external"
);
const friction1 = createArrow(
"f1",
new THREE.Vector3(-1, 0, 0),
new THREE.Vector3(-1.82, 0.43, 0.82),
COLORS.external,
0.5,
"external"
);
const friction2 = createArrow(
"f2",
new THREE.Vector3(-1, 0, 0),
new THREE.Vector3(3.48, 0.43, 0.82),
COLORS.external,
0.4,
"external"
);
const normal1 = createArrow(
"N1",
new THREE.Vector3(0, 1, 0),
new THREE.Vector3(-2.60, 0.18, 1.00),
COLORS.external,
2.0,
"external"
);
const normal2 = createArrow(
"N2",
new THREE.Vector3(0, 1, 0),
new THREE.Vector3(2.60, 0.18, 1.00),
COLORS.external,
1.6,
"external"
);
const weight1 = createArrow(
"W1",
new THREE.Vector3(0, -1, 0),
new THREE.Vector3(-2.60, 1.12, -0.25),
COLORS.weight,
0.9,
"weight"
);
const weight2 = createArrow(
"W2",
new THREE.Vector3(0, -1, 0),
new THREE.Vector3(2.60, 1.12, -0.25),
COLORS.weight,
0.9,
"weight"
);
function updateArrow(object, length, text, offset) {
const headLength = Math.min(0.24, length * 0.32);
const headWidth = Math.min(0.14, length * 0.18);
object.arrow.setLength(
Math.max(0.01, length),
headLength,
headWidth
);
const endpoint = object.origin
.clone()
.addScaledVector(object.direction, length);
object.label.position.copy(endpoint).add(offset);
object.label.element.textContent = text;
}
// -------------------------------------------------------------------------
// Physics model
// -------------------------------------------------------------------------
const G = 9.81;
const M1 = 2.0;
const M2 = 1.5;
const MU1 = 0.12;
const MU2 = 0.10;
const state = {
time: 0,
x1: 0,
x2: ropeLength,
velocity: 0.20,
acceleration: 0,
traction: 0,
tension: 0,
normal1: M1 * G,
normal2: M2 * G,
friction1: MU1 * M1 * G,
friction2: MU2 * M2 * G,
wheelAngle: 0
};
let paused = false;
let vectorsVisible = true;
function computeDynamics() {
state.traction = 5.5 + 1.5 * Math.sin(0.9 * state.time);
state.acceleration =
(state.traction - state.friction1 - state.friction2) /
(M1 + M2);
state.tension = M2 * state.acceleration + state.friction2;
}
function stepPhysics(dt) {
state.time += dt;
computeDynamics();
state.velocity += state.acceleration * dt;
state.x1 += state.velocity * dt;
state.x2 = state.x1 + ropeLength;
state.wheelAngle -= (state.velocity * dt) / wheelRadius;
}
function resetSimulation() {
state.time = 0;
state.x1 = 0;
state.x2 = ropeLength;
state.velocity = 0.20;
state.wheelAngle = 0;
computeDynamics();
}
resetSimulation();
// -------------------------------------------------------------------------
// Camera controls implemented directly, without Three.js addons
// -------------------------------------------------------------------------
const raycaster = new THREE.Raycaster();
const pointerNDC = new THREE.Vector2();
const cameraRight = new THREE.Vector3();
const cameraUp = new THREE.Vector3();
function updateCamera() {
cameraControls.spherical.phi = THREE.MathUtils.clamp(
cameraControls.spherical.phi,
0.24,
1.48
);
cameraControls.spherical.radius = THREE.MathUtils.clamp(
cameraControls.spherical.radius,
6.5,
30
);
const offset = new THREE.Vector3().setFromSpherical(
cameraControls.spherical
);
camera.position.copy(cameraControls.target).add(offset);
camera.lookAt(cameraControls.target);
}
function updatePointerNDC(event) {
pointerNDC.x =
(event.clientX / window.innerWidth) * 2 - 1;
pointerNDC.y =
-(event.clientY / window.innerHeight) * 2 + 1;
raycaster.setFromCamera(pointerNDC, camera);
}
const canvas = renderer.domElement;
canvas.addEventListener("contextmenu", (event) => event.preventDefault());
canvas.addEventListener("pointerdown", (event) => {
canvas.setPointerCapture(event.pointerId);
cameraControls.pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (cameraControls.pointers.size === 1) {
cameraControls.dragging =
event.button === 2 || event.shiftKey ? "pan" : "orbit";
} else if (cameraControls.pointers.size === 2) {
const points = [...cameraControls.pointers.values()];
cameraControls.pinchDistance = Math.hypot(
points[0].x - points[1].x,
points[0].y - points[1].y
);
}
});
canvas.addEventListener("pointermove", (event) => {
const previous = cameraControls.pointers.get(event.pointerId);
if (!previous) return;
const dx = event.clientX - previous.x;
const dy = event.clientY - previous.y;
previous.x = event.clientX;
previous.y = event.clientY;
if (cameraControls.pointers.size === 1) {
if (cameraControls.dragging === "orbit") {
cameraControls.spherical.theta -= dx * 0.006;
cameraControls.spherical.phi -= dy * 0.005;
} else if (cameraControls.dragging === "pan") {
updateCamera();
cameraRight.set(1, 0, 0).applyQuaternion(camera.quaternion);
cameraUp.set(0, 1, 0).applyQuaternion(camera.quaternion);
const panScale =
cameraControls.spherical.radius * 0.0012;
cameraControls.target
.addScaledVector(cameraRight, -dx * panScale)
.addScaledVector(cameraUp, dy * panScale);
cameraControls.target.y = THREE.MathUtils.clamp(
cameraControls.target.y,
-1.5,
4.5
);
}
} else if (cameraControls.pointers.size === 2) {
const points = [...cameraControls.pointers.values()];
const distance = Math.hypot(
points[0].x - points[1].x,
points[0].y - points[1].y
);
if (cameraControls.pinchDistance > 0) {
const scale =
cameraControls.pinchDistance / Math.max(1, distance);
cameraControls.spherical.radius *= scale;
}
cameraControls.pinchDistance = distance;
}
});
function endPointer(event) {
cameraControls.pointers.delete(event.pointerId);
if (cameraControls.pointers.size === 0) {
cameraControls.dragging = null;
cameraControls.pinchDistance = 0;
}
}
canvas.addEventListener("pointerup", endPointer);
canvas.addEventListener("pointercancel", endPointer);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraControls.spherical.radius *=
Math.exp(event.deltaY * 0.001);
},
{ passive: false }
);
// -------------------------------------------------------------------------
// UI controls
// -------------------------------------------------------------------------
const pauseButton = document.getElementById("pauseButton");
const resetButton = document.getElementById("resetButton");
const vectorButton = document.getElementById("vectorButton");
pauseButton.addEventListener("click", () => {
paused = !paused;
pauseButton.textContent = paused ? "Resume" : "Pause";
});
resetButton.addEventListener("click", () => {
resetSimulation();
});
vectorButton.addEventListener("click", () => {
vectorsVisible = !vectorsVisible;
forceGroup.visible = vectorsVisible;
for (const label of vectorLabels) {
label.visible = vectorsVisible;
}
vectorButton.textContent =
vectorsVisible ? "Hide vectors" : "Show vectors";
});
// -------------------------------------------------------------------------
// Animation and visual updates
// -------------------------------------------------------------------------
const statTraction = document.getElementById("statTraction");
const statTension = document.getElementById("statTension");
const statAcceleration = document.getElementById("statAcceleration");
const statSpeed = document.getElementById("statSpeed");
let previousTime = performance.now();
let statTimer = 0;
const tempWorldPoint = new THREE.Vector3();
function updateVisuals(dt) {
// Scroll a repeating 120 m track section past the camera.
const midpoint = (state.x1 + state.x2) * 0.5;
const wrappedMidpoint = THREE.MathUtils.euclideanModulo(
midpoint,
groundLength
);
groundGroup.position.x = -wrappedMidpoint;
cutawayLabel.position.set(
4.3,
-1.18,
cutFaceZ + 0.06
);
groundGroup.localToWorld(cutawayLabel.position);
for (const wheel of wheels) {
wheel.rotation.z = state.wheelAngle;
}
// Moving highlights suggest energy transport through the internal rope.
ropeBeads.forEach((bead, index) => {
const t =
(index / ropeBeads.length + state.time * 0.16) % 1;
bead.position.copy(ropeCurve.getPoint(t));
});
const tensionLength = 0.25 * state.tension;
const tractionLength = 0.25 * state.traction;
const friction1Length = 0.22 * state.friction1;
const friction2Length = 0.22 * state.friction2;
updateArrow(
tension1,
tensionLength,
`T = ${state.tension.toFixed(2)} N · internal`,
new THREE.Vector3(0, 0.22, 0)
);
updateArrow(
tension2,
tensionLength,
`T = ${state.tension.toFixed(2)} N · internal`,
new THREE.Vector3(0, 0.22, 0)
);
updateArrow(
traction,
tractionLength,
`Fₜ = ${state.traction.toFixed(2)} N · ground`,
new THREE.Vector3(0, 0.30, 0)
);
updateArrow(
friction1,
friction1Length,
`f₁ = ${state.friction1.toFixed(2)} N · ground`,
new THREE.Vector3(0, 0.24, 0)
);
updateArrow(
friction2,
friction2Length,
`f₂ = ${state.friction2.toFixed(2)} N · ground`,
new THREE.Vector3(0, 0.24, 0)
);
updateArrow(
normal1,
0.12 * state.normal1,
`N₁ = ${state.normal1.toFixed(1)} N · ground`,
new THREE.Vector3(0.30, 0.10, 0)
);
updateArrow(
normal2,
0.12 * state.normal2,
`N₂ = ${state.normal2.toFixed(1)} N · ground`,
new THREE.Vector3(0.30, 0.10, 0)
);
updateArrow(
weight1,
0.9,
`W₁ = ${(M1 * G).toFixed(1)} N`,
new THREE.Vector3(0.30, 0, 0)
);
updateArrow(
weight2,
0.9,
`W₂ = ${(M2 * G).toFixed(1)} N`,
new THREE.Vector3(0.30, 0, 0)
);
// Ground traction is supplied at the driven rear wheel.
const drivenWheel = wheels.find(
(wheel) =>
wheel.parent === cart1 &&
Math.abs(wheel.position.x + 0.62) < 0.001 &&
wheel.position.z > 0
);
if (drivenWheel) {
drivenWheel.children[0].material.emissive.setHex(0x0b6378);
drivenWheel.children[0].material.emissiveIntensity =
0.25 + 0.35 * state.traction;
}
statTimer += dt;
if (statTimer > 0.08) {
statTimer = 0;
statTraction.textContent = `${state.traction.toFixed(2)} N`;
statTension.textContent = `${state.tension.toFixed(2)} N`;
statAcceleration.textContent =
`${state.acceleration.toFixed(2)} m/s²`;
statSpeed.textContent =
`${state.velocity.toFixed(2)} m/s`;
}
}
function animate(now) {
requestAnimationFrame(animate);
const dt = Math.min(0.04, (now - previousTime) / 1000);
previousTime = now;
if (!paused) {
stepPhysics(dt);
}
updateCamera();
updateVisuals(dt);
projectLabels();
renderer.render(scene, camera);
}
requestAnimationFrame(animate);
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(
Math.min(window.devicePixelRatio, 2)
);
renderer.setSize(
window.innerWidth,
window.innerHeight
);
});
</script>
</body>
</html>
```