Prompt specification
Build or customize the Three.js 3D scene "Cooperative Carry Path Planner" 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: Cooperative Carry Path Planner
Category: Games & Play · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/cooperative-carry-path-planner-x3712
Scene Description Prompt:
Guide two low-poly characters carrying a rigid plank around obstacles, checking clearance for both people and plank. Three.js from cdnjs, standalone HTML.
Working Three.js Starter (cooperative-carry-path-planner-x3712):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Rigid Plank Clearance Demo</title>
<style>
:root {
color-scheme: dark;
--panel: rgba(15, 23, 34, 0.88);
--border: rgba(255, 255, 255, 0.12);
--text: #edf4ff;
--muted: #9aacc1;
--safe: #57e39b;
--tight: #ffd166;
--blocked: #ff647c;
--blue: #65a9ff;
--orange: #ff9b62;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #101722;
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--text);
}
canvas {
display: block;
touch-action: none;
}
button {
font: inherit;
}
.panel {
background: var(--panel);
border: 1px solid var(--border);
border-radius: 14px;
box-shadow: 0 16px 42px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(14px);
}
#hud {
position: fixed;
top: 18px;
left: 18px;
width: min(330px, calc(100vw - 36px));
padding: 16px;
pointer-events: none;
z-index: 10;
}
.eyebrow {
margin: 0 0 5px;
color: #8ebaff;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: 19px;
line-height: 1.25;
}
#objective {
margin: 7px 0 14px;
color: var(--muted);
font-size: 13px;
line-height: 1.45;
}
.overall {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 11px;
padding: 9px 11px;
border: 1px solid rgba(87, 227, 155, 0.22);
border-radius: 9px;
background: rgba(87, 227, 155, 0.08);
font-size: 12px;
font-weight: 700;
}
#overallState {
color: var(--safe);
font-size: 11px;
letter-spacing: 0.08em;
}
.status-row {
display: grid;
grid-template-columns: 1fr auto;
gap: 3px 10px;
align-items: center;
padding: 9px 0;
border-top: 1px solid rgba(255, 255, 255, 0.08);
}
.status-name {
display: flex;
align-items: center;
gap: 8px;
font-size: 13px;
font-weight: 750;
}
.status-dot {
width: 9px;
height: 9px;
border-radius: 50%;
background: var(--safe);
box-shadow: 0 0 12px currentColor;
}
.status-value {
font-size: 12px;
font-variant-numeric: tabular-nums;
color: var(--text);
}
.status-note {
grid-column: 1 / -1;
color: var(--muted);
font-size: 10px;
}
.status-tag {
grid-column: 2;
grid-row: 1;
margin-right: 64px;
color: var(--safe);
font-size: 9px;
font-weight: 850;
letter-spacing: 0.08em;
}
.legend {
display: flex;
gap: 12px;
margin-top: 10px;
color: var(--muted);
font-size: 10px;
}
.legend span {
display: flex;
align-items: center;
gap: 5px;
}
.legend i {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--safe);
}
#actions {
position: fixed;
top: 18px;
right: 18px;
display: flex;
gap: 8px;
z-index: 20;
}
.action {
min-width: 42px;
height: 39px;
padding: 0 12px;
color: var(--text);
background: rgba(15, 23, 34, 0.88);
border: 1px solid var(--border);
border-radius: 10px;
cursor: pointer;
font-size: 12px;
font-weight: 750;
box-shadow: 0 10px 24px rgba(0, 0, 0, 0.18);
}
.action:hover {
background: rgba(39, 55, 76, 0.96);
border-color: rgba(255, 255, 255, 0.24);
}
#controls {
position: fixed;
left: 50%;
bottom: 18px;
transform: translateX(-50%);
display: flex;
align-items: center;
gap: 18px;
padding: 11px 15px;
white-space: nowrap;
z-index: 15;
pointer-events: none;
}
.control-group {
display: flex;
align-items: center;
gap: 7px;
color: var(--muted);
font-size: 11px;
}
kbd {
min-width: 26px;
padding: 5px 7px;
color: var(--text);
background: rgba(255, 255, 255, 0.09);
border: 1px solid rgba(255, 255, 255, 0.17);
border-bottom-width: 2px;
border-radius: 6px;
text-align: center;
font: 700 10px ui-monospace, SFMono-Regular, Menlo, monospace;
}
#toast {
position: fixed;
left: 50%;
top: 78px;
transform: translate(-50%, -12px);
padding: 10px 16px;
color: var(--text);
background: rgba(15, 23, 34, 0.94);
border: 1px solid var(--border);
border-radius: 10px;
opacity: 0;
pointer-events: none;
transition: 160ms ease;
font-size: 12px;
font-weight: 700;
z-index: 30;
}
#toast.visible {
opacity: 1;
transform: translate(-50%, 0);
}
#help {
position: fixed;
inset: 0;
display: grid;
place-items: center;
padding: 24px;
background: rgba(4, 8, 13, 0.72);
backdrop-filter: blur(8px);
z-index: 50;
}
#help.hidden {
display: none;
}
.help-card {
width: min(510px, 100%);
padding: 25px;
}
.help-card h2 {
margin: 0 0 8px;
font-size: 22px;
}
.help-card p {
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.help-card ul {
padding-left: 20px;
color: var(--muted);
font-size: 13px;
line-height: 1.7;
}
.help-card strong {
color: var(--text);
}
#closeHelp {
width: 100%;
margin-top: 12px;
}
#touchControls {
position: fixed;
left: 16px;
right: 16px;
bottom: 16px;
display: none;
justify-content: space-between;
pointer-events: none;
z-index: 25;
}
.touch-cluster {
display: grid;
grid-template-columns: repeat(3, 46px);
grid-template-rows: repeat(2, 46px);
gap: 5px;
pointer-events: auto;
}
.touch-button {
color: white;
background: rgba(15, 23, 34, 0.82);
border: 1px solid rgba(255, 255, 255, 0.18);
border-radius: 10px;
font-weight: 800;
touch-action: none;
user-select: none;
}
.touch-button:active {
background: rgba(76, 130, 200, 0.9);
}
.touch-button.up {
grid-column: 2;
}
.touch-button.left {
grid-column: 1;
grid-row: 2;
}
.touch-button.down {
grid-column: 2;
grid-row: 2;
}
.touch-button.right {
grid-column: 3;
grid-row: 2;
}
.rotate-cluster {
display: flex;
align-items: flex-end;
gap: 6px;
pointer-events: auto;
}
@media (pointer: coarse) {
#touchControls {
display: flex;
}
#controls {
display: none;
}
#hud {
top: 10px;
left: 10px;
transform: scale(0.9);
transform-origin: top left;
}
#actions {
top: 10px;
right: 10px;
}
}
@media (max-width: 620px) {
#hud {
width: 275px;
}
#actions {
flex-direction: column;
}
.legend {
display: none;
}
}
</style>
</head>
<body>
<div id="hud" class="panel">
<p class="eyebrow">Clearance Challenge</p>
<h1>Rigid Plank Delivery</h1>
<p id="objective">
Move the complete carrying team to the green drop zone without touching
an obstacle.
</p>
<div class="overall">
<span>Formation clearance</span>
<span id="overallState">SAFE</span>
</div>
<div class="status-row">
<span class="status-name"><i id="dotA" class="status-dot"></i>Carrier A</span>
<span id="tagA" class="status-tag">SAFE</span>
<strong id="valueA" class="status-value">--</strong>
<span id="noteA" class="status-note">Checking clearance…</span>
</div>
<div class="status-row">
<span class="status-name"><i id="dotB" class="status-dot"></i>Carrier B</span>
<span id="tagB" class="status-tag">SAFE</span>
<strong id="valueB" class="status-value">--</strong>
<span id="noteB" class="status-note">Checking clearance…</span>
</div>
<div class="status-row">
<span class="status-name"><i id="dotP" class="status-dot"></i>Rigid plank</span>
<span id="tagP" class="status-tag">SAFE</span>
<strong id="valueP" class="status-value">--</strong>
<span id="noteP" class="status-note">Checking clearance…</span>
</div>
<div class="legend">
<span><i></i>Safe</span>
<span><i style="background: var(--tight)"></i>Tight</span>
<span><i style="background: var(--blocked)"></i>Blocked</span>
</div>
</div>
<div id="actions">
<button id="resetButton" class="action">Reset</button>
<button id="pauseButton" class="action">Pause</button>
<button id="helpButton" class="action">Help</button>
</div>
<div id="controls" class="panel">
<div class="control-group">
<kbd>W</kbd><kbd>A</kbd><kbd>S</kbd><kbd>D</kbd>
<span>Move team</span>
</div>
<div class="control-group">
<kbd>Q</kbd><kbd>E</kbd>
<span>Rotate plank</span>
</div>
<div class="control-group">
<kbd>R</kbd>
<span>Reset</span>
</div>
<div class="control-group">
<span>Drag to orbit · Wheel to zoom</span>
</div>
</div>
<div id="toast"></div>
<div id="touchControls">
<div class="touch-cluster">
<button class="touch-button up" data-key="KeyW">▲</button>
<button class="touch-button left" data-key="KeyA">◀</button>
<button class="touch-button down" data-key="KeyS">▼</button>
<button class="touch-button right" data-key="KeyD">▶</button>
</div>
<div class="rotate-cluster">
<button class="touch-button" data-key="KeyQ">↶</button>
<button class="touch-button" data-key="KeyE">↷</button>
</div>
</div>
<div id="help" class="hidden">
<div class="help-card panel">
<p class="eyebrow">How to play</p>
<h2>Guide the rigid carrying formation</h2>
<p>
The carriers remain fixed at opposite ends of the plank. Translate the
entire formation and rotate it carefully around obstacles.
</p>
<ul>
<li><strong>WASD / arrows:</strong> move relative to the camera.</li>
<li><strong>Q / E:</strong> rotate the plank and both carriers.</li>
<li><strong>R:</strong> reset the challenge.</li>
<li><strong>P:</strong> pause or resume.</li>
<li><strong>C:</strong> toggle clearance footprints.</li>
<li><strong>Mouse drag:</strong> orbit; <strong>wheel:</strong> zoom.</li>
</ul>
<p>
Clearance is evaluated independently for Carrier A, Carrier B, and the
oriented plank. Low objects may pass beneath the plank but still block
the carriers.
</p>
<button id="closeHelp" class="action">Start guiding</button>
</div>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------------
// Constants and scene setup
// ------------------------------------------------------------------
const ARENA_HALF_X = 8.6;
const ARENA_HALF_Z = 6.6;
const CARRIER_RADIUS = 0.31;
const PERSON_HEIGHT = 1.82;
const PLANK_BOTTOM = 0.96;
const PLANK_TOP = 1.11;
const PLANK_HALF_WIDTH = 0.15;
const CARRIER_SPAN = 2.72;
const PLANK_LENGTH = 3.02;
const MOVE_SPEED = 2.05;
const ROTATE_SPEED = 1.22;
const TIGHT_CLEARANCE = 0.22;
const GOAL = { x: 6.15, z: -4.5, radius: 1.08 };
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.setClearColor(0x101722, 1);
document.body.prepend(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x101722);
scene.fog = new THREE.Fog(0x101722, 25, 42);
const camera = new THREE.PerspectiveCamera(
43,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraTarget = new THREE.Vector3(0, 0.2, 0);
let cameraYaw = 0.53;
let cameraElevation = 0.67;
let cameraDistance = 20.5;
const hemisphere = new THREE.HemisphereLight(0xb9d8ff, 0x263126, 2.1);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xffffff, 3.1);
sun.position.set(-7, 13, 8);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -13;
sun.shadow.camera.right = 13;
sun.shadow.camera.top = 11;
sun.shadow.camera.bottom = -11;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 35;
sun.shadow.normalBias = 0.025;
scene.add(sun);
const fill = new THREE.DirectionalLight(0x6d91c8, 1.0);
fill.position.set(8, 5, -10);
scene.add(fill);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(ARENA_HALF_X * 2, ARENA_HALF_Z * 2),
new THREE.MeshStandardMaterial({
color: 0x202c37,
roughness: 0.94,
metalness: 0.03
})
);
floor.rotation.x = -Math.PI / 2;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(18, 18, 0x52667b, 0x344655);
grid.position.y = 0.008;
grid.material.transparent = true;
grid.material.opacity = 0.38;
scene.add(grid);
const boundaryPoints = [
new THREE.Vector3(-ARENA_HALF_X, 0.035, -ARENA_HALF_Z),
new THREE.Vector3(ARENA_HALF_X, 0.035, -ARENA_HALF_Z),
new THREE.Vector3(ARENA_HALF_X, 0.035, ARENA_HALF_Z),
new THREE.Vector3(-ARENA_HALF_X, 0.035, ARENA_HALF_Z),
new THREE.Vector3(-ARENA_HALF_X, 0.035, -ARENA_HALF_Z)
];
scene.add(
new THREE.Line(
new THREE.BufferGeometry().setFromPoints(boundaryPoints),
new THREE.LineBasicMaterial({ color: 0x7891aa })
)
);
const clearanceGroup = new THREE.Group();
scene.add(clearanceGroup);
// ------------------------------------------------------------------
// Obstacles
// ------------------------------------------------------------------
const obstacleSpecs = [
{
name: "Service wall",
x: -0.25,
z: 0.8,
w: 1.1,
d: 5.2,
h: 2.55,
color: 0x596a7e
},
{
name: "Storage crate",
x: 3.65,
z: -2.6,
w: 2.3,
d: 1.8,
h: 1.62,
color: 0xa56d42
},
{
name: "Low bench",
x: -3.65,
z: 2.15,
w: 2.55,
d: 0.76,
h: 0.56,
color: 0xb99549
},
{
name: "Utility column",
x: 4.45,
z: 3.65,
w: 1.4,
d: 1.4,
h: 2.8,
color: 0x56657a
},
{
name: "West column",
x: -4.65,
z: -3.15,
w: 1.25,
d: 1.25,
h: 2.42,
color: 0x5f7185
}
];
const obstacles = [];
function makeFootprint(x, z, w, d) {
const y = 0.018;
const points = [
new THREE.Vector3(x - w / 2, y, z - d / 2),
new THREE.Vector3(x + w / 2, y, z - d / 2),
new THREE.Vector3(x + w / 2, y, z + d / 2),
new THREE.Vector3(x - w / 2, y, z + d / 2),
new THREE.Vector3(x - w / 2, y, z - d / 2)
];
const line = new THREE.Line(
new THREE.BufferGeometry().setFromPoints(points),
new THREE.LineBasicMaterial({
color: 0x7eb4ff,
transparent: true,
opacity: 0.55
})
);
clearanceGroup.add(line);
}
for (const spec of obstacleSpecs) {
const material = new THREE.MeshStandardMaterial({
color: spec.color,
roughness: 0.76,
metalness: 0.12,
flatShading: true,
emissive: 0x000000,
emissiveIntensity: 0.5
});
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(spec.w, spec.h, spec.d),
material
);
mesh.position.set(spec.x, spec.h / 2, spec.z);
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add(mesh);
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(mesh.geometry),
new THREE.LineBasicMaterial({
color: 0xb8c7d8,
transparent: true,
opacity: 0.42
})
);
edges.position.copy(mesh.position);
scene.add(edges);
if (spec.h < 0.8) {
const top = new THREE.Mesh(
new THREE.BoxGeometry(spec.w + 0.08, 0.07, spec.d + 0.08),
new THREE.MeshStandardMaterial({
color: 0xd4b65f,
roughness: 0.65,
flatShading: true
})
);
top.position.set(spec.x, spec.h + 0.015, spec.z);
top.castShadow = true;
scene.add(top);
}
makeFootprint(spec.x, spec.z, spec.w, spec.d);
obstacles.push({ ...spec, mesh, material });
}
// ------------------------------------------------------------------
// Goal marker
// ------------------------------------------------------------------
const goalMaterial = new THREE.MeshStandardMaterial({
color: 0x42d892,
emissive: 0x167e51,
emissiveIntensity: 0.7,
roughness: 0.6,
transparent: true,
opacity: 0.48
});
const goalPad = new THREE.Mesh(
new THREE.CylinderGeometry(GOAL.radius, GOAL.radius, 0.045, 40),
goalMaterial
);
goalPad.position.set(GOAL.x, 0.025, GOAL.z);
goalPad.receiveShadow = true;
scene.add(goalPad);
const goalRing = new THREE.Mesh(
new THREE.TorusGeometry(GOAL.radius, 0.055, 8, 40),
new THREE.MeshStandardMaterial({
color: 0x6effb6,
emissive: 0x1b9b63,
emissiveIntensity: 0.85,
roughness: 0.45
})
);
goalRing.rotation.x = Math.PI / 2;
goalRing.position.set(GOAL.x, 0.065, GOAL.z);
scene.add(goalRing);
function makeTextSprite(text, color = "#ffffff") {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 128;
const ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = "rgba(12, 20, 29, 0.78)";
ctx.fillRect(0, 12, 512, 104);
ctx.strokeStyle = color;
ctx.lineWidth = 5;
ctx.strokeRect(4, 16, 504, 96);
ctx.fillStyle = color;
ctx.font = "bold 48px system-ui";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, 256, 66);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthWrite: false
})
);
sprite.scale.set(2.15, 0.54, 1);
return sprite;
}
const goalLabel = makeTextSprite("DROP ZONE", "#8affc7");
goalLabel.position.set(GOAL.x, 0.42, GOAL.z);
scene.add(goalLabel);
// ------------------------------------------------------------------
// Low-poly carriers
// ------------------------------------------------------------------
function makeCarrier(color, side) {
const root = new THREE.Group();
const body = new THREE.Group();
root.add(body);
const shirt = new THREE.MeshStandardMaterial({
color,
roughness: 0.83,
flatShading: true
});
const trousers = new THREE.MeshStandardMaterial({
color: 0x263447,
roughness: 0.92,
flatShading: true
});
const skin = new THREE.MeshStandardMaterial({
color: 0xd7a276,
roughness: 0.88,
flatShading: true
});
const boots = new THREE.MeshStandardMaterial({
color: 0x111923,
roughness: 0.95,
flatShading: true
});
const torso = new THREE.Mesh(
new THREE.BoxGeometry(0.5, 0.62, 0.32),
shirt
);
torso.position.y = 1.08;
torso.castShadow = true;
body.add(torso);
const belt = new THREE.Mesh(
new THREE.BoxGeometry(0.52, 0.08, 0.34),
trousers
);
belt.position.y = 0.75;
body.add(belt);
const head = new THREE.Mesh(
new THREE.DodecahedronGeometry(0.225, 0),
skin
);
head.position.y = 1.57;
head.castShadow = true;
body.add(head);
const nose = new THREE.Mesh(
new THREE.BoxGeometry(0.08, 0.07, 0.08),
skin
);
nose.position.set(0, 1.55, -0.215);
body.add(nose);
const eyeMaterial = new THREE.MeshBasicMaterial({ color: 0x15202b });
for (const x of [-0.075, 0.075]) {
const eye = new THREE.Mesh(
new THREE.BoxGeometry(0.035, 0.035, 0.02),
eyeMaterial
);
eye.position.set(x, 1.61, -0.215);
body.add(eye);
}
const legs = [];
for (const x of [-0.135, 0.135]) {
const pivot = new THREE.Group();
pivot.position.set(x, 0.72, 0);
const leg = new THREE.Mesh(
new THREE.CylinderGeometry(0.075, 0.095, 0.52, 5),
trousers
);
leg.position.y = -0.25;
leg.castShadow = true;
pivot.add(leg);
const boot = new THREE.Mesh(
new THREE.BoxGeometry(0.17, 0.12, 0.28),
boots
);
boot.position.set(0, -0.52, -0.035);
boot.castShadow = true;
pivot.add(boot);
body.add(pivot);
legs.push(pivot);
}
const arms = [];
for (const shoulderX of [-0.31, 0.31]) {
const pivot = new THREE.Group();
pivot.position.set(shoulderX, 1.31, 0);
const arm = new THREE.Mesh(
new THREE.BoxGeometry(0.12, 0.43, 0.14),
shirt
);
arm.position.y = -0.19;
arm.castShadow = true;
pivot.add(arm);
const hand = new THREE.Mesh(
new THREE.DodecahedronGeometry(0.075, 0),
skin
);
hand.position.y = -0.42;
pivot.add(hand);
body.add(pivot);
arms.push(pivot);
}
// The inner arm reaches toward the plank.
const innerArm = side < 0 ? arms[1] : arms[0];
innerArm.rotation.z = side < 0 ? -0.22 : 0.22;
const ringMaterial = new THREE.MeshBasicMaterial({
color: 0x57e39b,
transparent: true,
opacity: 0.8,
side: THREE.DoubleSide,
depthWrite: false
});
const ring = new THREE.Mesh(
new THREE.RingGeometry(CARRIER_RADIUS + 0.12, CARRIER_RADIUS + 0.19, 32),
ringMaterial
);
ring.rotation.x = -Math.PI / 2;
ring.position.y = 0.025;
clearanceGroup.add(ring);
scene.add(root);
return { root, body, legs, ring, ringMaterial, side };
}
const carrierA = makeCarrier(0x4d9eff, -1);
const carrierB = makeCarrier(0xff765d, 1);
// ------------------------------------------------------------------
// Rigid plank
// ------------------------------------------------------------------
const plankGroup = new THREE.Group();
const wood = new THREE.MeshStandardMaterial({
color: 0xc38a4d,
roughness: 0.8,
flatShading: true
});
const plankMesh = new THREE.Mesh(
new THREE.BoxGeometry(PLANK_LENGTH, 0.15, 0.3),
wood
);
plankMesh.castShadow = true;
plankMesh.receiveShadow = true;
plankGroup.add(plankMesh);
const plankBandMaterial = new THREE.MeshStandardMaterial({
color: 0x70452b,
roughness: 0.88,
flatShading: true
});
for (const x of [-PLANK_LENGTH / 2 + 0.13, PLANK_LENGTH / 2 - 0.13]) {
const band = new THREE.Mesh(
new THREE.BoxGeometry(0.09, 0.17, 0.32),
plankBandMaterial
);
band.position.x = x;
plankGroup.add(band);
}
for (const z of [-0.075, 0.075]) {
const grain = new THREE.Mesh(
new THREE.BoxGeometry(PLANK_LENGTH - 0.26, 0.008, 0.018),
new THREE.MeshBasicMaterial({ color: 0x855a35 })
);
grain.position.set(0, 0.079, z);
plankGroup.add(grain);
}
scene.add(plankGroup);
const plankShellMaterial = new THREE.MeshBasicMaterial({
color: 0x57e39b,
transparent: true,
opacity: 0.08,
depthWrite: false
});
const plankShell = new THREE.Mesh(
new THREE.BoxGeometry(PLANK_LENGTH, 0.2, 0.38),
plankShellMaterial
);
scene.add(plankShell);
const plankEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(
new THREE.BoxGeometry(PLANK_LENGTH, 0.2, 0.38)
),
new THREE.LineBasicMaterial({
color: 0x57e39b,
transparent: true,
opacity: 0.9
})
);
scene.add(plankEdges);
// ------------------------------------------------------------------
// Collision and clearance math
//
// People use a circular horizontal footprint and a vertical body span.
// The plank uses an oriented rectangle and its own vertical span.
// ------------------------------------------------------------------
function clamp(value, min, max) {
return Math.max(min, Math.min(max, value));
}
function personPositions(cx, cz, angle) {
const dx = Math.cos(angle);
const dz = Math.sin(angle);
const half = CARRIER_SPAN / 2;
return [
{ x: cx - dx * half, z: cz - dz * half },
{ x: cx + dx * half, z: cz + dz * half }
];
}
function rectanglePolygon(obstacle) {
const hw = obstacle.w / 2;
const hd = obstacle.d / 2;
return [
{ x: obstacle.x - hw, z: obstacle.z - hd },
{ x: obstacle.x + hw, z: obstacle.z - hd },
{ x: obstacle.x + hw, z: obstacle.z + hd },
{ x: obstacle.x - hw, z: obstacle.z + hd }
];
}
function plankPolygon(cx, cz, angle) {
const dx = Math.cos(angle);
const dz = Math.sin(angle);
const px = -Math.sin(angle);
const pz = Math.cos(angle);
const hl = PLANK_LENGTH / 2;
const hw = PLANK_HALF_WIDTH;
return [
{ x: cx - dx * hl - px * hw, z: cz - dz * hl - pz * hw },
{ x: cx + dx * hl - px * hw, z: cz + dz * hl - pz * hw },
{ x: cx + dx * hl + px * hw, z: cz + dz * hl + pz * hw },
{ x: cx - dx * hl + px * hw, z: cz - dz * hl + pz * hw }
];
}
function pointRectangleSignedDistance(px, pz, obstacle) {
const dx = Math.abs(px - obstacle.x) - obstacle.w / 2;
const dz = Math.abs(pz - obstacle.z) - obstacle.d / 2;
if (dx > 0 || dz > 0) {
return Math.hypot(Math.max(dx, 0), Math.max(dz, 0));
}
return -Math.min(-dx, -dz);
}
function verticalGap(aMin, aMax, bMin, bMax) {
if (aMax < bMin) return bMin - aMax;
if (bMax < aMin) return aMin - bMax;
return 0;
}
function polygonAxes(polygon) {
const axes = [];
for (let i = 0; i < polygon.length; i++) {
const a = polygon[i];
const b = polygon[(i + 1) % polygon.length];
const length = Math.hypot(b.x - a.x, b.z - a.z);
if (length > 1e-8) {
axes.push({
x: -(b.z - a.z) / length,
z: (b.x - a.x) / length
});
}
}
return axes;
}
function projectPolygon(polygon, axis) {
let min = Infinity;
let max = -Infinity;
for (const point of polygon) {
const projection = point.x * axis.x + point.z * axis.z;
min = Math.min(min, projection);
max = Math.max(max, projection);
}
return { min, max };
}
function polygonsOverlap(a, b) {
const axes = [...polygonAxes(a), ...polygonAxes(b)];
for (const axis of axes) {
const pa = projectPolygon(a, axis);
const pb = projectPolygon(b, axis);
if (pa.max < pb.min - 1e-8 || pb.max < pa.min - 1e-8) {
return false;
}
}
return true;
}
function orientation(a, b, c) {
return (
(b.z - a.z) * (c.x - b.x) -
(b.x - a.x) * (c.z - b.z)
);
}
function onSegment(a, b, p) {
return (
Math.min(a.x, b.x) - 1e-8 <= p.x &&
p.x <= Math.max(a.x, b.x) + 1e-8 &&
Math.min(a.z, b.z) - 1e-8 <= p.z &&
p.z <= Math.max(a.z, b.z) + 1e-8
);
}
function segmentsIntersect(a, b, c, d) {
const o1 = orientation(a, b, c);
const o2 = orientation(a, b, d);
const o3 = orientation(c, d, a);
const o4 = orientation(c, d, b);
if (
((o1 > 0 && o2 < 0) || (o1 < 0 && o2 > 0)) &&
((o3 > 0 && o4 < 0) || (o3 < 0 && o4 > 0))
) {
return true;
}
if (Math.abs(o1) < 1e-9 && onSegment(a, b, c)) return true;
if (Math.abs(o2) < 1e-9 && onSegment(a, b, d)) return true;
if (Math.abs(o3) < 1e-9 && onSegment(c, d, a)) return true;
if (Math.abs(o4) < 1e-9 && onSegment(c, d, b)) return true;
return false;
}
function pointSegmentDistance(p, a, b) {
const abx = b.x - a.x;
const abz = b.z - a.z;
const lengthSquared = abx * abx + abz * abz;
if (lengthSquared < 1e-12) {
return Math.hypot(p.x - a.x, p.z - a.z);
}
const t = clamp(
((p.x - a.x) * abx + (p.z - a.z) * abz) / lengthSquared,
0,
1
);
const qx = a.x + abx * t;
const qz = a.z + abz * t;
return Math.hypot(p.x - qx, p.z - qz);
}
function segmentDistance(a, b, c, d) {
if (segmentsIntersect(a, b, c, d)) return 0;
return Math.min(
pointSegmentDistance(a, c, d),
pointSegmentDistance(b, c, d),
pointSegmentDistance(c, a, b),
pointSegmentDistance(d, a, b)
);
}
function polygonDistance(a, b) {
let distance = Infinity;
for (let i = 0; i < a.length; i++) {
const a1 = a[i];
const a2 = a[(i + 1) % a.length];
for (let j = 0; j < b.length; j++) {
const b1 = b[j];
const b2 = b[(j + 1) % b.length];
distance = Math.min(distance, segmentDistance(a1, a2, b1, b2));
}
}
return distance;
}
function personHorizontalGap(px, pz, obstacle) {
return (
pointRectangleSignedDistance(px, pz, obstacle) - CARRIER_RADIUS
);
}
function personHitsObstacle(px, pz, obstacle) {
const horizontal = personHorizontalGap(px, pz, obstacle) < 0;
const vertical =
verticalGap(0, PERSON_HEIGHT, 0, obstacle.h) === 0;
return horizontal && vertical;
}
function plankHitsObstacle(cx, cz, angle, obstacle) {
const horizontal = polygonsOverlap(
plankPolygon(cx, cz, angle),
rectanglePolygon(obstacle)
);
const vertical =
verticalGap(PLANK_BOTTOM, PLANK_TOP, 0, obstacle.h) === 0;
return horizontal && vertical;
}
function poseHitsBoundary(cx, cz, angle) {
for (const person of personPositions(cx, cz, angle)) {
if (
Math.abs(person.x) + CARRIER_RADIUS > ARENA_HALF_X ||
Math.abs(person.z) + CARRIER_RADIUS > ARENA_HALF_Z
) {
return true;
}
}
for (const point of plankPolygon(cx, cz, angle)) {
if (
Math.abs(point.x) > ARENA_HALF_X ||
Math.abs(point.z) > ARENA_HALF_Z
) {
return true;
}
}
return false;
}
function poseIsClear(cx, cz, angle) {
if (poseHitsBoundary(cx, cz, angle)) return false;
for (const person of personPositions(cx, cz, angle)) {
for (const obstacle of obstacles) {
if (personHitsObstacle(person.x, person.z, obstacle)) {
return false;
}
}
}
for (const obstacle of obstacles) {
if (plankHitsObstacle(cx, cz, angle, obstacle)) {
return false;
}
}
return true;
}
function evaluateClearance(cx, cz, angle) {
const people = personPositions(cx, cz, angle);
const results = [
{ min: Infinity, name: "Arena edge", collision: false },
{ min: Infinity, name: "Arena edge", collision: false },
{ min: Infinity, name: "Arena edge", collision: false }
];
const boundaryPeople = people.map((person) =>
Math.min(
ARENA_HALF_X - Math.abs(person.x) - CARRIER_RADIUS,
ARENA_HALF_Z - Math.abs(person.z) - CARRIER_RADIUS
)
);
results[0].min = boundaryPeople[0];
results[1].min = boundaryPeople[1];
const plankCorners = plankPolygon(cx, cz, angle);
let plankBoundary = Infinity;
for (const point of plankCorners) {
plankBoundary = Math.min(
plankBoundary,
ARENA_HALF_X - Math.abs(point.x),
ARENA_HALF_Z - Math.abs(point.z)
);
}
results[2].min = plankBoundary;
for (const obstacle of obstacles) {
for (let index = 0; index < 2; index++) {
const person = people[index];
const horizontal = personHorizontalGap(
person.x,
person.z,
obstacle
);
const vertical = verticalGap(0, PERSON_HEIGHT, 0, obstacle.h);
const collision = horizontal < 0 && vertical === 0;
let clearance;
if (collision) {
clearance = Math.min(horizontal, -0.01);
} else if (horizontal < 0) {
clearance = vertical;
} else if (vertical === 0) {
clearance = horizontal;
} else {
clearance = Math.hypot(horizontal, vertical);
}
if (clearance < results[index].min) {
results[index].min = clearance;
results[index].name = obstacle.name;
}
if (collision) {
results[index].collision = true;
}
}
const obstacleRectangle = rectanglePolygon(obstacle);
const orientedPlank = plankPolygon(cx, cz, angle);
const overlaps = polygonsOverlap(orientedPlank, obstacleRectangle);
const horizontalDistance = overlaps
? 0
: polygonDistance(orientedPlank, obstacleRectangle);
const vertical = verticalGap(
PLANK_BOTTOM,
PLANK_TOP,
0,
obstacle.h
);
const collision = overlaps && vertical === 0;
let clearance;
if (collision) {
clearance = -0.01;
} else if (overlaps) {
clearance = vertical;
} else if (vertical === 0) {
clearance = horizontalDistance;
} else {
clearance = Math.hypot(horizontalDistance, vertical);
}
if (clearance < results[2].min) {
results[2].min = clearance;
results[2].name = obstacle.name;
}
if (collision) {
results[2].collision = true;
}
}
return results;
}
// ------------------------------------------------------------------
// Input and movement
// ------------------------------------------------------------------
const keys = new Set();
const movementCodes = [
"KeyW", "KeyA", "KeyS", "KeyD",
"ArrowUp", "ArrowLeft", "ArrowDown", "ArrowRight",
"KeyQ", "KeyE"
];
window.addEventListener("keydown", (event) => {
if (movementCodes.includes(event.code)) {
event.preventDefault();
keys.add(event.code);
}
if (event.code === "KeyR") resetChallenge();
if (event.code === "KeyP") togglePause();
if (event.code === "KeyC") clearanceGroup.visible = !clearanceGroup.visible;
if (event.code === "KeyH") toggleHelp();
});
window.addEventListener("keyup", (event) => {
keys.delete(event.code);
});
window.addEventListener("blur", () => keys.clear());
for (const button of document.querySelectorAll("[data-key]")) {
button.addEventListener("pointerdown", (event) => {
event.preventDefault();
keys.add(button.dataset.key);
button.setPointerCapture(event.pointerId);
});
const release = (event) => {
event.preventDefault();
keys.delete(button.dataset.key);
};
button.addEventListener("pointerup", release);
button.addEventListener("pointercancel", release);
button.addEventListener("lostpointercapture", () => {
keys.delete(button.dataset.key);
});
}
const state = {
cx: -5.8,
cz: 4.35,
angle: 0,
walkPhase: 0,
moving: false,
paused: false,
complete: false,
blocked: false
};
function resetChallenge() {
state.cx = -5.8;
state.cz = 4.35;
state.angle = 0;
state.walkPhase = 0;
state.moving = false;
state.paused = false;
state.complete = false;
state.blocked = false;
keys.clear();
document.getElementById("pauseButton").textContent = "Pause";
showToast("Challenge reset");
updateVisuals(0);
}
function togglePause() {
state.paused = !state.paused;
document.getElementById("pauseButton").textContent =
state.paused ? "Resume" : "Pause";
keys.clear();
}
function movementInput() {
let forward = 0;
let right = 0;
if (keys.has("KeyW") || keys.has("ArrowUp")) forward += 1;
if (keys.has("KeyS") || keys.has("ArrowDown")) forward -= 1;
if (keys.has("KeyD") || keys.has("ArrowRight")) right += 1;
if (keys.has("KeyA") || keys.has("ArrowLeft")) right -= 1;
const length = Math.hypot(forward, right);
if (length === 0) return { x: 0, z: 0 };
forward /= length;
right /= length;
// Screen-relative movement based on the current camera yaw.
const forwardX = -Math.sin(cameraYaw);
const forwardZ = -Math.cos(cameraYaw);
const rightX = Math.cos(cameraYaw);
const rightZ = -Math.sin(cameraYaw);
return {
x: forwardX * forward + rightX * right,
z: forwardZ * forward + rightZ * right
};
}
function updateMovement(dt) {
if (state.paused || state.complete) {
state.moving = false;
return;
}
const input = movementInput();
const dx = input.x * MOVE_SPEED * dt;
const dz = input.z * MOVE_SPEED * dt;
const rotateInput =
(keys.has("KeyE") ? 1 : 0) - (keys.has("KeyQ") ? 1 : 0);
const rotation = rotateInput * ROTATE_SPEED * dt;
const oldX = state.cx;
const oldZ = state.cz;
const oldAngle = state.angle;
let moved = false;
// Try the complete translation, then slide along one axis.
if (poseIsClear(state.cx + dx, state.cz + dz, state.angle)) {
state.cx += dx;
state.cz += dz;
moved = true;
} else {
if (poseIsClear(state.cx + dx, state.cz, state.angle)) {
state.cx += dx;
moved = true;
}
if (poseIsClear(state.cx, state.cz + dz, state.angle)) {
state.cz += dz;
moved = true;
}
}
// Rotation is tested independently around the team center.
if (Math.abs(rotation) > 1e-8) {
if (poseIsClear(state.cx, state.cz, state.angle + rotation)) {
state.angle += rotation;
moved = true;
}
}
const actualDistance = Math.hypot(
state.cx - oldX,
state.cz - oldZ
);
state.moving = actualDistance > 0.0001;
state.walkPhase += actualDistance * 7.5;
const nowBlocked = !poseIsClear(state.cx, state.cz, state.angle);
if (nowBlocked && !state.blocked) {
const clearances = evaluateClearance(
state.cx,
state.cz,
state.angle
);
const blockedNames = clearances
.filter((result) => result.collision)
.map((result) => result.name);
showToast(
`Blocked by ${[...new Set(blockedNames)].join(", ")}`
);
}
state.blocked = nowBlocked;
const goalDistance = Math.hypot(
state.cx - GOAL.x,
state.cz - GOAL.z
);
if (goalDistance < GOAL.radius * 0.82) {
state.complete = true;
state.moving = false;
goalMaterial.emissiveIntensity = 1.7;
showToast("Delivery complete — the full formation reached the zone");
}
}
// ------------------------------------------------------------------
// Visual updates and HUD
// ------------------------------------------------------------------
const safeColor = new THREE.Color(0x57e39b);
const tightColor = new THREE.Color(0xffd166);
const blockedColor = new THREE.Color(0xff647c);
const blueColor = new THREE.Color(0x65a9ff);
const orangeColor = new THREE.Color(0xff9b62);
function statusFor(result) {
if (result.collision) {
return { label: "BLOCKED", color: blockedColor };
}
if (result.min < TIGHT_CLEARANCE) {
return { label: "TIGHT", color: tightColor };
}
return { label: "SAFE", color: safeColor };
}
function updateHUD(clearances) {
const entries = [
{ result: clearances[0], dot: "dotA", tag: "tagA", value: "valueA", note: "noteA" },
{ result: clearances[1], dot: "dotB", tag: "tagB", value: "valueB", note: "noteB" },
{ result: clearances[2], dot: "dotP", tag: "tagP", value: "valueP", note: "noteP" }
];
let overallLabel = "SAFE";
let overallColor = safeColor;
for (const entry of entries) {
const status = statusFor(entry.result);
const colorHex = `#${status.color.getHexString()}`;
document.getElementById(entry.dot).style.background = colorHex;
document.getElementById(entry.dot).style.color = colorHex;
document.getElementById(entry.tag).style.color = colorHex;
document.getElementById(entry.tag).textContent = status.label;
const centimeters = Math.max(0, entry.result.min * 100);
document.getElementById(entry.value).textContent =
`${centimeters.toFixed(0)} cm`;
document.getElementById(entry.note).textContent =
`Nearest: ${entry.result.name}`;
if (status.label === "BLOCKED") {
overallLabel = "BLOCKED";
overallColor = blockedColor;
} else if (
status.label === "TIGHT" &&
overallLabel === "SAFE"
) {
overallLabel = "TIGHT";
overallColor = tightColor;
}
}
const overall = document.getElementById("overallState");
overall.textContent = overallLabel;
overall.style.color = `#${overallColor.getHexString()}`;
}
function updateVisuals(dt) {
const people = personPositions(state.cx, state.cz, state.angle);
const carriers = [carrierA, carrierB];
for (let index = 0; index < carriers.length; index++) {
const carrier = carriers[index];
const person = people[index];
carrier.root.position.set(person.x, 0, person.z);
carrier.root.rotation.y = -state.angle;
const bob = state.moving
? Math.abs(Math.sin(state.walkPhase)) * 0.025
: 0;
carrier.body.position.y = bob;
carrier.legs[0].rotation.x =
state.moving ? Math.sin(state.walkPhase) * 0.52 : 0;
carrier.legs[1].rotation.x =
state.moving ? -Math.sin(state.walkPhase) * 0.52 : 0;
carrier.ring.position.set(person.x, 0.025, person.z);
}
plankGroup.position.set(state.cx, 1.035, state.cz);
plankGroup.rotation.y = -state.angle;
plankShell.position.set(state.cx, 1.035, state.cz);
plankShell.rotation.y = -state.angle;
plankEdges.position.set(state.cx, 1.035, state.cz);
plankEdges.rotation.y = -state.angle;
const clearances = evaluateClearance(
state.cx,
state.cz,
state.angle
);
const carrierAStatus = statusFor(clearances[0]);
const carrierBStatus = statusFor(clearances[1]);
const plankStatus = statusFor(clearances[2]);
carrierA.ringMaterial.color.copy(carrierAStatus.color);
carrierB.ringMaterial.color.copy(carrierBStatus.color);
plankShellMaterial.color.copy(plankStatus.color);
plankEdges.material.color.copy(plankStatus.color);
updateHUD(clearances);
// Highlight obstacles currently involved in a collision.
for (const obstacle of obstacles) {
let hit = false;
for (const person of people) {
if (personHitsObstacle(person.x, person.z, obstacle)) {
hit = true;
}
}
if (plankHitsObstacle(state.cx, state.cz, state.angle, obstacle)) {
hit = true;
}
obstacle.material.emissive.setHex(hit ? 0x8f2438 : 0x000000);
obstacle.material.emissiveIntensity = hit ? 0.75 : 0;
}
goalRing.rotation.z += dt * 0.3;
goalMaterial.emissiveIntensity = state.complete
? 1.7
: 0.7 + Math.sin(performance.now() * 0.003) * 0.15;
}
let toastTimer = 0;
function showToast(message) {
const toast = document.getElementById("toast");
toast.textContent = message;
toast.classList.add("visible");
clearTimeout(toastTimer);
toastTimer = setTimeout(() => {
toast.classList.remove("visible");
}, 2200);
}
// ------------------------------------------------------------------
// Help and direct camera controls
// ------------------------------------------------------------------
function toggleHelp() {
document.getElementById("help").classList.toggle("hidden");
}
document.getElementById("resetButton").addEventListener("click", resetChallenge);
document.getElementById("pauseButton").addEventListener("click", togglePause);
document.getElementById("helpButton").addEventListener("click", toggleHelp);
document.getElementById("closeHelp").addEventListener("click", toggleHelp);
let dragging = false;
let lastPointerX = 0;
let lastPointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
dragging = true;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
cameraYaw -= dx * 0.006;
cameraElevation = clamp(
cameraElevation + dy * 0.005,
0.28,
1.18
);
lastPointerX = event.clientX;
lastPointerY = event.clientY;
});
function stopDragging() {
dragging = false;
}
renderer.domElement.addEventListener("pointerup", stopDragging);
renderer.domElement.addEventListener("pointercancel", stopDragging);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraDistance = clamp(
cameraDistance + event.deltaY * 0.012,
12.5,
28
);
},
{ passive: false }
);
function updateCamera() {
const horizontal =
cameraDistance * Math.cos(cameraElevation);
camera.position.set(
cameraTarget.x + Math.sin(cameraYaw) * horizontal,
cameraTarget.y + Math.sin(cameraElevation) * cameraDistance,
cameraTarget.z + Math.cos(cameraYaw) * horizontal
);
camera.lookAt(cameraTarget);
}
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
// ------------------------------------------------------------------
// Main loop
// ------------------------------------------------------------------
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.04);
updateMovement(dt);
updateVisuals(dt);
updateCamera();
renderer.render(scene, camera);
}
updateVisuals(0);
animate();
</script>
</body>
</html>
```