Prompt specification
Build or customize the Three.js 3D scene "Different Arcs Same Basket" 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: Different Arcs Same Basket
Category: Games & Play · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/different-arcs-same-basket-x3628
Scene Description Prompt:
Compare low-poly basketball trajectories reaching one basket under ideal projectile motion, with adjustable launch angle and speed. Three.js from cdnjs, one HTML file.
Working Three.js Starter (different-arcs-same-basket-x3628):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Arc Lab — Basketball Trajectory Comparator</title>
<style>
:root {
color-scheme: dark;
--bg: #07111f;
--panel: rgba(10, 20, 34, 0.84);
--panel-solid: #0c1828;
--line: rgba(163, 196, 225, 0.16);
--text: #eef7ff;
--muted: #8ea4bb;
--accent: #67e6ff;
--accent-2: #ffba55;
--good: #61e69a;
--bad: #ff7387;
}
* {
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;
}
button,
input {
font: inherit;
}
#scene {
position: fixed;
inset: 0;
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#scene.grabbing {
cursor: grabbing;
}
.brand {
position: fixed;
top: 22px;
left: 24px;
z-index: 3;
pointer-events: none;
text-shadow: 0 2px 20px rgba(0, 0, 0, 0.5);
}
.brand-kicker {
margin-bottom: 5px;
color: var(--accent);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.2em;
}
.brand h1 {
margin: 0;
font-size: clamp(26px, 3vw, 43px);
line-height: 0.95;
letter-spacing: -0.055em;
}
.brand p {
max-width: 360px;
margin: 10px 0 0;
color: #a9bbcd;
font-size: 13px;
line-height: 1.5;
}
.panel {
position: fixed;
top: 18px;
right: 18px;
z-index: 5;
width: 354px;
max-height: calc(100vh - 36px);
overflow: auto;
padding: 18px;
border: 1px solid var(--line);
border-radius: 20px;
background:
linear-gradient(145deg, rgba(255, 255, 255, 0.035), transparent 45%),
var(--panel);
box-shadow:
0 24px 70px rgba(0, 0, 0, 0.4),
inset 0 1px 0 rgba(255, 255, 255, 0.035);
backdrop-filter: blur(18px) saturate(135%);
scrollbar-width: thin;
scrollbar-color: #294158 transparent;
}
.panel-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 18px;
}
.panel-head strong {
font-size: 12px;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.badge {
padding: 6px 9px;
border: 1px solid rgba(103, 230, 255, 0.25);
border-radius: 999px;
background: rgba(103, 230, 255, 0.08);
color: var(--accent);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.08em;
white-space: nowrap;
}
.control {
margin-bottom: 18px;
}
.control-row {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 14px;
margin-bottom: 9px;
}
.control-row label {
color: #c6d5e3;
font-size: 12px;
font-weight: 700;
}
output {
color: var(--text);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 12px;
font-weight: 700;
}
input[type="range"] {
width: 100%;
height: 5px;
margin: 4px 0 0;
border-radius: 999px;
appearance: none;
background: #223449;
outline: none;
}
input[type="range"]::-webkit-slider-thumb {
width: 17px;
height: 17px;
border: 3px solid var(--panel-solid);
border-radius: 50%;
appearance: none;
background: var(--accent);
box-shadow: 0 0 0 1px var(--accent), 0 0 16px rgba(103, 230, 255, 0.4);
cursor: pointer;
}
input[type="range"]::-moz-range-thumb {
width: 13px;
height: 13px;
border: 3px solid var(--panel-solid);
border-radius: 50%;
background: var(--accent);
box-shadow: 0 0 0 1px var(--accent), 0 0 16px rgba(103, 230, 255, 0.4);
cursor: pointer;
}
.range-ends {
display: flex;
justify-content: space-between;
margin-top: 7px;
color: #60778e;
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 9px;
}
.solution-note {
margin: -6px 0 15px;
color: var(--muted);
font-size: 11px;
line-height: 1.45;
}
.solution-note strong {
color: var(--accent-2);
}
.button-row {
display: grid;
grid-template-columns: 1fr 1.25fr;
gap: 9px;
}
button {
min-height: 42px;
padding: 10px 12px;
border: 1px solid var(--line);
border-radius: 11px;
color: var(--text);
background: #15263a;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.035em;
cursor: pointer;
transition:
transform 140ms ease,
border-color 140ms ease,
background 140ms ease,
opacity 140ms ease;
}
button:hover:not(:disabled) {
transform: translateY(-1px);
border-color: rgba(103, 230, 255, 0.45);
background: #1a3048;
}
button:active:not(:disabled) {
transform: translateY(0);
}
button:disabled {
cursor: wait;
opacity: 0.55;
}
button.primary {
border-color: rgba(103, 230, 255, 0.48);
background: linear-gradient(135deg, #47d8f4, #5c9dff);
color: #04101c;
box-shadow: 0 8px 24px rgba(55, 190, 235, 0.18);
}
button.primary:hover:not(:disabled) {
background: linear-gradient(135deg, #6ae9ff, #70aaff);
}
.prediction {
position: relative;
margin: 15px 0;
padding: 13px 13px 12px 38px;
overflow: hidden;
border: 1px solid var(--line);
border-radius: 13px;
background: rgba(4, 12, 22, 0.48);
}
.prediction::before {
content: "";
position: absolute;
top: 16px;
left: 15px;
width: 9px;
height: 9px;
border-radius: 50%;
background: var(--muted);
box-shadow: 0 0 0 5px rgba(142, 164, 187, 0.08);
}
.prediction[data-mode="good"]::before {
background: var(--good);
box-shadow: 0 0 0 5px rgba(97, 230, 154, 0.1), 0 0 16px rgba(97, 230, 154, 0.55);
}
.prediction[data-mode="bad"]::before {
background: var(--bad);
box-shadow: 0 0 0 5px rgba(255, 115, 135, 0.1);
}
.prediction-title {
margin-bottom: 3px;
color: var(--accent);
font-size: 10px;
font-weight: 900;
letter-spacing: 0.14em;
}
.prediction[data-mode="good"] .prediction-title {
color: var(--good);
}
.prediction[data-mode="bad"] .prediction-title {
color: var(--bad);
}
.prediction-detail {
color: #a8bacb;
font-size: 11px;
line-height: 1.45;
}
.section-title {
display: flex;
align-items: center;
justify-content: space-between;
margin: 18px 0 9px;
color: #b8c8d8;
font-size: 10px;
font-weight: 900;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.text-button {
min-height: auto;
padding: 3px 0;
border: 0;
background: transparent;
color: #6f879e;
font-size: 9px;
letter-spacing: 0.08em;
}
.text-button:hover:not(:disabled) {
transform: none;
border: 0;
background: transparent;
color: var(--accent);
}
.formula {
padding: 11px 12px;
border-left: 2px solid rgba(255, 186, 85, 0.6);
border-radius: 0 9px 9px 0;
background: rgba(255, 186, 85, 0.055);
color: #bfcbd6;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 10px;
line-height: 1.65;
}
.formula b {
color: var(--accent-2);
font-weight: 700;
}
.comparison {
width: 100%;
border-collapse: collapse;
font-size: 10px;
}
.comparison th {
padding: 7px 5px;
border-bottom: 1px solid var(--line);
color: #637a91;
font-size: 8px;
letter-spacing: 0.1em;
text-align: right;
text-transform: uppercase;
}
.comparison th:first-child,
.comparison td:first-child {
text-align: left;
}
.comparison td {
padding: 9px 5px;
border-bottom: 1px solid rgba(163, 196, 225, 0.08);
color: #b8c8d7;
text-align: right;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
.comparison .empty {
padding: 15px 0 8px;
color: #647b91;
text-align: center !important;
font-family: inherit;
}
.shot-dot {
display: inline-block;
width: 7px;
height: 7px;
margin-right: 7px;
border-radius: 50%;
vertical-align: 1px;
}
.score {
color: var(--good) !important;
font-weight: 800;
}
.miss {
color: var(--bad) !important;
font-weight: 800;
}
.camera-hint {
position: fixed;
bottom: 18px;
left: 24px;
z-index: 3;
padding: 9px 12px;
border: 1px solid var(--line);
border-radius: 10px;
background: rgba(7, 17, 31, 0.58);
color: #7890a7;
font-size: 10px;
pointer-events: none;
backdrop-filter: blur(10px);
}
.scene-label {
position: fixed;
z-index: 2;
padding: 6px 9px;
border: 1px solid rgba(103, 230, 255, 0.25);
border-radius: 999px;
background: rgba(7, 17, 31, 0.76);
color: var(--accent);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.08em;
pointer-events: none;
transform: translate(-50%, -140%);
white-space: nowrap;
}
#toast {
position: fixed;
left: 50%;
bottom: 25px;
z-index: 10;
padding: 12px 18px;
border: 1px solid rgba(103, 230, 255, 0.3);
border-radius: 999px;
background: rgba(8, 20, 34, 0.92);
color: var(--text);
box-shadow: 0 12px 40px rgba(0, 0, 0, 0.35);
font-size: 12px;
font-weight: 800;
opacity: 0;
pointer-events: none;
transform: translate(-50%, 15px);
transition:
opacity 180ms ease,
transform 180ms ease;
}
#toast.show {
opacity: 1;
transform: translate(-50%, 0);
}
@media (max-width: 760px) {
.brand {
top: 14px;
left: 14px;
}
.brand p {
display: none;
}
.panel {
top: auto;
right: 10px;
bottom: 10px;
left: 10px;
width: auto;
max-height: 48vh;
padding: 14px;
border-radius: 16px;
}
.panel-head {
margin-bottom: 13px;
}
.camera-hint {
display: none;
}
.scene-label {
display: none;
}
#toast {
top: 75px;
bottom: auto;
}
}
</style>
</head>
<body>
<canvas id="scene"></canvas>
<header class="brand">
<div class="brand-kicker">PROJECTILE STUDY / 01</div>
<h1>ARC LAB</h1>
<p>
Compare ideal basketball trajectories toward one basket.
Change the launch angle, tune the speed, and keep every arc.
</p>
</header>
<aside class="panel">
<div class="panel-head">
<strong>Launch controls</strong>
<span class="badge">9.90 M TARGET</span>
</div>
<div class="control">
<div class="control-row">
<label for="angle">Launch angle</label>
<output id="angleOutput">48°</output>
</div>
<input
id="angle"
type="range"
min="25"
max="72"
step="1"
value="48"
aria-label="Launch angle"
/>
<div class="range-ends">
<span>25° FLAT</span>
<span>72° STEEP</span>
</div>
</div>
<div class="control">
<div class="control-row">
<label for="speed">Launch speed</label>
<output id="speedOutput">0.00 m/s</output>
</div>
<input
id="speed"
type="range"
min="6"
max="16.5"
step="0.01"
value="10.56"
aria-label="Launch speed"
/>
<div class="range-ends">
<span>6.0 M/S</span>
<span>16.5 M/S</span>
</div>
</div>
<p class="solution-note" id="solutionNote"></p>
<div class="button-row">
<button id="solveButton" type="button">FIND BASKET SPEED</button>
<button id="launchButton" class="primary" type="button">
LAUNCH & COMPARE
</button>
</div>
<div
id="prediction"
class="prediction"
data-mode="good"
aria-live="polite"
>
<div id="predictionTitle" class="prediction-title">ON TARGET</div>
<div id="predictionDetail" class="prediction-detail"></div>
</div>
<div class="section-title">
<span>Ideal projectile equation</span>
</div>
<div class="formula">
x(t) = v cos(θ)t<br />
y(t) = y₀ + v sin(θ)t − ½gt²<br />
<b>v² = gx² / [2 cos²(θ)(x tan(θ) + y₀ − yₕ)]</b><br />
g = 9.81 m/s² · x = 9.90 m · y₀ = 1.75 m · yₕ = 3.05 m
</div>
<div class="section-title">
<span>Shot comparison</span>
<button id="clearButton" class="text-button" type="button">
CLEAR ARCS
</button>
</div>
<table class="comparison">
<thead>
<tr>
<th>Arc</th>
<th>Angle</th>
<th>Speed</th>
<th>Result</th>
</tr>
</thead>
<tbody id="comparisonBody">
<tr>
<td class="empty" colspan="4">Launch a shot to begin comparing</td>
</tr>
</tbody>
</table>
</aside>
<div class="camera-hint">DRAG TO ORBIT · WHEEL TO ZOOM · DOUBLE-CLICK TO RESET VIEW</div>
<div id="hoopLabel" class="scene-label">TARGET · 3.05 M</div>
<div id="toast"></div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Simulation constants
// ------------------------------------------------------------
const G = 9.81;
const RELEASE = new THREE.Vector3(0, 1.75, 0);
const TARGET = new THREE.Vector3(9.9, 3.05, 0);
const SCORE_TOLERANCE = 0.18;
const angleInput = document.getElementById("angle");
const speedInput = document.getElementById("speed");
const angleOutput = document.getElementById("angleOutput");
const speedOutput = document.getElementById("speedOutput");
const solutionNote = document.getElementById("solutionNote");
const prediction = document.getElementById("prediction");
const predictionTitle = document.getElementById("predictionTitle");
const predictionDetail = document.getElementById("predictionDetail");
const launchButton = document.getElementById("launchButton");
const solveButton = document.getElementById("solveButton");
const clearButton = document.getElementById("clearButton");
const comparisonBody = document.getElementById("comparisonBody");
const toast = document.getElementById("toast");
const hoopLabel = document.getElementById("hoopLabel");
const degrees = (radians) => radians * 180 / Math.PI;
const radians = (deg) => deg * Math.PI / 180;
function solutionSpeed(angleDeg) {
const theta = radians(angleDeg);
const denominator =
TARGET.x * Math.tan(theta) + RELEASE.y - TARGET.y;
if (denominator <= 0) return null;
return Math.sqrt(
(G * TARGET.x * TARGET.x) /
(2 * Math.cos(theta) ** 2 * denominator)
);
}
function makeShot(angleDeg, speed) {
const theta = radians(angleDeg);
const vx = speed * Math.cos(theta);
const vy = speed * Math.sin(theta);
const groundTime =
(vy + Math.sqrt(vy * vy + 2 * G * RELEASE.y)) / G;
const targetTime = (TARGET.x - RELEASE.x) / vx;
const landingX = vx * groundTime;
const reachedTarget = landingX >= TARGET.x;
const crossingY =
RELEASE.y +
vy * targetTime -
0.5 * G * targetTime * targetTime;
const error = crossingY - TARGET.y;
const score = reachedTarget && Math.abs(error) <= SCORE_TOLERANCE;
return {
angle: angleDeg,
speed,
vx,
vy,
groundTime,
targetTime,
landingX,
reachedTarget,
crossingY,
error,
score,
shortBy: reachedTarget ? 0 : Math.max(0, TARGET.x - landingX)
};
}
function positionAt(shot, time) {
return new THREE.Vector3(
RELEASE.x + shot.vx * time,
RELEASE.y + shot.vy * time - 0.5 * G * time * time,
RELEASE.z
);
}
function trajectoryPoints(shot, segments = 110) {
const points = [];
for (let i = 0; i <= segments; i++) {
const time = shot.groundTime * i / segments;
points.push(positionAt(shot, time));
}
return points;
}
// ------------------------------------------------------------
// Renderer and scene
// ------------------------------------------------------------
const canvas = document.getElementById("scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07111f);
scene.fog = new THREE.Fog(0x07111f, 18, 38);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraTarget = new THREE.Vector3(4.8, 1.55, 0);
const cameraState = {
theta: -0.48,
phi: 1.27,
radius: 16.5,
targetTheta: -0.48,
targetPhi: 1.27,
targetRadius: 16.5
};
const hemisphere = new THREE.HemisphereLight(
0xb9dcff,
0x172131,
1.8
);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffffff, 2.8);
keyLight.position.set(-3, 11, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -10;
keyLight.shadow.camera.right = 16;
keyLight.shadow.camera.top = 10;
keyLight.shadow.camera.bottom = -8;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 30;
keyLight.shadow.bias = -0.00025;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x58cfff, 1.6);
rimLight.position.set(12, 7, -6);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0xffa54d, 18, 9, 2);
warmLight.position.set(10.2, 4.1, 0);
scene.add(warmLight);
// ------------------------------------------------------------
// Court
// ------------------------------------------------------------
const floorMaterial = new THREE.MeshStandardMaterial({
color: 0x14263a,
roughness: 0.9,
metalness: 0.03
});
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(28, 15),
floorMaterial
);
floor.rotation.x = -Math.PI / 2;
floor.position.set(5, 0, 0);
floor.receiveShadow = true;
scene.add(floor);
const courtMaterial = new THREE.LineBasicMaterial({
color: 0x74b8cc,
transparent: true,
opacity: 0.36
});
function courtLine(points, closed = false, material = courtMaterial) {
const geometry = new THREE.BufferGeometry().setFromPoints(
points.map((point) => new THREE.Vector3(...point))
);
const line = closed
? new THREE.LineLoop(geometry, material)
: new THREE.Line(geometry, material);
scene.add(line);
return line;
}
courtLine([
[-2.6, 0.015, -4.5],
[13.8, 0.015, -4.5],
[13.8, 0.015, 4.5],
[-2.6, 0.015, 4.5]
], true);
courtLine([
[5.2, 0.015, -4.5],
[5.2, 0.015, 4.5]
]);
const arcPoints = [];
for (let i = 0; i <= 64; i++) {
const angle = -Math.PI / 2 + Math.PI * i / 64;
arcPoints.push([
5.2 + Math.cos(angle) * 2.8,
0.016,
Math.sin(angle) * 2.8
]);
}
courtLine(arcPoints);
courtLine([
[-2.6, 0.016, -1.8],
[-1.6, 0.016, -1.8],
[-1.6, 0.016, 1.8],
[-2.6, 0.016, 1.8]
]);
// ------------------------------------------------------------
// Basket
// ------------------------------------------------------------
const metalMaterial = new THREE.MeshStandardMaterial({
color: 0xe15d3f,
roughness: 0.42,
metalness: 0.58,
flatShading: true
});
const rim = new THREE.Mesh(
new THREE.TorusGeometry(0.28, 0.034, 8, 20),
metalMaterial
);
rim.rotation.x = Math.PI / 2;
rim.position.set(TARGET.x, TARGET.y, TARGET.z);
rim.castShadow = true;
scene.add(rim);
const targetGlow = new THREE.Mesh(
new THREE.TorusGeometry(0.31, 0.012, 6, 24),
new THREE.MeshBasicMaterial({
color: 0xffbd69,
transparent: true,
opacity: 0.65
})
);
targetGlow.rotation.x = Math.PI / 2;
targetGlow.position.set(TARGET.x, TARGET.y, TARGET.z);
scene.add(targetGlow);
const backboardMaterial = new THREE.MeshPhysicalMaterial({
color: 0xb9e6f2,
transparent: true,
opacity: 0.25,
roughness: 0.12,
metalness: 0.05,
side: THREE.DoubleSide
});
const backboard = new THREE.Mesh(
new THREE.BoxGeometry(0.08, 1.35, 1.8),
backboardMaterial
);
backboard.position.set(10.25, 3.45, 0);
backboard.castShadow = true;
scene.add(backboard);
const boardEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(backboard.geometry),
new THREE.LineBasicMaterial({
color: 0xdff6ff,
transparent: true,
opacity: 0.62
})
);
boardEdges.position.copy(backboard.position);
scene.add(boardEdges);
courtLine([
[10.198, 2.95, -0.37],
[10.198, 2.95, 0.37],
[10.198, 3.55, 0.37],
[10.198, 3.55, -0.37]
], true, new THREE.LineBasicMaterial({
color: 0xf2fbff,
transparent: true,
opacity: 0.85
}));
const poleMaterial = new THREE.MeshStandardMaterial({
color: 0x52677c,
roughness: 0.55,
metalness: 0.65,
flatShading: true
});
const pole = new THREE.Mesh(
new THREE.CylinderGeometry(0.12, 0.16, 3.75, 8),
poleMaterial
);
pole.position.set(10.78, 1.875, 0);
pole.castShadow = true;
scene.add(pole);
const support = new THREE.Mesh(
new THREE.BoxGeometry(0.58, 0.12, 0.14),
poleMaterial
);
support.position.set(10.49, 3.75, 0);
support.castShadow = true;
scene.add(support);
// Low-poly net made from line segments.
const netVertices = [];
const segments = 12;
for (let i = 0; i < segments; i++) {
const a = i / segments * Math.PI * 2;
const b = (i + 0.5) / segments * Math.PI * 2;
const c = (i - 0.5) / segments * Math.PI * 2;
const topA = [
TARGET.x + Math.cos(a) * 0.27,
TARGET.y - 0.02,
Math.sin(a) * 0.27
];
const bottomB = [
TARGET.x + Math.cos(b) * 0.17,
TARGET.y - 0.62,
Math.sin(b) * 0.17
];
const bottomC = [
TARGET.x + Math.cos(c) * 0.17,
TARGET.y - 0.62,
Math.sin(c) * 0.17
];
netVertices.push(...topA, ...bottomB);
netVertices.push(...topA, ...bottomC);
}
for (let level = 1; level <= 3; level++) {
const t = level / 4;
const y = TARGET.y - 0.02 - t * 0.6;
const radius = 0.27 - t * 0.1;
for (let i = 0; i < segments; i++) {
const a = i / segments * Math.PI * 2;
const b = (i + 1) / segments * Math.PI * 2;
netVertices.push(
TARGET.x + Math.cos(a) * radius,
y,
Math.sin(a) * radius,
TARGET.x + Math.cos(b) * radius,
y,
Math.sin(b) * radius
);
}
}
const netGeometry = new THREE.BufferGeometry();
netGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(netVertices, 3)
);
const net = new THREE.LineSegments(
netGeometry,
new THREE.LineBasicMaterial({
color: 0xeef8ff,
transparent: true,
opacity: 0.62
})
);
scene.add(net);
// ------------------------------------------------------------
// Low-poly shooter
// ------------------------------------------------------------
const shooter = new THREE.Group();
shooter.position.set(-0.5, 0, 0);
scene.add(shooter);
const jerseyMaterial = new THREE.MeshStandardMaterial({
color: 0x287ca5,
roughness: 0.78,
flatShading: true
});
const shortsMaterial = new THREE.MeshStandardMaterial({
color: 0x15364f,
roughness: 0.84,
flatShading: true
});
const skinMaterial = new THREE.MeshStandardMaterial({
color: 0xc98761,
roughness: 0.88,
flatShading: true
});
const shoeMaterial = new THREE.MeshStandardMaterial({
color: 0xecf5f8,
roughness: 0.8,
flatShading: true
});
function addCylinderBetween(a, b, radius, material, radialSegments = 6) {
const start = new THREE.Vector3(...a);
const end = new THREE.Vector3(...b);
const direction = end.clone().sub(start);
const length = direction.length();
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(
radius,
radius,
length,
radialSegments
),
material
);
mesh.position.copy(start).add(end).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
mesh.castShadow = true;
shooter.add(mesh);
return mesh;
}
addCylinderBetween([-0.16, 0.18, 0], [-0.1, 1.0, 0], 0.12, skinMaterial);
addCylinderBetween([0.16, 0.18, 0], [0.1, 1.0, 0], 0.12, skinMaterial);
const leftShoe = new THREE.Mesh(
new THREE.BoxGeometry(0.28, 0.16, 0.24),
shoeMaterial
);
leftShoe.position.set(-0.2, 0.08, 0.03);
leftShoe.castShadow = true;
shooter.add(leftShoe);
const rightShoe = leftShoe.clone();
rightShoe.position.x = 0.2;
shooter.add(rightShoe);
const shorts = new THREE.Mesh(
new THREE.CylinderGeometry(0.3, 0.26, 0.43, 7),
shortsMaterial
);
shorts.position.y = 1.08;
shorts.castShadow = true;
shooter.add(shorts);
const torso = new THREE.Mesh(
new THREE.CylinderGeometry(0.26, 0.31, 0.65, 7),
jerseyMaterial
);
torso.position.y = 1.55;
torso.castShadow = true;
shooter.add(torso);
const head = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.22, 1),
skinMaterial
);
head.position.y = 2.08;
head.castShadow = true;
shooter.add(head);
addCylinderBetween(
[-0.22, 1.78, 0],
[-0.12, 1.55, 0.31],
0.075,
skinMaterial
);
addCylinderBetween(
[0.22, 1.78, 0],
[0.54, 1.76, 0.08],
0.075,
skinMaterial
);
// ------------------------------------------------------------
// Basketball
// ------------------------------------------------------------
function createBasketball() {
const group = new THREE.Group();
const ball = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.225, 1),
new THREE.MeshStandardMaterial({
color: 0xe87828,
roughness: 0.82,
flatShading: true
})
);
ball.castShadow = true;
ball.receiveShadow = true;
group.add(ball);
const seamMaterial = new THREE.MeshBasicMaterial({
color: 0x25150f
});
const seamA = new THREE.Mesh(
new THREE.TorusGeometry(0.222, 0.008, 4, 14),
seamMaterial
);
group.add(seamA);
const seamB = new THREE.Mesh(
new THREE.TorusGeometry(0.222, 0.008, 4, 14),
seamMaterial
);
seamB.rotation.y = Math.PI / 2;
group.add(seamB);
const seamC = new THREE.Mesh(
new THREE.TorusGeometry(0.222, 0.008, 4, 14),
seamMaterial
);
seamC.rotation.x = Math.PI / 2;
group.add(seamC);
return group;
}
const restingBall = createBasketball();
restingBall.position.copy(RELEASE);
scene.add(restingBall);
const activeBall = createBasketball();
activeBall.position.copy(RELEASE);
activeBall.visible = false;
scene.add(activeBall);
function createShadow() {
const shadow = new THREE.Mesh(
new THREE.CircleGeometry(0.22, 18),
new THREE.MeshBasicMaterial({
color: 0x000000,
transparent: true,
opacity: 0.25,
depthWrite: false
})
);
shadow.rotation.x = -Math.PI / 2;
shadow.position.y = 0.02;
scene.add(shadow);
return shadow;
}
const restingShadow = createShadow();
restingShadow.position.set(RELEASE.x, 0.02, RELEASE.z);
const activeShadow = createShadow();
activeShadow.visible = false;
function updateShadow(shadow, position) {
const height = Math.max(0, position.y);
shadow.position.x = position.x;
shadow.position.z = position.z;
shadow.scale.setScalar(1 + height * 0.17);
shadow.material.opacity = 0.28 / (1 + height * 0.75);
}
// ------------------------------------------------------------
// Trajectory rendering
// ------------------------------------------------------------
const previewGroup = new THREE.Group();
const historyGroup = new THREE.Group();
scene.add(previewGroup, historyGroup);
function disposeGroup(group) {
group.traverse((object) => {
if (object.geometry) object.geometry.dispose();
if (object.material) {
if (Array.isArray(object.material)) {
object.material.forEach((material) => material.dispose());
} else {
object.material.dispose();
}
}
});
group.clear();
}
function createTrajectoryMesh(points, color, radius = 0.018, opacity = 1) {
const curve = new THREE.CatmullRomCurve3(points);
const geometry = new THREE.TubeGeometry(
curve,
Math.min(140, points.length),
radius,
5,
false
);
const material = new THREE.MeshBasicMaterial({
color,
transparent: opacity < 1,
opacity
});
return new THREE.Mesh(geometry, material);
}
function createTrajectoryGroup(
points,
color,
radius = 0.018,
opacity = 1,
showMarkers = false
) {
const group = new THREE.Group();
group.add(createTrajectoryMesh(points, color, radius, opacity));
if (showMarkers) {
const markerGeometry = new THREE.IcosahedronGeometry(0.043, 0);
const markerMaterial = new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: 0.68
});
for (let i = 5; i < points.length - 5; i += 11) {
const marker = new THREE.Mesh(markerGeometry, markerMaterial);
marker.position.copy(points[i]);
group.add(marker);
}
}
return group;
}
const historyColors = [
0xffbd55,
0xe681ff,
0x62e69b,
0xff7287,
0x8fa4ff,
0x5fe1e8
];
const history = [];
let currentShot = null;
let animating = false;
let toastTimer = null;
function updatePrediction(shot) {
currentShot = shot;
if (shot.score) {
prediction.dataset.mode = "good";
predictionTitle.textContent = "ON TARGET · SCORE PROJECTED";
predictionDetail.textContent =
`The ball crosses the rim plane within ${Math.abs(shot.error).toFixed(2)} m of center.`;
} else if (!shot.reachedTarget) {
prediction.dataset.mode = "bad";
predictionTitle.textContent = "SHORT OF THE BASKET";
predictionDetail.textContent =
`The ball lands approximately ${shot.shortBy.toFixed(2)} m before the rim.`;
} else if (shot.error < 0) {
prediction.dataset.mode = "bad";
predictionTitle.textContent = "LOW AT THE RIM";
predictionDetail.textContent =
`Projected crossing is ${Math.abs(shot.error).toFixed(2)} m below the center.`;
} else {
prediction.dataset.mode = "bad";
predictionTitle.textContent = "HIGH AT THE RIM";
predictionDetail.textContent =
`Projected crossing is ${shot.error.toFixed(2)} m above the center.`;
}
}
function updateInterface() {
const angle = Number(angleInput.value);
const speed = Number(speedInput.value);
const exactSpeed = solutionSpeed(angle);
angleOutput.textContent = `${angle}°`;
speedOutput.textContent = `${speed.toFixed(2)} m/s`;
if (exactSpeed !== null) {
solutionNote.innerHTML =
`Exact basket speed at <strong>${angle}°</strong>: ` +
`<strong>${exactSpeed.toFixed(2)} m/s</strong>.`;
} else {
solutionNote.textContent =
"This angle has no positive ideal solution for the current target.";
}
const shot = makeShot(angle, speed);
updatePrediction(shot);
disposeGroup(previewGroup);
const points = trajectoryPoints(shot);
previewGroup.add(
createTrajectoryGroup(
points,
0x67e6ff,
0.021,
0.82,
true
)
);
}
function showToast(message) {
toast.textContent = message;
toast.classList.add("show");
clearTimeout(toastTimer);
toastTimer = setTimeout(() => toast.classList.remove("show"), 2100);
}
function renderComparison() {
if (!history.length) {
comparisonBody.innerHTML = `
<tr>
<td class="empty" colspan="4">
Launch a shot to begin comparing
</td>
</tr>
`;
return;
}
comparisonBody.innerHTML = history
.slice()
.reverse()
.map((shot, reverseIndex) => {
const originalIndex = history.length - 1 - reverseIndex;
const color = `#${historyColors[originalIndex % historyColors.length]
.toString(16)
.padStart(6, "0")}`;
return `
<tr>
<td>
<span class="shot-dot" style="background:${color}"></span>
${String(originalIndex + 1).padStart(2, "0")}
</td>
<td>${shot.angle}°</td>
<td>${shot.speed.toFixed(2)}</td>
<td class="${shot.score ? "score" : "miss"}">
${shot.score ? "SCORE" : "MISS"}
</td>
</tr>
`;
})
.join("");
}
function launchShot() {
if (animating) return;
const shot = makeShot(
Number(angleInput.value),
Number(speedInput.value)
);
const colorIndex = history.length % historyColors.length;
const color = historyColors[colorIndex];
const points = trajectoryPoints(shot);
const mesh = createTrajectoryGroup(
points,
color,
0.014,
0.92
);
historyGroup.add(mesh);
history.push({
...shot,
color,
mesh
});
// Keep the scene and table manageable.
if (history.length > 6) {
const oldest = history.shift();
historyGroup.remove(oldest.mesh);
disposeGroup(oldest.mesh);
}
renderComparison();
animateShot(shot);
}
function animateShot(shot) {
animating = true;
launchButton.disabled = true;
launchButton.textContent = "SHOT IN FLIGHT";
activeBall.visible = true;
activeShadow.visible = true;
activeBall.position.copy(RELEASE);
const startTime = performance.now();
let crossedTarget = false;
function frame(now) {
if (!animating) return;
const elapsed = (now - startTime) / 1000;
const simulationTime = elapsed * 1.35;
const position = positionAt(shot, simulationTime);
activeBall.position.copy(position);
activeBall.rotation.x += 0.07;
activeBall.rotation.z += 0.045;
updateShadow(activeShadow, position);
if (!crossedTarget && simulationTime >= shot.targetTime) {
crossedTarget = true;
if (shot.score) {
showToast(
`SHOT ${history.length}: SCORE · ${shot.angle}° · ${shot.speed.toFixed(2)} m/s`
);
} else if (shot.reachedTarget) {
showToast(
`SHOT ${history.length}: MISS · ${Math.abs(shot.error).toFixed(2)} m rim error`
);
} else {
showToast(
`SHOT ${history.length}: SHORT · ${shot.shortBy.toFixed(2)} m before the rim`
);
}
}
if (simulationTime >= shot.groundTime) {
activeBall.visible = false;
activeShadow.visible = false;
animating = false;
launchButton.disabled = false;
launchButton.textContent = "LAUNCH & COMPARE";
return;
}
requestAnimationFrame(frame);
}
requestAnimationFrame(frame);
}
function clearHistory() {
animating = false;
launchButton.disabled = false;
launchButton.textContent = "LAUNCH & COMPARE";
activeBall.visible = false;
activeShadow.visible = false;
history.length = 0;
disposeGroup(historyGroup);
renderComparison();
}
angleInput.addEventListener("input", updateInterface);
speedInput.addEventListener("input", updateInterface);
solveButton.addEventListener("click", () => {
const exactSpeed = solutionSpeed(Number(angleInput.value));
if (exactSpeed === null) {
showToast("No positive ideal solution for this angle.");
return;
}
speedInput.value = Math.min(
Number(speedInput.max),
Math.max(Number(speedInput.min), exactSpeed)
).toFixed(2);
updateInterface();
showToast(`Basket speed set to ${Number(speedInput.value).toFixed(2)} m/s.`);
});
launchButton.addEventListener("click", launchShot);
clearButton.addEventListener("click", clearHistory);
// ------------------------------------------------------------
// Custom orbit camera controls — no addons required
// ------------------------------------------------------------
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.classList.add("grabbing");
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - pointerX;
const deltaY = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
cameraState.targetTheta -= deltaX * 0.005;
cameraState.targetPhi = THREE.MathUtils.clamp(
cameraState.targetPhi + deltaY * 0.004,
0.62,
1.48
);
});
function stopDragging(event) {
dragging = false;
canvas.classList.remove("grabbing");
if (event && canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", stopDragging);
canvas.addEventListener("pointercancel", stopDragging);
canvas.addEventListener("lostpointercapture", () => {
dragging = false;
canvas.classList.remove("grabbing");
});
canvas.addEventListener("wheel", (event) => {
event.preventDefault();
cameraState.targetRadius = THREE.MathUtils.clamp(
cameraState.targetRadius * Math.exp(event.deltaY * 0.001),
8,
27
);
}, { passive: false });
canvas.addEventListener("dblclick", () => {
cameraState.targetTheta = -0.48;
cameraState.targetPhi = 1.27;
cameraState.targetRadius = 16.5;
});
function updateCamera() {
cameraState.theta +=
(cameraState.targetTheta - cameraState.theta) * 0.12;
cameraState.phi +=
(cameraState.targetPhi - cameraState.phi) * 0.12;
cameraState.radius +=
(cameraState.targetRadius - cameraState.radius) * 0.12;
const spherical = new THREE.Spherical(
cameraState.radius,
cameraState.phi,
cameraState.theta
);
const offset = new THREE.Vector3().setFromSpherical(spherical);
camera.position.copy(cameraTarget).add(offset);
camera.lookAt(cameraTarget);
}
// ------------------------------------------------------------
// Resize and render loop
// ------------------------------------------------------------
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height);
}
window.addEventListener("resize", resize);
const projectedTarget = new THREE.Vector3();
function animate() {
requestAnimationFrame(animate);
updateCamera();
projectedTarget.copy(TARGET);
projectedTarget.y += 0.45;
projectedTarget.project(camera);
const labelX = (projectedTarget.x * 0.5 + 0.5) * window.innerWidth;
const labelY = (-projectedTarget.y * 0.5 + 0.5) * window.innerHeight;
hoopLabel.style.left = `${labelX}px`;
hoopLabel.style.top = `${labelY}px`;
hoopLabel.style.opacity =
projectedTarget.z > -1 && projectedTarget.z < 1 ? "1" : "0";
renderer.render(scene, camera);
}
// Set the initial speed to the exact solution for the default angle.
speedInput.value = solutionSpeed(Number(angleInput.value)).toFixed(2);
updateInterface();
resize();
animate();
</script>
</body>
</html>
```