Prompt specification
Build or customize the Three.js 3D scene "Two Character Discs Approaching a Gap" 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 Character Discs Approaching a Gap
Category: Games & Play · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/two-character-discs-approaching-a-gap-x3713
Scene Description Prompt:
Two stylized character discs approach a narrow gap; visible collision radii determine whether both fit. Three.js from cdnjs, one HTML file.
Working Three.js Starter (two-character-discs-approaching-a-gap-x3713):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Two-Disc Gap Test</title>
<style>
:root {
color-scheme: dark;
--bg: #07111f;
--panel: rgba(10, 22, 38, 0.88);
--border: rgba(159, 210, 255, 0.18);
--text: #eef8ff;
--muted: #91a9bf;
--cyan: #58e7ff;
--pink: #ff73bd;
--green: #72f5b5;
--red: #ff657a;
}
* {
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;
}
canvas {
display: block;
touch-action: none;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
#hud {
position: fixed;
inset: 0;
pointer-events: none;
}
.brand {
position: absolute;
top: 24px;
left: 26px;
max-width: 390px;
text-shadow: 0 2px 20px rgba(0, 0, 0, 0.7);
}
.eyebrow {
margin-bottom: 7px;
color: var(--cyan);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(23px, 3vw, 38px);
line-height: 1;
letter-spacing: -0.045em;
}
.subtitle {
margin-top: 10px;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
#status {
position: absolute;
top: 24px;
right: 26px;
min-width: 245px;
padding: 16px 18px;
border: 1px solid var(--border);
border-radius: 18px;
background: var(--panel);
box-shadow: 0 18px 60px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(16px);
}
#status[data-mode="good"] {
border-color: rgba(114, 245, 181, 0.4);
}
#status[data-mode="bad"] {
border-color: rgba(255, 101, 122, 0.42);
}
#status[data-mode="running"] {
border-color: rgba(88, 231, 255, 0.42);
}
#verdict {
font-size: 17px;
font-weight: 850;
letter-spacing: -0.02em;
}
#status[data-mode="good"] #verdict {
color: var(--green);
}
#status[data-mode="bad"] #verdict {
color: var(--red);
}
#status[data-mode="running"] #verdict {
color: var(--cyan);
}
#detail {
margin-top: 6px;
color: var(--muted);
font-size: 12px;
line-height: 1.45;
}
.results {
display: flex;
gap: 8px;
margin-top: 13px;
}
.result {
flex: 1;
padding: 8px 9px;
border: 1px solid rgba(255, 255, 255, 0.08);
border-radius: 10px;
color: var(--muted);
background: rgba(255, 255, 255, 0.035);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.result.good {
color: var(--green);
}
.result.bad {
color: var(--red);
}
.result.waiting {
color: var(--cyan);
}
#controls {
position: absolute;
bottom: 24px;
left: 50%;
width: min(850px, calc(100% - 32px));
padding: 18px;
transform: translateX(-50%);
border: 1px solid var(--border);
border-radius: 21px;
background: var(--panel);
box-shadow: 0 22px 80px rgba(0, 0, 0, 0.38);
backdrop-filter: blur(18px);
pointer-events: auto;
}
.control-grid {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
gap: 18px;
}
.control label {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 12px;
margin-bottom: 9px;
color: #c9d9e8;
font-size: 12px;
font-weight: 750;
}
.control output {
color: var(--text);
font-variant-numeric: tabular-nums;
}
input[type="range"] {
width: 100%;
height: 5px;
margin: 7px 0;
appearance: none;
border-radius: 999px;
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%
);
outline: none;
}
input[type="range"]::-webkit-slider-thumb {
width: 17px;
height: 17px;
appearance: none;
border: 3px solid #081421;
border-radius: 50%;
background: var(--accent);
box-shadow: 0 0 0 1px var(--accent), 0 0 18px var(--accent);
cursor: pointer;
}
input[type="range"]::-moz-range-thumb {
width: 12px;
height: 12px;
border: 3px solid #081421;
border-radius: 50%;
background: var(--accent);
box-shadow: 0 0 0 1px var(--accent), 0 0 18px var(--accent);
cursor: pointer;
}
.toolbar {
display: flex;
align-items: center;
gap: 10px;
margin-top: 16px;
padding-top: 15px;
border-top: 1px solid rgba(255, 255, 255, 0.08);
}
button {
min-height: 40px;
padding: 0 17px;
border: 0;
border-radius: 11px;
color: #04111c;
background: var(--cyan);
box-shadow: 0 7px 24px rgba(88, 231, 255, 0.2);
font: inherit;
font-size: 12px;
font-weight: 850;
letter-spacing: 0.04em;
text-transform: uppercase;
cursor: pointer;
}
button:hover {
filter: brightness(1.08);
transform: translateY(-1px);
}
button.secondary {
color: #d7e7f5;
background: rgba(255, 255, 255, 0.08);
box-shadow: none;
}
.hint {
margin-left: auto;
color: var(--muted);
font-size: 11px;
text-align: right;
}
.ring-key {
display: inline-flex;
align-items: center;
gap: 7px;
margin-right: 15px;
}
.ring-key::before {
width: 13px;
height: 13px;
border: 2px solid var(--cyan);
border-radius: 50%;
content: "";
box-shadow: 0 0 8px rgba(88, 231, 255, 0.45);
}
@media (max-width: 720px) {
.brand {
top: 16px;
left: 16px;
max-width: calc(100% - 32px);
}
.subtitle {
display: none;
}
#status {
top: 82px;
right: 16px;
min-width: 205px;
padding: 12px 14px;
}
#controls {
bottom: 12px;
width: calc(100% - 24px);
padding: 14px;
}
.control-grid {
grid-template-columns: 1fr;
gap: 11px;
}
.toolbar {
flex-wrap: wrap;
}
.hint {
width: 100%;
margin: 3px 0 0;
text-align: left;
}
}
</style>
</head>
<body>
<div id="hud">
<header class="brand">
<div class="eyebrow">Collision Geometry / 02</div>
<h1>Two-Disc Gap Test</h1>
<div class="subtitle">
Each glowing circle is the disc’s true collision radius.
A disc clears the opening when its diameter is no wider than the gap.
</div>
</header>
<section id="status" data-mode="ready">
<div id="verdict">READY — BOTH FIT</div>
<div id="detail">Adjust the dimensions, then run the test.</div>
<div class="results">
<div id="resultA" class="result waiting">A · Waiting</div>
<div id="resultB" class="result waiting">B · Waiting</div>
</div>
</section>
<section id="controls">
<div class="control-grid">
<div class="control" style="--accent: #72f5b5">
<label for="gap">
Gap width
<output id="gapValue">2.40</output>
</label>
<input id="gap" type="range" min="1.20" max="4.00" step="0.05" value="2.40">
</div>
<div class="control" style="--accent: #58e7ff">
<label for="radiusA">
Disc A radius
<output id="radiusAValue">0.70</output>
</label>
<input id="radiusA" type="range" min="0.35" max="1.20" step="0.05" value="0.70">
</div>
<div class="control" style="--accent: #ff73bd">
<label for="radiusB">
Disc B radius
<output id="radiusBValue">0.90</output>
</label>
<input id="radiusB" type="range" min="0.35" max="1.20" step="0.05" value="0.90">
</div>
</div>
<div class="toolbar">
<button id="run" type="button">Run test</button>
<button id="reset" class="secondary" type="button">Reset</button>
<div class="hint">
<span class="ring-key">Collision boundary</span>
Drag to orbit · Wheel to zoom · Space to run/pause
</div>
</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 and scene
// -------------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.15;
document.body.prepend(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07111f);
scene.fog = new THREE.FogExp2(0x07111f, 0.026);
const camera = new THREE.PerspectiveCamera(
43,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraTarget = new THREE.Vector3(0, 0.45, 0);
let cameraTheta = 0.72;
let cameraPhi = 0.78;
let cameraRadius = 16;
function updateCamera() {
const sinPhi = Math.sin(cameraPhi);
camera.position.set(
cameraTarget.x + cameraRadius * sinPhi * Math.sin(cameraTheta),
cameraTarget.y + cameraRadius * Math.cos(cameraPhi),
cameraTarget.z + cameraRadius * sinPhi * Math.cos(cameraTheta)
);
camera.lookAt(cameraTarget);
}
updateCamera();
const hemi = new THREE.HemisphereLight(0xc8ecff, 0x142033, 2.1);
scene.add(hemi);
const keyLight = new THREE.DirectionalLight(0xe9f7ff, 3.2);
keyLight.position.set(6, 12, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -12;
keyLight.shadow.camera.right = 12;
keyLight.shadow.camera.top = 12;
keyLight.shadow.camera.bottom = -12;
keyLight.shadow.bias = -0.0004;
scene.add(keyLight);
const cyanLight = new THREE.PointLight(0x39d9ff, 24, 13, 2);
cyanLight.position.set(-4, 2.5, 2);
scene.add(cyanLight);
const pinkLight = new THREE.PointLight(0xff4ca6, 24, 13, 2);
pinkLight.position.set(4, 2.5, -2);
scene.add(pinkLight);
// -------------------------------------------------------------------------
// Environment
// -------------------------------------------------------------------------
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(32, 26),
new THREE.MeshStandardMaterial({
color: 0x0b1928,
roughness: 0.82,
metalness: 0.16
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.025;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(28, 28, 0x31506a, 0x172a3b);
grid.position.y = -0.005;
grid.material.transparent = true;
grid.material.opacity = 0.55;
scene.add(grid);
const route = new THREE.Mesh(
new THREE.PlaneGeometry(12.8, 0.12),
new THREE.MeshBasicMaterial({
color: 0x4adff5,
transparent: true,
opacity: 0.17,
depthWrite: false
})
);
route.rotation.x = -Math.PI / 2;
route.position.set(0, 0.006, 0);
scene.add(route);
function makeArrow(x, direction, color) {
const arrow = new THREE.Mesh(
new THREE.ConeGeometry(0.14, 0.42, 3),
new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: 0.55
})
);
arrow.rotation.z = direction > 0 ? -Math.PI / 2 : Math.PI / 2;
arrow.position.set(x, 0.035, 0);
scene.add(arrow);
}
makeArrow(-3.3, 1, 0x58e7ff);
makeArrow(3.3, -1, 0xff73bd);
const dustGeometry = new THREE.BufferGeometry();
const dustPositions = new Float32Array(210 * 3);
for (let i = 0; i < 210; i++) {
dustPositions[i * 3] = (Math.random() - 0.5) * 27;
dustPositions[i * 3 + 1] = Math.random() * 7 + 0.3;
dustPositions[i * 3 + 2] = (Math.random() - 0.5) * 22;
}
dustGeometry.setAttribute(
"position",
new THREE.BufferAttribute(dustPositions, 3)
);
const dust = new THREE.Points(
dustGeometry,
new THREE.PointsMaterial({
color: 0x9adfff,
size: 0.025,
transparent: true,
opacity: 0.42,
depthWrite: false
})
);
scene.add(dust);
// -------------------------------------------------------------------------
// UI state
// -------------------------------------------------------------------------
const gapInput = document.querySelector("#gap");
const radiusAInput = document.querySelector("#radiusA");
const radiusBInput = document.querySelector("#radiusB");
const gapValue = document.querySelector("#gapValue");
const radiusAValue = document.querySelector("#radiusAValue");
const radiusBValue = document.querySelector("#radiusBValue");
const runButton = document.querySelector("#run");
const resetButton = document.querySelector("#reset");
const statusBox = document.querySelector("#status");
const verdict = document.querySelector("#verdict");
const detail = document.querySelector("#detail");
const resultA = document.querySelector("#resultA");
const resultB = document.querySelector("#resultB");
let gap = Number(gapInput.value);
let radiusA = Number(radiusAInput.value);
let radiusB = Number(radiusBInput.value);
let phase = "ready";
let simulationTime = 0;
let discs = [];
const WALL_THICKNESS = 0.56;
const WALL_LENGTH = 11.6;
const WALL_HEIGHT = 1.55;
const TRAVEL_SPEED = 2.55;
const wallLayer = new THREE.Group();
const characterLayer = new THREE.Group();
const gaugeLayer = new THREE.Group();
scene.add(wallLayer, characterLayer, gaugeLayer);
// -------------------------------------------------------------------------
// Helpers
// -------------------------------------------------------------------------
function disposeObject(object) {
object.traverse((child) => {
if (child.geometry) child.geometry.dispose();
if (child.material) {
const materials = Array.isArray(child.material)
? child.material
: [child.material];
for (const material of materials) {
if (material.map) material.map.dispose();
material.dispose();
}
}
});
object.removeFromParent();
}
function roundedRect(context, x, y, width, height, radius) {
context.beginPath();
context.roundRect(x, y, width, height, radius);
context.closePath();
}
function makeLabel(text, color) {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 192;
const context = canvas.getContext("2d");
context.clearRect(0, 0, canvas.width, canvas.height);
roundedRect(context, 12, 18, 488, 150, 50);
context.fillStyle = "rgba(5, 16, 29, 0.88)";
context.fill();
context.lineWidth = 7;
context.strokeStyle = color;
context.stroke();
context.fillStyle = color;
context.font = "800 66px system-ui, sans-serif";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(text, 256, 96);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(1.75, 0.66, 1);
sprite.renderOrder = 20;
return sprite;
}
function updateRangeFill(input) {
const percent =
((Number(input.value) - Number(input.min)) /
(Number(input.max) - Number(input.min))) *
100;
input.style.setProperty("--fill", `${percent}%`);
}
function updateControlReadouts() {
gapValue.value = gap.toFixed(2);
radiusAValue.value = radiusA.toFixed(2);
radiusBValue.value = radiusB.toFixed(2);
updateRangeFill(gapInput);
updateRangeFill(radiusAInput);
updateRangeFill(radiusBInput);
}
// -------------------------------------------------------------------------
// Wall and dimension gauge
// -------------------------------------------------------------------------
function buildWall() {
while (wallLayer.children.length) {
disposeObject(wallLayer.children[0]);
}
const segmentLength = (WALL_LENGTH - gap) / 2;
const wallMaterial = new THREE.MeshStandardMaterial({
color: 0x15273a,
roughness: 0.62,
metalness: 0.34
});
const capMaterial = new THREE.MeshStandardMaterial({
color: 0x203b52,
roughness: 0.46,
metalness: 0.42
});
const edgeMaterial = new THREE.MeshStandardMaterial({
color: 0x76f6c2,
emissive: 0x1acb83,
emissiveIntensity: 1.6,
roughness: 0.35
});
for (const side of [-1, 1]) {
const segment = new THREE.Mesh(
new THREE.BoxGeometry(
WALL_THICKNESS,
WALL_HEIGHT,
segmentLength
),
wallMaterial
);
segment.position.z = side * (gap / 2 + segmentLength / 2);
segment.castShadow = true;
segment.receiveShadow = true;
wallLayer.add(segment);
const cap = new THREE.Mesh(
new THREE.BoxGeometry(
WALL_THICKNESS + 0.13,
0.12,
segmentLength
),
capMaterial
);
cap.position.set(
0,
WALL_HEIGHT / 2 + 0.06,
segment.position.z
);
cap.castShadow = true;
wallLayer.add(cap);
// Fine luminous seam details.
for (let z = -WALL_LENGTH / 2 + 0.7; z < WALL_LENGTH / 2; z += 1.05) {
if (
(side < 0 && z < -gap / 2 - 0.18) ||
(side > 0 && z > gap / 2 + 0.18)
) {
const seam = new THREE.Mesh(
new THREE.BoxGeometry(0.575, 0.025, 0.025),
edgeMaterial
);
seam.position.set(0, WALL_HEIGHT / 2 + 0.127, z);
wallLayer.add(seam);
}
}
}
const edgeGeometry = new THREE.BoxGeometry(
WALL_THICKNESS + 0.14,
WALL_HEIGHT + 0.12,
0.075
);
for (const side of [-1, 1]) {
const edge = new THREE.Mesh(edgeGeometry, edgeMaterial);
edge.position.set(0, WALL_HEIGHT / 2 + 0.02, side * gap / 2);
edge.castShadow = true;
wallLayer.add(edge);
}
const threshold = new THREE.Mesh(
new THREE.PlaneGeometry(1.4, gap),
new THREE.MeshBasicMaterial({
color: 0x65f4bb,
transparent: true,
opacity: 0.12,
side: THREE.DoubleSide,
depthWrite: false
})
);
threshold.rotation.x = -Math.PI / 2;
threshold.position.set(0, 0.009, 0);
wallLayer.add(threshold);
}
function buildGauge() {
while (gaugeLayer.children.length) {
disposeObject(gaugeLayer.children[0]);
}
const x = -1.2;
const y = 0.055;
const geometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(x, y, -gap / 2),
new THREE.Vector3(x, y, gap / 2)
]);
const line = new THREE.Line(
geometry,
new THREE.LineBasicMaterial({
color: 0x76f5bb,
transparent: true,
opacity: 0.95
})
);
gaugeLayer.add(line);
for (const side of [-1, 1]) {
const tick = new THREE.Line(
new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(x - 0.2, y, side * gap / 2),
new THREE.Vector3(x + 0.2, y, side * gap / 2)
]),
new THREE.LineBasicMaterial({ color: 0x76f5bb })
);
gaugeLayer.add(tick);
}
const label = makeLabel(`GAP ${gap.toFixed(2)}`, "#76f5bb");
label.position.set(x - 0.05, 1.85, 0);
label.scale.set(1.95, 0.73, 1);
gaugeLayer.add(label);
}
// -------------------------------------------------------------------------
// Stylized character discs
// -------------------------------------------------------------------------
function createDisc({
id,
radius,
color,
direction,
startX,
delay
}) {
const root = new THREE.Group();
const bodyMaterial = new THREE.MeshStandardMaterial({
color,
emissive: color,
emissiveIntensity: 0.11,
roughness: 0.3,
metalness: 0.18
});
const topMaterial = new THREE.MeshStandardMaterial({
color: new THREE.Color(color).lerp(new THREE.Color(0xffffff), 0.26),
roughness: 0.34,
metalness: 0.08
});
const body = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, 0.64, 64),
bodyMaterial
);
body.position.y = 0.34;
body.castShadow = true;
body.receiveShadow = true;
root.add(body);
const top = new THREE.Mesh(
new THREE.CylinderGeometry(
radius * 0.82,
radius * 0.87,
0.065,
48
),
topMaterial
);
top.position.y = 0.685;
top.castShadow = true;
root.add(top);
const rim = new THREE.Mesh(
new THREE.TorusGeometry(radius * 0.92, radius * 0.035, 10, 64),
new THREE.MeshStandardMaterial({
color: 0xf4fbff,
emissive: color,
emissiveIntensity: 0.3,
roughness: 0.25,
metalness: 0.25
})
);
rim.rotation.x = Math.PI / 2;
rim.position.y = 0.71;
root.add(rim);
const eyeMaterial = new THREE.MeshStandardMaterial({
color: 0x07111d,
roughness: 0.4
});
for (const side of [-1, 1]) {
const eye = new THREE.Mesh(
new THREE.SphereGeometry(radius * 0.105, 16, 12),
eyeMaterial
);
eye.scale.y = 0.38;
eye.position.set(
direction * radius * 0.34,
0.755,
side * radius * 0.19
);
root.add(eye);
const cheek = new THREE.Mesh(
new THREE.SphereGeometry(radius * 0.055, 12, 8),
new THREE.MeshBasicMaterial({ color: 0xff9bc9 })
);
cheek.scale.y = 0.25;
cheek.position.set(
direction * radius * 0.43,
0.75,
side * radius * 0.34
);
root.add(cheek);
}
const mouth = new THREE.Mesh(
new THREE.BoxGeometry(radius * 0.055, 0.018, radius * 0.22),
eyeMaterial
);
mouth.position.set(direction * radius * 0.49, 0.75, 0);
root.add(mouth);
const antenna = new THREE.Mesh(
new THREE.CylinderGeometry(radius * 0.022, radius * 0.022, 0.28, 8),
new THREE.MeshStandardMaterial({
color: 0xeef8ff,
metalness: 0.5,
roughness: 0.25
})
);
antenna.position.set(-direction * radius * 0.08, 0.91, 0);
root.add(antenna);
const antennaTip = new THREE.Mesh(
new THREE.SphereGeometry(radius * 0.055, 12, 10),
new THREE.MeshBasicMaterial({ color })
);
antennaTip.position.set(
-direction * radius * 0.08,
1.06,
0
);
root.add(antennaTip);
// Exact collision-radius outline on the floor.
const collisionMaterial = new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: 0.9,
side: THREE.DoubleSide,
depthWrite: false
});
const collisionRing = new THREE.Mesh(
new THREE.RingGeometry(
Math.max(0.01, radius - 0.025),
radius + 0.025,
96
),
collisionMaterial
);
collisionRing.rotation.x = -Math.PI / 2;
collisionRing.position.y = 0.025;
collisionRing.renderOrder = 4;
root.add(collisionRing);
const impactMaterial = new THREE.MeshBasicMaterial({
color: 0xff657a,
transparent: true,
opacity: 0,
side: THREE.DoubleSide,
depthWrite: false
});
const impactRing = new THREE.Mesh(
new THREE.RingGeometry(radius * 0.76, radius * 1.12, 64),
impactMaterial
);
impactRing.rotation.x = -Math.PI / 2;
impactRing.position.y = 0.035;
impactRing.visible = false;
root.add(impactRing);
const label = makeLabel(
`${id} · Ø ${(radius * 2).toFixed(2)}`,
id === "A" ? "#58e7ff" : "#ff73bd"
);
label.position.set(0, 1.65, 0);
root.add(label);
root.position.x = startX;
characterLayer.add(root);
return {
id,
radius,
color,
direction,
startX,
targetX: -startX + direction * 0.18,
delay,
root,
collisionRing,
collisionMaterial,
impactRing,
impactMaterial,
fit: radius * 2 <= gap + 0.0001,
passed: false,
blocked: false,
collisionTriggered: false,
impactAmount: 0
};
}
function buildCharacters() {
while (characterLayer.children.length) {
disposeObject(characterLayer.children[0]);
}
discs = [
createDisc({
id: "A",
radius: radiusA,
color: 0x43d9f4,
direction: 1,
startX: -5.6,
delay: 0
}),
createDisc({
id: "B",
radius: radiusB,
color: 0xf45ca8,
direction: -1,
startX: 5.6,
delay: 1.35
})
];
}
// -------------------------------------------------------------------------
// Simulation and result display
// -------------------------------------------------------------------------
function setResult(element, text, mode) {
element.textContent = text;
element.className = `result ${mode}`;
}
function updateVerdict() {
const fitsA = radiusA * 2 <= gap + 0.0001;
const fitsB = radiusB * 2 <= gap + 0.0001;
if (phase === "ready") {
const both = fitsA && fitsB;
const neither = !fitsA && !fitsB;
statusBox.dataset.mode = both ? "good" : neither ? "bad" : "running";
verdict.textContent = both
? "READY — BOTH FIT"
: neither
? "READY — BOTH TOO WIDE"
: "READY — ONE TOO WIDE";
detail.textContent =
`Gap ${gap.toFixed(2)} · A Ø ${(radiusA * 2).toFixed(2)}` +
` · B Ø ${(radiusB * 2).toFixed(2)}`;
setResult(resultA, `A · ${fitsA ? "Fits" : "Too wide"}`, fitsA ? "good" : "bad");
setResult(resultB, `B · ${fitsB ? "Fits" : "Too wide"}`, fitsB ? "good" : "bad");
return;
}
if (phase === "done") {
const passedCount = discs.filter((disc) => disc.passed).length;
statusBox.dataset.mode = passedCount === 2 ? "good" : "bad";
verdict.textContent =
passedCount === 2
? "COMPLETE — BOTH FIT"
: passedCount === 1
? "COMPLETE — ONLY ONE FITS"
: "COMPLETE — BOTH BLOCKED";
detail.textContent =
`A diameter ${(radiusA * 2).toFixed(2)} · ` +
`B diameter ${(radiusB * 2).toFixed(2)} · ` +
`Gap ${gap.toFixed(2)}`;
for (const disc of discs) {
const element = disc.id === "A" ? resultA : resultB;
setResult(
element,
`${disc.id} · ${disc.passed ? "Passed" : "Blocked"}`,
disc.passed ? "good" : "bad"
);
}
return;
}
statusBox.dataset.mode = "running";
verdict.textContent = phase === "paused" ? "PAUSED" : "TESTING — IN MOTION";
detail.textContent = "The wall uses each disc’s visible collision boundary.";
for (const disc of discs) {
const element = disc.id === "A" ? resultA : resultB;
if (!disc.fit) {
setResult(element, `${disc.id} · Blocked`, "bad");
} else if (disc.passed) {
setResult(element, `${disc.id} · Passed`, "good");
} else if (simulationTime < disc.delay) {
setResult(element, `${disc.id} · Waiting`, "waiting");
} else {
setResult(element, `${disc.id} · Moving`, "waiting");
}
}
}
function resetSimulation() {
phase = "ready";
simulationTime = 0;
runButton.textContent = "Run test";
for (const disc of discs) {
disc.root.position.set(disc.startX, 0, 0);
disc.passed = false;
disc.blocked = false;
disc.collisionTriggered = false;
disc.impactAmount = 0;
disc.impactRing.visible = false;
disc.impactMaterial.opacity = 0;
disc.collisionMaterial.color.set(disc.color);
}
updateVerdict();
}
function updateDisc(disc, deltaTime) {
const localTime = simulationTime - disc.delay;
if (localTime < 0) {
disc.root.position.x = disc.startX;
return;
}
if (disc.fit) {
disc.root.position.x =
disc.startX + disc.direction * TRAVEL_SPEED * localTime;
if (
!disc.passed &&
Math.abs(disc.root.position.x) > WALL_THICKNESS / 2 + disc.radius
) {
disc.passed = true;
}
if (disc.direction > 0) {
disc.root.position.x = Math.min(disc.root.position.x, disc.targetX);
} else {
disc.root.position.x = Math.max(disc.root.position.x, disc.targetX);
}
} else {
const contactX =
-disc.direction * (WALL_THICKNESS / 2 + disc.radius);
const travelDistance = Math.abs(contactX - disc.startX);
const travelTime = travelDistance / TRAVEL_SPEED;
if (localTime <= travelTime) {
disc.root.position.x =
disc.startX + disc.direction * TRAVEL_SPEED * localTime;
} else {
disc.blocked = true;
const bounceAmount = Math.min(
0.62,
(localTime - travelTime) * 1.55
);
disc.root.position.x =
contactX - disc.direction * bounceAmount;
if (!disc.collisionTriggered) {
disc.collisionTriggered = true;
disc.impactAmount = 1;
disc.impactRing.visible = true;
}
}
}
if (disc.impactAmount > 0) {
disc.impactAmount = Math.max(0, disc.impactAmount - deltaTime * 1.8);
disc.impactMaterial.opacity = disc.impactAmount * 0.75;
const scale = 1 + (1 - disc.impactAmount) * 0.45;
disc.impactRing.scale.setScalar(scale);
}
// Small idle animation keeps the characters alive.
disc.root.position.y =
Math.sin(simulationTime * 4 + disc.delay) * 0.012;
}
function simulationFinished() {
return discs.every((disc) => {
if (disc.fit) {
return disc.passed;
}
const localTime = simulationTime - disc.delay;
const contactX =
-disc.direction * (WALL_THICKNESS / 2 + disc.radius);
const travelTime =
Math.abs(contactX - disc.startX) / TRAVEL_SPEED;
return localTime > travelTime + 0.55;
});
}
function toggleRun() {
if (phase === "running") {
phase = "paused";
runButton.textContent = "Resume";
} else if (phase === "paused") {
phase = "running";
runButton.textContent = "Pause";
} else {
resetSimulation();
phase = "running";
runButton.textContent = "Pause";
}
updateVerdict();
}
// -------------------------------------------------------------------------
// Build and input
// -------------------------------------------------------------------------
function rebuild() {
gap = Number(gapInput.value);
radiusA = Number(radiusAInput.value);
radiusB = Number(radiusBInput.value);
updateControlReadouts();
buildWall();
buildGauge();
buildCharacters();
resetSimulation();
}
gapInput.addEventListener("input", rebuild);
radiusAInput.addEventListener("input", rebuild);
radiusBInput.addEventListener("input", rebuild);
runButton.addEventListener("click", toggleRun);
resetButton.addEventListener("click", resetSimulation);
window.addEventListener("keydown", (event) => {
const tag = event.target.tagName;
if (
event.code === "Space" &&
tag !== "INPUT" &&
tag !== "BUTTON" &&
tag !== "TEXTAREA"
) {
event.preventDefault();
toggleRun();
}
if (event.key.toLowerCase() === "r" && tag !== "INPUT") {
resetSimulation();
}
});
// Custom camera controls: no examples/addons modules are used.
let dragging = false;
let previousPointerX = 0;
let previousPointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
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;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
cameraTheta -= deltaX * 0.006;
cameraPhi = THREE.MathUtils.clamp(
cameraPhi + deltaY * 0.005,
0.32,
1.38
);
updateCamera();
});
function endDrag(event) {
dragging = false;
if (
event.pointerId !== undefined &&
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 * Math.exp(event.deltaY * 0.001),
9,
25
);
updateCamera();
},
{ passive: false }
);
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
rebuild();
// -------------------------------------------------------------------------
// Animation loop
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const deltaTime = Math.min(clock.getDelta(), 0.05);
if (phase === "running") {
simulationTime += deltaTime;
for (const disc of discs) {
updateDisc(disc, deltaTime);
}
if (simulationFinished()) {
phase = "done";
runButton.textContent = "Run again";
}
updateVerdict();
}
dust.rotation.y += deltaTime * 0.012;
dust.position.y = Math.sin(clock.elapsedTime * 0.18) * 0.08;
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```