Prompt specification
Build or customize the Three.js 3D scene "One Table-Tennis Return" 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: One Table-Tennis Return
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/one-table-tennis-return-x3630
Scene Description Prompt:
Make a voxel Three.js scene in a self-contained HTML file using cdnjs: one table-tennis return. Animate a simplified serve and paddle return using stated bounce rules. Count contact only at geometric overlap and preserve the ball's identity through the interaction.
Working Three.js Starter (one-table-tennis-return-x3630):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Voxel Table Tennis — One Return</title>
<style>
:root {
color-scheme: dark;
--panel: rgba(18, 24, 36, 0.88);
--border: rgba(255, 255, 255, 0.14);
--accent: #ffcf5a;
--text: #f5f7fb;
--muted: #aeb8ca;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #9fc5df;
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--text);
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
}
.panel {
position: fixed;
z-index: 10;
background: var(--panel);
border: 1px solid var(--border);
border-radius: 14px;
box-shadow: 0 12px 35px rgba(0, 0, 0, 0.22);
backdrop-filter: blur(12px);
}
#hud {
top: 16px;
left: 16px;
width: min(370px, calc(100vw - 32px));
padding: 16px;
}
.eyebrow {
color: var(--accent);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
h1 {
margin: 4px 0 8px;
font-size: 22px;
line-height: 1.1;
}
#status {
min-height: 38px;
color: var(--muted);
font-size: 13px;
line-height: 1.45;
}
.metrics {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin: 12px 0;
}
.metric {
padding: 10px;
border: 1px solid var(--border);
border-radius: 10px;
background: rgba(255, 255, 255, 0.035);
}
.metric strong {
display: block;
color: var(--accent);
font-size: 24px;
line-height: 1;
}
.metric span {
display: block;
margin-top: 5px;
color: var(--muted);
font-size: 11px;
letter-spacing: 0.04em;
text-transform: uppercase;
}
.buttons {
display: flex;
gap: 8px;
}
button {
flex: 1;
padding: 9px 10px;
border: 1px solid var(--border);
border-radius: 8px;
background: rgba(255, 255, 255, 0.07);
color: var(--text);
cursor: pointer;
font: inherit;
font-size: 12px;
font-weight: 700;
}
button:hover {
background: rgba(255, 255, 255, 0.14);
}
#rules {
right: 16px;
bottom: 16px;
width: min(390px, calc(100vw - 32px));
padding: 15px 16px;
color: var(--muted);
font-size: 12px;
line-height: 1.5;
}
#rules strong {
color: var(--text);
}
#rules ol {
margin: 8px 0 12px;
padding-left: 20px;
}
#rules li { margin: 5px 0; }
#events {
margin: 0;
padding: 0;
list-style: none;
border-top: 1px solid var(--border);
}
#events li {
padding: 7px 0;
border-bottom: 1px solid rgba(255, 255, 255, 0.07);
color: #dce3ef;
font-variant-numeric: tabular-nums;
}
#events time {
display: inline-block;
width: 54px;
color: var(--accent);
font-weight: 800;
}
#cameraHint {
position: fixed;
z-index: 5;
right: 16px;
top: 16px;
padding: 8px 11px;
border-radius: 9px;
background: rgba(18, 24, 36, 0.72);
color: white;
font-size: 11px;
pointer-events: none;
}
@media (max-width: 760px) {
#rules {
display: none;
}
#cameraHint {
top: auto;
bottom: 12px;
}
}
</style>
</head>
<body>
<div id="hud" class="panel">
<div class="eyebrow">Voxel physics demonstration</div>
<h1>One table-tennis return</h1>
<div id="status">Toss — waiting for geometric paddle overlap.</div>
<div class="metrics">
<div class="metric">
<strong id="contactCount">0</strong>
<span>Paddle overlaps</span>
</div>
<div class="metric">
<strong id="bounceCount">0</strong>
<span>Legal bounces</span>
</div>
</div>
<div class="buttons">
<button id="restart">Restart same ball</button>
<button id="cameraReset">Reset camera</button>
</div>
</div>
<div id="rules" class="panel">
<strong>Stated interaction rules</strong>
<ol>
<li>The orange cube is one persistent mesh from toss through the return.</li>
<li>A paddle contact is counted only when the ball’s world-space AABB overlaps the solid paddle-head AABB.</li>
<li>The first legal table bounce must be on the receiver’s half.</li>
<li>The receiver may return only after that bounce and another paddle overlap.</li>
<li>The returned ball must then bounce once on the server’s half.</li>
<li>Legal bounces reflect vertical velocity with restitution 0.82 and apply 0.985 horizontal damping.</li>
<li>A net-plane overlap ends the demonstration as a fault.</li>
</ol>
<ul id="events"></ul>
</div>
<div id="cameraHint">Drag to orbit · Wheel to zoom</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ---------------------------------------------------------------------
// Scene and constants
// ---------------------------------------------------------------------
const TABLE_WIDTH = 3.6;
const TABLE_LENGTH = 5.6;
const TABLE_TOP = 1.5;
const TABLE_THICKNESS = 0.22;
const BALL_SIZE = 0.18;
const BALL_HALF = BALL_SIZE / 2;
const GRAVITY = 9.8;
const FIXED_DT = 1 / 240;
const statusEl = document.querySelector("#status");
const contactCountEl = document.querySelector("#contactCount");
const bounceCountEl = document.querySelector("#bounceCount");
const eventsEl = document.querySelector("#events");
const restartButton = document.querySelector("#restart");
const cameraResetButton = document.querySelector("#cameraReset");
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(devicePixelRatio, 1.75));
renderer.setSize(innerWidth, innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
document.body.prepend(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x9fc5df);
scene.fog = new THREE.Fog(0x9fc5df, 17, 32);
const camera = new THREE.PerspectiveCamera(
43,
innerWidth / innerHeight,
0.1,
80
);
const cameraTarget = new THREE.Vector3(0, 1.35, 0);
let cameraTheta = 0.67;
let cameraPhi = 1.0;
let cameraRadius = 11.2;
function updateCamera() {
const horizontal = Math.sin(cameraPhi) * cameraRadius;
camera.position.set(
cameraTarget.x + horizontal * Math.sin(cameraTheta),
cameraTarget.y + Math.cos(cameraPhi) * cameraRadius,
cameraTarget.z + horizontal * Math.cos(cameraTheta)
);
camera.lookAt(cameraTarget);
}
updateCamera();
// ---------------------------------------------------------------------
// Lighting and procedural voxel environment
// ---------------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0xd9efff, 0x65704d, 2.25));
const sun = new THREE.DirectionalLight(0xfff1cf, 3.0);
sun.position.set(5, 10, 6);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -9;
sun.shadow.camera.right = 9;
sun.shadow.camera.top = 9;
sun.shadow.camera.bottom = -9;
sun.shadow.camera.near = 0.5;
sun.shadow.camera.far = 30;
sun.shadow.bias = -0.0004;
scene.add(sun);
function standardMaterial(color, extra = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.82,
metalness: 0.02,
...extra
});
}
function addBox(
width, height, depth, color,
x = 0, y = 0, z = 0,
parent = scene,
extra = {}
) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
standardMaterial(color, extra)
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// Ground.
addBox(24, 0.25, 20, 0x7ea96b, 0, -0.145, 0);
// Table top and edge treatment.
const tableMesh = addBox(
TABLE_WIDTH, TABLE_THICKNESS, TABLE_LENGTH,
0xf5f1e8,
0, TABLE_TOP - TABLE_THICKNESS / 2, 0
);
addBox(
0.09, 0.012, TABLE_LENGTH - 0.04,
0x2366a8,
-TABLE_WIDTH / 2 + 0.045, TABLE_TOP + 0.001, 0
);
addBox(
0.09, 0.012, TABLE_LENGTH - 0.04,
0x2366a8,
TABLE_WIDTH / 2 - 0.045, TABLE_TOP + 0.001, 0
);
addBox(
TABLE_WIDTH - 0.12, 0.012, 0.09,
0x2366a8,
0, TABLE_TOP + 0.001, -TABLE_LENGTH / 2 + 0.045
);
addBox(
TABLE_WIDTH - 0.12, 0.012, 0.09,
0x2366a8,
0, TABLE_TOP + 0.001, TABLE_LENGTH / 2 - 0.045
);
// Table center line.
addBox(
0.035, 0.012, TABLE_LENGTH - 0.12,
0x38536a,
0, TABLE_TOP + 0.002, 0
);
// Table legs.
for (const x of [-1.38, 1.38]) {
for (const z of [-2.15, 2.15]) {
addBox(0.22, 1.39, 0.22, 0x344052, x, 0.695, z);
}
}
addBox(2.95, 0.16, 0.18, 0x344052, 0, 0.55, -2.15);
addBox(2.95, 0.16, 0.18, 0x344052, 0, 0.55, 2.15);
// Net posts.
for (const x of [-1.94, 1.94]) {
addBox(0.13, 2.55, 0.13, 0x334052, x, 1.275, 0);
addBox(0.38, 0.12, 0.38, 0x263241, x, 0.06, 0);
}
// Net tape.
addBox(
3.88, 0.075, 0.07,
0xf0f2ef,
0, 2.225, 0
);
// Pixel-grid net.
for (let x = -1.65; x <= 1.66; x += 0.275) {
addBox(
0.018, 0.68, 0.022,
0x34475b,
x, 1.85, 0,
scene,
{ transparent: true, opacity: 0.86 }
);
}
for (let y = 1.54; y <= 2.19; y += 0.13) {
addBox(
3.34, 0.018, 0.022,
0x34475b,
0, y, 0,
scene,
{ transparent: true, opacity: 0.86 }
);
}
// ---------------------------------------------------------------------
// The single, persistent ball mesh
// ---------------------------------------------------------------------
const ball = new THREE.Mesh(
new THREE.BoxGeometry(BALL_SIZE, BALL_SIZE, BALL_SIZE),
standardMaterial(0xffa51f, {
emissive: 0x6f2c00,
emissiveIntensity: 0.12
})
);
ball.name = "Persistent orange voxel ball";
ball.castShadow = true;
ball.receiveShadow = true;
scene.add(ball);
const ballVelocity = new THREE.Vector3();
const ballBox = new THREE.Box3();
// ---------------------------------------------------------------------
// Voxel paddles
// ---------------------------------------------------------------------
function createPaddle(baseColor, accentColor) {
const group = new THREE.Group();
const baseMaterial = standardMaterial(0xb9783e);
const accentMaterial = standardMaterial(accentColor, {
emissive: accentColor,
emissiveIntensity: 0
});
// This solid head is also the exact geometric collider.
const head = new THREE.Mesh(
new THREE.BoxGeometry(0.8, 0.8, 0.14),
baseMaterial
);
head.name = "Solid paddle head collider";
head.castShadow = true;
head.receiveShadow = true;
group.add(head);
// Pixel tiles on both faces for the voxel look.
for (const side of [-1, 1]) {
for (let row = -1; row <= 1; row++) {
for (let column = -1; column <= 1; column++) {
const tile = new THREE.Mesh(
new THREE.BoxGeometry(0.19, 0.19, 0.018),
accentMaterial
);
tile.position.set(
column * 0.215,
row * 0.215,
side * 0.078
);
tile.castShadow = true;
group.add(tile);
}
}
}
// Handle is visual only; the solid head defines paddle contact.
const handle = new THREE.Mesh(
new THREE.BoxGeometry(0.17, 0.58, 0.14),
baseMaterial
);
handle.position.y = -0.66;
handle.castShadow = true;
group.add(handle);
scene.add(group);
return {
group,
head,
baseMaterial,
accentMaterial,
flash: 0
};
}
const serverPaddle = createPaddle(0xe75b45, 0xf06b52);
const receiverPaddle = createPaddle(0x287cc1, 0x3999df);
// ---------------------------------------------------------------------
// Blocky players and articulated voxel arms
// ---------------------------------------------------------------------
const upVector = new THREE.Vector3(0, 1, 0);
function setBeam(mesh, from, to) {
const direction = new THREE.Vector3().subVectors(to, from);
const length = direction.length();
mesh.position.copy(from).add(to).multiplyScalar(0.5);
mesh.scale.set(1, Math.max(length, 0.001), 1);
mesh.quaternion.setFromUnitVectors(
upVector,
direction.normalize()
);
}
function createVoxelPlayer(x, z, facing, shirtColor) {
const group = new THREE.Group();
group.position.set(x, 0, z);
const skin = 0xe5ad78;
const shirt = standardMaterial(shirtColor);
const shorts = standardMaterial(0x273448);
const skinMaterial = standardMaterial(skin);
addBox(0.25, 0.78, 0.29, 0x273448, -0.18, 0.39, 0, group);
addBox(0.25, 0.78, 0.29, 0x273448, 0.18, 0.39, 0, group);
addBox(0.74, 0.9, 0.43, shirtColor, 0, 1.2, 0, group);
addBox(0.78, 0.25, 0.46, 0x273448, 0, 0.79, 0, group);
addBox(0.2, 0.18, 0.2, skin, 0, 1.73, 0, group);
addBox(0.53, 0.53, 0.5, skin, 0, 2.02, 0, group);
addBox(
0.55, 0.12, 0.52,
0x3b2d28,
0, 2.25, facing * 0.015,
group
);
addBox(
0.065, 0.065, 0.035,
0x17202a,
-0.12, 2.06, facing * 0.262,
group
);
addBox(
0.065, 0.065, 0.035,
0x17202a,
0.12, 2.06, facing * 0.262,
group
);
const staticArm = new THREE.Mesh(
new THREE.BoxGeometry(0.19, 1, 0.19),
shirt
);
staticArm.castShadow = true;
scene.add(staticArm);
const shoulder = new THREE.Vector3(
x,
1.6,
z + facing * 0.22
);
const outerHand = new THREE.Vector3(
x + (facing < 0 ? -0.48 : 0.48),
0.96,
z + facing * 0.08
);
setBeam(staticArm, shoulder, outerHand);
const paddleArm = new THREE.Mesh(
new THREE.BoxGeometry(0.19, 1, 0.19),
shirt
);
paddleArm.castShadow = true;
scene.add(paddleArm);
const hand = new THREE.Mesh(
new THREE.BoxGeometry(0.22, 0.22, 0.22),
skinMaterial
);
hand.castShadow = true;
scene.add(hand);
return { shoulder, paddleArm, hand };
}
const serverPlayer = createVoxelPlayer(
-1.22, 3.35, -1, 0xe75b45
);
const receiverPlayer = createVoxelPlayer(
0.88, -3.38, 1, 0x287cc1
);
function updatePlayerArm(player, paddle) {
const handPosition = paddle.group.position.clone();
handPosition.y -= 0.78;
player.hand.position.copy(handPosition);
setBeam(player.paddleArm, player.shoulder, handPosition);
}
// ---------------------------------------------------------------------
// Paddle animation keyframes
// ---------------------------------------------------------------------
const serverFrames = [
{ t: 0.00, p: [-0.82, 1.65, 2.98] },
{ t: 0.35, p: [-0.78, 1.90, 2.84] },
{ t: 0.62, p: [-0.70, 2.58, 2.60] },
{ t: 0.90, p: [-0.70, 2.90, 2.57] },
{ t: 1.15, p: [-0.70, 2.68, 2.78] },
{ t: 1.70, p: [-0.55, 2.30, 2.88] },
{ t: 2.15, p: [ 0.35, 2.65, 2.98] },
{ t: 2.70, p: [ 1.15, 2.55, 2.96] }
];
const receiverFrames = [
{ t: 0.00, p: [-0.47, 1.85, -3.02] },
{ t: 0.70, p: [-0.47, 1.95, -2.84] },
{ t: 1.10, p: [-0.47, 2.10, -2.68] },
{ t: 1.40, p: [-0.47, 2.27, -2.62] },
{ t: 1.80, p: [-0.40, 2.10, -2.56] },
{ t: 2.40, p: [ 0.10, 2.32, -2.70] },
{ t: 3.00, p: [ 0.55, 2.22, -2.80] }
];
function sampleFrames(frames, time) {
if (time <= frames[0].t) {
return new THREE.Vector3(...frames[0].p);
}
const last = frames[frames.length - 1];
if (time >= last.t) {
return new THREE.Vector3(...last.p);
}
for (let i = 0; i < frames.length - 1; i++) {
const a = frames[i];
const b = frames[i + 1];
if (time >= a.t && time <= b.t) {
const alpha = (time - a.t) / (b.t - a.t);
return new THREE.Vector3(
THREE.MathUtils.lerp(a.p[0], b.p[0], alpha),
THREE.MathUtils.lerp(a.p[1], b.p[1], alpha),
THREE.MathUtils.lerp(a.p[2], b.p[2], alpha)
);
}
}
return new THREE.Vector3(...last.p);
}
function updatePaddles(time) {
serverPaddle.group.position.copy(sampleFrames(serverFrames, time));
receiverPaddle.group.position.copy(sampleFrames(receiverFrames, time));
updatePlayerArm(serverPlayer, serverPaddle);
updatePlayerArm(receiverPlayer, receiverPaddle);
serverPaddle.flash = Math.max(0, serverPaddle.flash - FIXED_DT * 4);
receiverPaddle.flash = Math.max(0, receiverPaddle.flash - FIXED_DT * 4);
serverPaddle.accentMaterial.emissiveIntensity = serverPaddle.flash * 1.3;
receiverPaddle.accentMaterial.emissiveIntensity = receiverPaddle.flash * 1.3;
}
// ---------------------------------------------------------------------
// Collision volumes
// ---------------------------------------------------------------------
tableMesh.updateWorldMatrix(true, true);
const tableBox = new THREE.Box3().setFromObject(tableMesh);
const serverPaddleBox = new THREE.Box3();
const receiverPaddleBox = new THREE.Box3();
// The net is treated as a thin collision plane.
const netBox = new THREE.Box3(
new THREE.Vector3(-1.72, TABLE_TOP, -0.06),
new THREE.Vector3(1.72, 2.24, 0.06)
);
// ---------------------------------------------------------------------
// Simulation state
// ---------------------------------------------------------------------
let simulationTime = 0;
let accumulator = 0;
let phase = "toss";
let paddleContacts = 0;
let legalBounces = 0;
let serverWasOverlapping = false;
let receiverWasOverlapping = false;
let tableWasOverlapping = false;
function setStatus(text) {
statusEl.textContent = text;
}
function recordEvent(text) {
const item = document.createElement("li");
const time = document.createElement("time");
const label = document.createElement("span");
time.textContent = `${simulationTime.toFixed(2)}s`;
label.textContent = text;
item.append(time, label);
eventsEl.prepend(item);
}
function updateCounters() {
contactCountEl.textContent = String(paddleContacts);
bounceCountEl.textContent = String(legalBounces);
}
function reflectBall(restitution, horizontalDamping = 0.985) {
ball.position.y = TABLE_TOP + BALL_HALF;
ballVelocity.y = Math.abs(ballVelocity.y) * restitution;
ballVelocity.x *= horizontalDamping;
ballVelocity.z *= horizontalDamping;
}
function resetSimulation() {
simulationTime = 0;
accumulator = 0;
phase = "toss";
paddleContacts = 0;
legalBounces = 0;
serverWasOverlapping = false;
receiverWasOverlapping = false;
tableWasOverlapping = false;
// The same existing ball mesh is reused; it is never replaced.
ball.position.set(-0.70, 2.35, 3.00);
ball.scale.set(1, 1, 1);
ball.visible = true;
ballVelocity.set(0, 3.30, -0.65);
updatePaddles(0);
updateCounters();
eventsEl.replaceChildren();
setStatus("Toss — waiting for geometric paddle overlap.");
recordEvent("Persistent ball mesh reset for a new attempt.");
}
function integrateBall(dt) {
ballVelocity.y -= GRAVITY * dt;
ball.position.addScaledVector(ballVelocity, dt);
}
function handlePaddleContacts(serverOverlap, receiverOverlap) {
if (serverOverlap && !serverWasOverlapping) {
paddleContacts++;
serverPaddle.flash = 1;
updateCounters();
if (phase === "toss") {
// Simplified serve response, applied only at actual overlap.
ballVelocity.set(0.25, 2.80, -5.20);
phase = "serveFlight";
setStatus("Serve contact — ball travelling toward the receiver’s half.");
recordEvent("Server paddle AABB overlapped the ball.");
} else {
recordEvent("Server paddle overlapped the ball.");
}
}
if (receiverOverlap && !receiverWasOverlapping) {
paddleContacts++;
receiverPaddle.flash = 1;
updateCounters();
if (phase === "receiverReady") {
// Simplified return response, applied only at actual overlap.
ballVelocity.set(0.15, 3.10, 5.20);
phase = "returnFlight";
setStatus("Return contact — ball travelling toward the server’s half.");
recordEvent("Receiver paddle AABB overlapped the ball.");
} else {
recordEvent("Receiver paddle overlapped the ball.");
}
}
serverWasOverlapping = serverOverlap;
receiverWasOverlapping = receiverOverlap;
}
function handleTableOverlap(overlapping) {
if (overlapping && !tableWasOverlapping) {
if (
phase === "serveFlight" &&
ball.position.z < 0
) {
legalBounces++;
updateCounters();
reflectBall(0.82);
phase = "receiverReady";
setStatus("Legal receiver-side bounce — awaiting a paddle overlap.");
recordEvent("Ball overlapped the table on the receiver’s half.");
} else if (
phase === "returnFlight" &&
ball.position.z > 0
) {
legalBounces++;
updateCounters();
reflectBall(0.82);
phase = "complete";
setStatus("Complete — one serve, one return, and one legal return bounce.");
recordEvent("Returned ball overlapped the table on the server’s half.");
} else {
// Non-rally contacts use a softer response.
reflectBall(0.30);
}
}
tableWasOverlapping = overlapping;
}
function step(dt) {
simulationTime += dt;
updatePaddles(simulationTime);
integrateBall(dt);
// Refresh only the world matrices needed by the colliders.
serverPaddle.group.updateWorldMatrix(true, true);
receiverPaddle.group.updateWorldMatrix(true, true);
ball.updateWorldMatrix(true, false);
ballBox.setFromObject(ball);
serverPaddleBox.setFromObject(serverPaddle.head);
receiverPaddleBox.setFromObject(receiverPaddle.head);
const serverOverlap = ballBox.intersectsBox(serverPaddleBox);
const receiverOverlap = ballBox.intersectsBox(receiverPaddleBox);
const tableOverlap = ballBox.intersectsBox(tableBox);
handlePaddleContacts(serverOverlap, receiverOverlap);
handleTableOverlap(tableOverlap);
if (
phase !== "fault" &&
phase !== "complete" &&
ballBox.intersectsBox(netBox)
) {
phase = "fault";
ballVelocity.z *= -0.25;
ballVelocity.y = Math.max(0.6, Math.abs(ballVelocity.y) * 0.3);
ballVelocity.x *= 0.4;
setStatus("Fault — the ball geometrically overlapped the net plane.");
recordEvent("Net-plane overlap ended the rally.");
}
}
// ---------------------------------------------------------------------
// Direct camera controls, implemented without Three.js addons
// ---------------------------------------------------------------------
let dragging = false;
let pointerX = 0;
let pointerY = 0;
renderer.domElement.addEventListener("pointerdown", event => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", event => {
if (!dragging) return;
const deltaX = event.clientX - pointerX;
const deltaY = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
cameraTheta -= deltaX * 0.006;
cameraPhi = THREE.MathUtils.clamp(
cameraPhi - deltaY * 0.005,
0.28,
1.42
);
updateCamera();
});
renderer.domElement.addEventListener("pointerup", event => {
dragging = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
});
renderer.domElement.addEventListener("pointercancel", () => {
dragging = false;
});
renderer.domElement.addEventListener("wheel", event => {
event.preventDefault();
cameraRadius = THREE.MathUtils.clamp(
cameraRadius * Math.exp(event.deltaY * 0.001),
6.2,
18
);
updateCamera();
}, { passive: false });
function resetCamera() {
cameraTheta = 0.67;
cameraPhi = 1.0;
cameraRadius = 11.2;
updateCamera();
}
cameraResetButton.addEventListener("click", resetCamera);
restartButton.addEventListener("click", resetSimulation);
// ---------------------------------------------------------------------
// Resize and animation loop
// ---------------------------------------------------------------------
addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(devicePixelRatio, 1.75));
renderer.setSize(innerWidth, innerHeight);
});
resetSimulation();
let previousTime = performance.now();
function animate(now) {
requestAnimationFrame(animate);
const frameDelta = Math.min((now - previousTime) / 1000, 0.05);
previousTime = now;
accumulator += frameDelta;
while (accumulator >= FIXED_DT) {
step(FIXED_DT);
accumulator -= FIXED_DT;
}
renderer.render(scene, camera);
}
requestAnimationFrame(animate);
</script>
</body>
</html>
```