Prompt specification
Build or customize the Three.js 3D scene "Two-cart collision track" 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: Two-cart collision track
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/two-cart-collision-track-x1369
Scene Description Prompt:
Build two carts on a straight frictionless model track with mass and velocity labels; place a clear collision marker and keep initial conditions reproducible. Build this with Three.js from cdnjs as a low-poly scene in one self-contained HTML file.
Working Three.js Starter (two-cart-collision-track-x1369):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Frictionless Cart Collision</title>
<style>
:root {
color-scheme: light;
--ink: #152033;
--muted: #627087;
--panel: rgba(249, 251, 255, 0.88);
--line: rgba(21, 32, 51, 0.12);
--cyan: #00a9c5;
--orange: #ff8b3d;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #e9eef4;
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--ink);
}
#scene {
position: fixed;
inset: 0;
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
}
#scene:active {
cursor: grabbing;
}
.panel {
position: fixed;
z-index: 10;
border: 1px solid var(--line);
border-radius: 18px;
background: var(--panel);
box-shadow:
0 18px 50px rgba(28, 43, 66, 0.12),
inset 0 1px 0 rgba(255, 255, 255, 0.85);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
}
#info {
top: 20px;
left: 20px;
width: 326px;
padding: 18px;
pointer-events: none;
}
.eyebrow {
margin-bottom: 6px;
color: var(--cyan);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: 22px;
line-height: 1.15;
letter-spacing: -0.03em;
}
.subtitle {
margin: 7px 0 15px;
color: var(--muted);
font-size: 12px;
line-height: 1.45;
}
.section-title {
display: flex;
align-items: center;
justify-content: space-between;
margin: 14px 0 8px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.deterministic {
padding: 3px 7px;
border-radius: 999px;
background: rgba(0, 169, 197, 0.1);
color: #087e91;
letter-spacing: 0.04em;
}
.cart-row {
display: grid;
grid-template-columns: 30px 1fr auto;
align-items: center;
gap: 9px;
padding: 9px 0;
border-top: 1px solid var(--line);
font-size: 12px;
}
.cart-row strong {
font-size: 12px;
}
.cart-row .numbers {
color: var(--muted);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 11px;
text-align: right;
line-height: 1.45;
}
.swatch {
display: grid;
width: 27px;
height: 27px;
place-items: center;
border-radius: 8px;
color: white;
font-size: 11px;
font-weight: 900;
box-shadow: inset 0 -3px 0 rgba(0, 0, 0, 0.12);
}
.swatch.a {
background: var(--cyan);
}
.swatch.b {
background: var(--orange);
}
.outcome {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 8px;
}
.outcome div {
padding: 10px;
border: 1px solid var(--line);
border-radius: 11px;
background: rgba(255, 255, 255, 0.58);
}
.outcome span {
display: block;
margin-bottom: 4px;
color: var(--muted);
font-size: 10px;
}
.outcome b {
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 13px;
}
#live {
top: 20px;
right: 20px;
width: 205px;
padding: 15px;
pointer-events: none;
}
#phase {
display: inline-flex;
align-items: center;
gap: 7px;
color: #087e91;
font-size: 10px;
font-weight: 900;
letter-spacing: 0.11em;
text-transform: uppercase;
}
#phase::before {
width: 7px;
height: 7px;
border-radius: 50%;
background: currentColor;
box-shadow: 0 0 0 4px rgba(0, 169, 197, 0.12);
content: "";
}
#phase.collision,
#phase.post {
color: #d86720;
}
#phase.collision::before,
#phase.post::before {
box-shadow: 0 0 0 4px rgba(255, 139, 61, 0.15);
}
.clock {
margin: 10px 0 12px;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 30px;
font-weight: 800;
letter-spacing: -0.05em;
}
.clock small {
margin-left: 4px;
color: var(--muted);
font-size: 12px;
font-weight: 500;
letter-spacing: 0;
}
.live-row {
display: flex;
justify-content: space-between;
gap: 12px;
padding-top: 8px;
border-top: 1px solid var(--line);
color: var(--muted);
font-size: 11px;
}
.live-row b {
color: var(--ink);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-weight: 700;
}
#transport {
position: fixed;
z-index: 20;
bottom: 22px;
left: 50%;
display: flex;
gap: 8px;
padding: 7px;
border: 1px solid var(--line);
border-radius: 15px;
background: rgba(249, 251, 255, 0.9);
box-shadow: 0 12px 35px rgba(28, 43, 66, 0.14);
backdrop-filter: blur(12px);
transform: translateX(-50%);
}
button {
min-width: 92px;
padding: 10px 14px;
border: 0;
border-radius: 10px;
background: transparent;
color: var(--ink);
font: inherit;
font-size: 12px;
font-weight: 750;
cursor: pointer;
transition:
background 150ms ease,
transform 150ms ease;
}
button:hover {
background: rgba(21, 32, 51, 0.07);
}
button:active {
transform: translateY(1px);
}
button.primary {
background: var(--ink);
color: white;
}
button.primary:hover {
background: #263652;
}
#hint {
position: fixed;
z-index: 10;
right: 20px;
bottom: 24px;
color: var(--muted);
font-size: 10px;
pointer-events: none;
}
@media (max-width: 760px) {
#info {
top: 10px;
left: 10px;
width: min(310px, calc(100vw - 20px));
padding: 14px;
}
h1 {
font-size: 18px;
}
.subtitle,
.outcome {
display: none;
}
#live {
top: auto;
right: 10px;
bottom: 78px;
width: 165px;
padding: 12px;
}
#hint {
display: none;
}
#transport {
bottom: 10px;
}
button {
min-width: 76px;
padding: 9px 11px;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive 3D frictionless cart collision model"></canvas>
<section id="info" class="panel">
<div class="eyebrow">Physics Model 01</div>
<h1>Elastic Cart Collision</h1>
<p class="subtitle">
One-dimensional, perfectly elastic collision on a straight frictionless track.
</p>
<div class="section-title">
<span>Fixed initial conditions</span>
<span class="deterministic">DETERMINISTIC</span>
</div>
<div class="cart-row">
<span class="swatch a">A</span>
<strong>Cart A</strong>
<span class="numbers">
m = 2.00 kg<br />
x = −2.40 m<br />
v = +1.50 m/s
</span>
</div>
<div class="cart-row">
<span class="swatch b">B</span>
<strong>Cart B</strong>
<span class="numbers">
m = 1.00 kg<br />
x = +2.40 m<br />
v = −1.50 m/s
</span>
</div>
<div class="section-title">
<span>Predicted outcome</span>
<span>Energy = 3.375 J</span>
</div>
<div class="outcome">
<div>
<span>Cart A after</span>
<b>−0.50 m/s</b>
</div>
<div>
<span>Cart B after</span>
<b>+2.50 m/s</b>
</div>
</div>
</section>
<section id="live" class="panel">
<div id="phase">Approaching</div>
<div class="clock"><span id="time">0.00</span><small>s</small></div>
<div class="live-row">
<span>A velocity</span>
<b id="live-a">+1.50 m/s</b>
</div>
<div class="live-row">
<span>B velocity</span>
<b id="live-b">−1.50 m/s</b>
</div>
<div class="live-row">
<span>Contact point</span>
<b>x = 0.00 m</b>
</div>
</section>
<div id="transport">
<button id="play" class="primary" type="button">Pause</button>
<button id="replay" type="button">Replay</button>
</div>
<div id="hint">Drag to orbit · Scroll to zoom · Space to pause</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Deterministic model data
// -------------------------------------------------------------------------
const MODEL = Object.freeze({
cartLength: 1.5,
massA: 2.0,
massB: 1.0,
xA0: -2.4,
xB0: 2.4,
vA0: 1.5,
vB0: -1.5,
collisionX: 0,
maxTime: 4.2
});
// The carts have a visual rigid length, so contact occurs when their
// facing surfaces meet at x = 0.
const COLLISION_TIME =
(MODEL.xB0 - MODEL.xA0 - MODEL.cartLength) /
(MODEL.vA0 - MODEL.vB0);
const CONTACT_A = MODEL.collisionX - MODEL.cartLength / 2;
const CONTACT_B = MODEL.collisionX + MODEL.cartLength / 2;
const FINAL_VA =
((MODEL.massA - MODEL.massB) / (MODEL.massA + MODEL.massB)) *
MODEL.vA0 +
((2 * MODEL.massB) / (MODEL.massA + MODEL.massB)) * MODEL.vB0;
const FINAL_VB =
((2 * MODEL.massA) / (MODEL.massA + MODEL.massB)) * MODEL.vA0 +
((MODEL.massB - MODEL.massA) / (MODEL.massA + MODEL.massB)) * MODEL.vB0;
const state = {
time: 0,
playing: true,
collided: null
};
// -------------------------------------------------------------------------
// Renderer, scene, and camera
// -------------------------------------------------------------------------
const canvas = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
canvas,
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;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe9eef4);
scene.fog = new THREE.Fog(0xe9eef4, 29, 55);
const camera = new THREE.OrthographicCamera(-10, 10, 6, -6, 0.1, 100);
camera.zoom = 1.05;
const cameraTarget = new THREE.Vector3(0, 1.0, 0);
const cameraDistance = 24;
let cameraYaw = 0.14;
let cameraPitch = 0.36;
function updateCamera() {
const cp = Math.cos(cameraPitch);
const sp = Math.sin(cameraPitch);
camera.position.set(
cameraTarget.x + cameraDistance * cp * Math.sin(cameraYaw),
cameraTarget.y + cameraDistance * sp,
cameraTarget.z + cameraDistance * cp * Math.cos(cameraYaw)
);
camera.lookAt(cameraTarget);
}
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
renderer.setSize(width, height, false);
// Preserve at least 21.6 world units of horizontal coverage.
const halfHeight = Math.max(5.6, 10.8 / aspect);
camera.left = -halfHeight * aspect;
camera.right = halfHeight * aspect;
camera.top = halfHeight;
camera.bottom = -halfHeight;
camera.updateProjectionMatrix();
}
window.addEventListener("resize", resize);
resize();
updateCamera();
// Lightweight camera controls implemented without external addons.
let dragging = false;
let pointerX = 0;
let pointerY = 0;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
cameraYaw -= dx * 0.006;
cameraPitch = THREE.MathUtils.clamp(
cameraPitch + dy * 0.004,
0.12,
1.05
);
updateCamera();
});
canvas.addEventListener("pointerup", (event) => {
dragging = false;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
});
canvas.addEventListener("pointercancel", () => {
dragging = false;
});
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
camera.zoom = THREE.MathUtils.clamp(
camera.zoom * Math.exp(-event.deltaY * 0.001),
0.65,
1.8
);
camera.updateProjectionMatrix();
},
{ passive: false }
);
// -------------------------------------------------------------------------
// Lighting and low-poly environment
// -------------------------------------------------------------------------
const hemisphere = new THREE.HemisphereLight(0xffffff, 0x718096, 2.2);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xffffff, 3.1);
sun.position.set(-7, 13, 9);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -16;
sun.shadow.camera.right = 16;
sun.shadow.camera.top = 10;
sun.shadow.camera.bottom = -10;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 35;
sun.shadow.bias = -0.0003;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xb8dcff, 1.1);
fill.position.set(8, 5, -10);
scene.add(fill);
const groundMaterial = new THREE.MeshStandardMaterial({
color: 0xe6ebf1,
roughness: 0.95,
metalness: 0
});
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(60, 42),
groundMaterial
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.035;
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(50, 50, 0x9eacbd, 0xc7d0dc);
grid.position.y = -0.02;
grid.material.transparent = true;
grid.material.opacity = 0.22;
scene.add(grid);
function makeBox(
width,
height,
depth,
material,
x = 0,
y = 0,
z = 0,
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;
}
const bedMaterial = new THREE.MeshStandardMaterial({
color: 0x25344b,
roughness: 0.8,
metalness: 0.05,
flatShading: true
});
const sleeperMaterial = new THREE.MeshStandardMaterial({
color: 0x8995a5,
roughness: 0.95,
metalness: 0,
flatShading: true
});
const railMaterial = new THREE.MeshStandardMaterial({
color: 0xb7c2cf,
roughness: 0.32,
metalness: 0.65,
flatShading: true
});
const trackLength = 22;
makeBox(trackLength, 0.22, 3.05, bedMaterial, 0, 0.15, 0);
for (let x = -10.5; x <= 10.5; x += 1.05) {
makeBox(0.18, 0.12, 3.4, sleeperMaterial, x, 0.32, 0);
}
makeBox(trackLength, 0.12, 0.15, railMaterial, 0, 0.44, -0.68);
makeBox(trackLength, 0.12, 0.15, railMaterial, 0, 0.44, 0.68);
// End blocks make the track read as a physical model rather than an
// infinitely extending rail.
const endMaterial = new THREE.MeshStandardMaterial({
color: 0x40516a,
roughness: 0.7,
metalness: 0.1,
flatShading: true
});
makeBox(0.28, 0.48, 3.15, endMaterial, -11, 0.25, 0);
makeBox(0.28, 0.48, 3.15, endMaterial, 11, 0.25, 0);
// -------------------------------------------------------------------------
// Canvas-generated sprite labels
// -------------------------------------------------------------------------
function roundedRect(ctx, x, y, width, height, radius) {
const r = Math.min(radius, width / 2, height / 2);
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.arcTo(x + width, y, x + width, y + height, r);
ctx.arcTo(x + width, y + height, x, y + height, r);
ctx.arcTo(x, y + height, x, y, r);
ctx.arcTo(x, y, x + width, y, r);
ctx.closePath();
}
function createSprite(width = 640, height = 220) {
const labelCanvas = document.createElement("canvas");
labelCanvas.width = width;
labelCanvas.height = height;
const texture = new THREE.CanvasTexture(labelCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
texture.magFilter = THREE.LinearFilter;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false,
toneMapped: false
});
const sprite = new THREE.Sprite(material);
sprite.renderOrder = 20;
sprite.userData.canvas = labelCanvas;
sprite.userData.context = labelCanvas.getContext("2d");
sprite.userData.texture = texture;
return sprite;
}
function drawCartLabel(sprite, name, mass, velocity, accent) {
const ctx = sprite.userData.context;
const { width, height } = sprite.userData.canvas;
ctx.clearRect(0, 0, width, height);
ctx.shadowColor = "rgba(18, 29, 48, 0.20)";
ctx.shadowBlur = 22;
ctx.shadowOffsetY = 8;
roundedRect(ctx, 8, 8, width - 16, height - 16, 30);
ctx.fillStyle = "rgba(250, 252, 255, 0.95)";
ctx.fill();
ctx.shadowColor = "transparent";
ctx.lineWidth = 5;
ctx.strokeStyle = accent;
ctx.stroke();
roundedRect(ctx, 26, 27, 94, 48, 14);
ctx.fillStyle = accent;
ctx.fill();
ctx.fillStyle = "#ffffff";
ctx.font = "800 27px ui-monospace, SFMono-Regular, Menlo, monospace";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(name, 73, 52);
ctx.textAlign = "left";
ctx.fillStyle = "#65738a";
ctx.font = "800 24px ui-sans-serif, system-ui, sans-serif";
ctx.fillText(`CART ${name}`, 143, 52);
ctx.fillStyle = "#172238";
ctx.font = "800 37px ui-monospace, SFMono-Regular, Menlo, monospace";
ctx.fillText(`m = ${mass.toFixed(2)} kg`, 31, 113);
ctx.fillStyle = accent;
ctx.font = "800 39px ui-monospace, SFMono-Regular, Menlo, monospace";
ctx.fillText(`v = ${velocity}`, 31, 171);
sprite.userData.texture.needsUpdate = true;
}
function formatVelocity(value) {
const sign = value >= 0 ? "+" : "−";
return `${sign}${Math.abs(value).toFixed(2)} m/s`;
}
// -------------------------------------------------------------------------
// Low-poly carts
// -------------------------------------------------------------------------
function createCart(color, name) {
const group = new THREE.Group();
const bodyMaterial = new THREE.MeshStandardMaterial({
color,
roughness: 0.58,
metalness: 0.08,
flatShading: true
});
const darkMaterial = new THREE.MeshStandardMaterial({
color: 0x202a38,
roughness: 0.72,
metalness: 0.18,
flatShading: true
});
const topMaterial = new THREE.MeshStandardMaterial({
color: 0xf4f8fb,
roughness: 0.62,
metalness: 0.04,
flatShading: true
});
makeBox(1.5, 0.54, 1.2, bodyMaterial, 0, 1.08, 0, group);
makeBox(1.14, 0.08, 0.92, topMaterial, -0.04, 1.38, 0, group);
// Bumpers identify the contact faces.
makeBox(0.13, 0.34, 1.3, darkMaterial, -0.78, 1.02, 0, group);
makeBox(0.13, 0.34, 1.3, darkMaterial, 0.78, 1.02, 0, group);
const wheelGeometry = new THREE.CylinderGeometry(0.24, 0.24, 0.16, 12);
wheelGeometry.rotateX(Math.PI / 2);
const hubGeometry = new THREE.CylinderGeometry(0.105, 0.105, 0.18, 10);
hubGeometry.rotateX(Math.PI / 2);
for (const x of [-0.47, 0.47]) {
for (const z of [-0.68, 0.68]) {
const wheel = new THREE.Mesh(wheelGeometry, darkMaterial);
wheel.position.set(x, 0.77, z);
wheel.castShadow = true;
group.add(wheel);
const hub = new THREE.Mesh(hubGeometry, railMaterial);
hub.position.copy(wheel.position);
hub.castShadow = true;
group.add(hub);
}
}
// Flat arrow on the cart roof shows the current direction.
const arrowShape = new THREE.Shape();
arrowShape.moveTo(-0.23, -0.11);
arrowShape.lineTo(0.05, -0.11);
arrowShape.lineTo(0.05, -0.23);
arrowShape.lineTo(0.27, 0);
arrowShape.lineTo(0.05, 0.23);
arrowShape.lineTo(0.05, 0.11);
arrowShape.lineTo(-0.23, 0.11);
arrowShape.closePath();
const arrow = new THREE.Mesh(
new THREE.ShapeGeometry(arrowShape),
new THREE.MeshBasicMaterial({
color: 0xffffff,
side: THREE.DoubleSide,
toneMapped: false
})
);
arrow.rotation.x = -Math.PI / 2;
arrow.position.set(0.05, 1.43, 0);
group.add(arrow);
const label = createSprite();
label.scale.set(3.0, 1.03, 1);
label.position.set(0, 2.35, 0);
group.add(label);
group.userData = {
name,
arrow,
label
};
scene.add(group);
return group;
}
const cartA = createCart(0x00a9c5, "A");
const cartB = createCart(0xff8b3d, "B");
// -------------------------------------------------------------------------
// Collision marker
// -------------------------------------------------------------------------
const markerMaterial = new THREE.MeshBasicMaterial({
color: 0xff6b2c,
transparent: true,
opacity: 0.78,
depthWrite: false,
toneMapped: false
});
const markerRing = new THREE.Mesh(
new THREE.TorusGeometry(0.38, 0.045, 8, 32),
markerMaterial
);
markerRing.rotation.x = Math.PI / 2;
markerRing.position.set(MODEL.collisionX, 0.57, 0);
markerRing.renderOrder = 8;
scene.add(markerRing);
const markerBeam = new THREE.Mesh(
new THREE.BoxGeometry(0.045, 2.2, 2.55),
new THREE.MeshBasicMaterial({
color: 0xff7a35,
transparent: true,
opacity: 0.09,
depthWrite: false,
toneMapped: false
})
);
markerBeam.position.set(MODEL.collisionX, 1.55, 0);
markerBeam.renderOrder = 7;
scene.add(markerBeam);
const markerLine = new THREE.Mesh(
new THREE.BoxGeometry(0.075, 0.035, 2.45),
markerMaterial
);
markerLine.position.set(MODEL.collisionX, 0.54, 0);
markerLine.renderOrder = 8;
scene.add(markerLine);
const poleMaterial = new THREE.MeshStandardMaterial({
color: 0x596a80,
roughness: 0.6,
metalness: 0.3,
flatShading: true
});
for (const z of [-1.48, 1.48]) {
const pole = new THREE.Mesh(
new THREE.CylinderGeometry(0.035, 0.045, 1.25, 8),
poleMaterial
);
pole.position.set(MODEL.collisionX, 1.08, z);
pole.castShadow = true;
scene.add(pole);
const cap = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.11, 0),
new THREE.MeshBasicMaterial({
color: 0xff6b2c,
toneMapped: false
})
);
cap.position.set(MODEL.collisionX, 1.73, z);
scene.add(cap);
}
const markerLabel = createSprite(640, 140);
markerLabel.scale.set(3.35, 0.73, 1);
markerLabel.position.set(MODEL.collisionX, 2.95, 0);
{
const ctx = markerLabel.userData.context;
const { width, height } = markerLabel.userData.canvas;
ctx.clearRect(0, 0, width, height);
roundedRect(ctx, 8, 8, width - 16, height - 16, 28);
ctx.fillStyle = "rgba(255, 247, 240, 0.96)";
ctx.fill();
ctx.lineWidth = 5;
ctx.strokeStyle = "#ff6b2c";
ctx.stroke();
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillStyle = "#d95e20";
ctx.font = "900 31px ui-sans-serif, system-ui, sans-serif";
ctx.fillText("COLLISION POINT", width / 2, 51);
ctx.fillStyle = "#6c5648";
ctx.font = "700 23px ui-monospace, SFMono-Regular, Menlo, monospace";
ctx.fillText("x = 0.00 m • elastic", width / 2, 98);
markerLabel.userData.texture.needsUpdate = true;
}
scene.add(markerLabel);
// -------------------------------------------------------------------------
// UI and deterministic animation
// -------------------------------------------------------------------------
const phaseElement = document.querySelector("#phase");
const timeElement = document.querySelector("#time");
const liveAElement = document.querySelector("#live-a");
const liveBElement = document.querySelector("#live-b");
const playButton = document.querySelector("#play");
const replayButton = document.querySelector("#replay");
function updateLabels(collided) {
const velocityA = collided ? FINAL_VA : MODEL.vA0;
const velocityB = collided ? FINAL_VB : MODEL.vB0;
drawCartLabel(
cartA.userData.label,
"A",
MODEL.massA,
formatVelocity(velocityA),
"#00a9c5"
);
drawCartLabel(
cartB.userData.label,
"B",
MODEL.massB,
formatVelocity(velocityB),
"#ff8b3d"
);
cartA.userData.arrow.scale.x = velocityA >= 0 ? 1 : -1;
cartB.userData.arrow.scale.x = velocityB >= 0 ? 1 : -1;
liveAElement.textContent = formatVelocity(velocityA);
liveBElement.textContent = formatVelocity(velocityB);
}
function updateModel() {
const t = state.time;
const collided = t >= COLLISION_TIME;
let xA;
let xB;
if (!collided) {
xA = MODEL.xA0 + MODEL.vA0 * t;
xB = MODEL.xB0 + MODEL.vB0 * t;
} else {
const after = t - COLLISION_TIME;
xA = CONTACT_A + FINAL_VA * after;
xB = CONTACT_B + FINAL_VB * after;
}
cartA.position.x = xA;
cartB.position.x = xB;
if (state.collided !== collided) {
state.collided = collided;
updateLabels(collided);
phaseElement.className = collided ? "post" : "";
phaseElement.textContent = collided ? "Post-collision" : "Approaching";
}
timeElement.textContent = t.toFixed(2);
// A deterministic expanding pulse marks the exact contact moment.
const age = t - COLLISION_TIME;
const pulse = age >= 0 ? Math.exp(-age * 4.2) : 0;
markerRing.scale.setScalar(1 + pulse * 1.55);
markerRing.material.opacity = 0.7 + pulse * 0.3;
markerBeam.material.opacity = 0.09 + pulse * 0.24;
}
function togglePlayback() {
if (state.time >= MODEL.maxTime) {
state.time = 0;
state.collided = null;
}
state.playing = !state.playing;
playButton.textContent = state.playing ? "Pause" : "Play";
}
function replay() {
state.time = 0;
state.playing = true;
state.collided = null;
playButton.textContent = "Pause";
updateModel();
}
playButton.addEventListener("click", togglePlayback);
replayButton.addEventListener("click", replay);
window.addEventListener("keydown", (event) => {
if (event.code === "Space") {
event.preventDefault();
togglePlayback();
}
if (event.key.toLowerCase() === "r") {
replay();
}
});
updateModel();
const clock = new THREE.Clock();
renderer.setAnimationLoop(() => {
const delta = Math.min(clock.getDelta(), 0.05);
if (state.playing) {
state.time = Math.min(
state.time + delta,
MODEL.maxTime
);
if (state.time >= MODEL.maxTime) {
state.playing = false;
playButton.textContent = "Play";
}
}
updateModel();
renderer.render(scene, camera);
});
</script>
</body>
</html>
```