Prompt specification
Build or customize the Three.js 3D scene "Original Garden Paddle Game" 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: Original Garden Paddle Game
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/original-garden-paddle-game-x3631
Scene Description Prompt:
Original garden paddle game. Build a small original paddle game on an outdoor table with clear bounds. Use predictable collision rules, visible scoring, and resettable rallies without external physics libraries. Please build this with Three.js from cdnjs in one self-contained HTML file, using visible voxel blocks.
Working Three.js Starter (original-garden-paddle-game-x3631):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta
name="viewport"
content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"
>
<title>Voxel Garden Rally</title>
<style>
:root {
--cream: #fff5cf;
--ink: #173b35;
--orange: #f47b52;
--purple: #8164c7;
--green: #4f9b76;
--panel: rgba(255, 249, 220, 0.93);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #bce6ee;
color: var(--ink);
font-family: "Courier New", Courier, monospace;
}
#stage {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: crosshair;
}
#hud {
position: fixed;
inset: 0;
pointer-events: none;
user-select: none;
}
#scoreboard {
position: absolute;
top: 18px;
left: 50%;
display: flex;
align-items: stretch;
transform: translateX(-50%);
filter: drop-shadow(6px 7px 0 rgba(23, 59, 53, 0.2));
}
.score-card {
min-width: 142px;
padding: 11px 18px 9px;
text-align: center;
background: var(--panel);
border: 4px solid var(--ink);
}
.score-card.player {
border-right-width: 2px;
}
.score-card.bot {
border-left-width: 2px;
}
.score-label {
display: block;
margin-bottom: 2px;
font-size: 12px;
font-weight: 900;
letter-spacing: 1px;
}
.score-value {
display: block;
font-size: clamp(30px, 5vw, 48px);
line-height: 0.95;
font-weight: 900;
}
.player .score-value {
color: var(--orange);
}
.bot .score-value {
color: var(--purple);
}
.target-card {
display: grid;
place-items: center;
padding: 8px 12px;
color: var(--cream);
background: var(--ink);
border: 4px solid var(--ink);
font-size: 11px;
font-weight: 900;
line-height: 1.35;
text-align: center;
letter-spacing: 1px;
}
#message {
position: absolute;
top: 25%;
left: 50%;
width: min(430px, calc(100vw - 34px));
padding: 14px 18px 12px;
transform: translateX(-50%);
text-align: center;
background: var(--panel);
border: 4px solid var(--ink);
box-shadow: 7px 7px 0 rgba(23, 59, 53, 0.22);
transition: transform 120ms ease, opacity 120ms ease;
}
#message.pop {
transform: translateX(-50%) scale(1.04);
}
#message[data-tone="player"] {
border-color: var(--orange);
}
#message[data-tone="bot"] {
border-color: var(--purple);
}
#messageTitle {
font-size: clamp(19px, 3vw, 29px);
font-weight: 900;
letter-spacing: 1px;
}
#messageSub {
margin-top: 5px;
font-size: 12px;
line-height: 1.35;
}
#meters {
position: absolute;
top: 130px;
left: 50%;
display: flex;
gap: 8px;
transform: translateX(-50%);
}
.meter {
padding: 6px 9px;
border: 3px solid var(--ink);
background: rgba(255, 249, 220, 0.9);
font-size: 11px;
font-weight: 900;
letter-spacing: 0.6px;
}
#controls {
position: absolute;
right: 18px;
bottom: 18px;
display: flex;
gap: 8px;
pointer-events: auto;
}
button {
min-height: 43px;
padding: 9px 13px;
border: 3px solid var(--ink);
border-radius: 0;
color: var(--ink);
background: var(--cream);
box-shadow: 4px 4px 0 rgba(23, 59, 53, 0.25);
font: 900 12px "Courier New", Courier, monospace;
letter-spacing: 0.5px;
cursor: pointer;
touch-action: manipulation;
}
button:hover:not(:disabled) {
transform: translate(1px, 1px);
box-shadow: 3px 3px 0 rgba(23, 59, 53, 0.25);
}
button:active:not(:disabled) {
transform: translate(3px, 3px);
box-shadow: 1px 1px 0 rgba(23, 59, 53, 0.25);
}
button:disabled {
opacity: 0.45;
cursor: default;
}
#serveButton {
color: #fff9e8;
background: var(--orange);
}
#help {
position: absolute;
bottom: 21px;
left: 20px;
max-width: calc(100vw - 390px);
padding: 8px 10px;
border: 2px solid rgba(23, 59, 53, 0.7);
background: rgba(255, 249, 220, 0.78);
font-size: 11px;
font-weight: 700;
line-height: 1.45;
}
@media (max-width: 720px) {
#scoreboard {
top: 10px;
}
.score-card {
min-width: 105px;
padding: 8px 10px 7px;
}
.target-card {
padding: 7px 8px;
font-size: 9px;
}
#meters {
top: 104px;
}
#message {
top: 27%;
}
#help {
display: none;
}
#controls {
right: 10px;
bottom: 10px;
left: 10px;
justify-content: center;
}
button {
flex: 1;
padding-right: 7px;
padding-left: 7px;
font-size: 10px;
}
}
</style>
</head>
<body>
<main id="stage" aria-label="Voxel Garden Rally game"></main>
<div id="hud">
<div id="scoreboard" aria-live="polite">
<div class="score-card player">
<span class="score-label">YOU</span>
<strong class="score-value" id="playerScore">0</strong>
</div>
<div class="target-card">
FIRST TO<br>7
</div>
<div class="score-card bot">
<span class="score-label">GARDEN BOT</span>
<strong class="score-value" id="botScore">0</strong>
</div>
</div>
<div id="meters">
<div class="meter">RALLY: <span id="rallyCount">0</span></div>
<div class="meter">SPEED: <span id="speedValue">7.5</span></div>
</div>
<div id="message" data-tone="">
<div id="messageTitle">READY TO SERVE</div>
<div id="messageSub">Press Space or tap the table.</div>
</div>
<div id="help">
MOVE: mouse / drag / ← → / A D<br>
SERVE: Space · RESET RALLY: R · FIRST TO 7
</div>
<div id="controls">
<button id="serveButton" type="button">SERVE</button>
<button id="resetButton" type="button">RESET RALLY</button>
<button id="newButton" type="button">NEW MATCH</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";
// -------------------------------------------------------------------------
// DOM and renderer
// -------------------------------------------------------------------------
const stage = document.querySelector("#stage");
const playerScoreElement = document.querySelector("#playerScore");
const botScoreElement = document.querySelector("#botScore");
const rallyCountElement = document.querySelector("#rallyCount");
const speedValueElement = document.querySelector("#speedValue");
const messageElement = document.querySelector("#message");
const messageTitleElement = document.querySelector("#messageTitle");
const messageSubElement = document.querySelector("#messageSub");
const serveButton = document.querySelector("#serveButton");
const resetButton = document.querySelector("#resetButton");
const newButton = document.querySelector("#newButton");
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
stage.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xbce6ee);
scene.fog = new THREE.Fog(0xbce6ee, 32, 64);
const camera = new THREE.OrthographicCamera(-18, 18, 11, -11, 0.1, 100);
camera.position.set(7.2, 15.5, 18.5);
camera.lookAt(0, 0, -0.2);
const hemisphere = new THREE.HemisphereLight(0xe9fbff, 0x4d7148, 2.2);
scene.add(hemisphere);
const sunlight = new THREE.DirectionalLight(0xfff3c4, 2.7);
sunlight.position.set(-9, 18, 11);
sunlight.castShadow = true;
sunlight.shadow.mapSize.set(2048, 2048);
sunlight.shadow.camera.left = -18;
sunlight.shadow.camera.right = 18;
sunlight.shadow.camera.top = 18;
sunlight.shadow.camera.bottom = -18;
sunlight.shadow.camera.near = 1;
sunlight.shadow.camera.far = 45;
sunlight.shadow.bias = -0.0006;
scene.add(sunlight);
const unitBox = new THREE.BoxGeometry(1, 1, 1);
function block(
parent,
width,
height,
depth,
x,
y,
z,
material,
castShadow = true,
receiveShadow = true
) {
const mesh = new THREE.Mesh(unitBox, material);
mesh.scale.set(width, height, depth);
mesh.position.set(x, y, z);
mesh.castShadow = castShadow;
mesh.receiveShadow = receiveShadow;
parent.add(mesh);
return mesh;
}
function standard(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.78,
metalness: 0,
...options
});
}
// -------------------------------------------------------------------------
// Garden table, court, and environment
// -------------------------------------------------------------------------
const grassMaterial = standard(0x74aa65);
const soilMaterial = standard(0x77513c);
const courtMaterial = standard(0x4d896d);
const creamMaterial = standard(0xfff2c4);
const yellowMaterial = standard(0xf4ce55);
const orangeMaterial = standard(0xf47b52, {
emissive: 0xf47b52,
emissiveIntensity: 0.06
});
const purpleMaterial = standard(0x8164c7, {
emissive: 0x8164c7,
emissiveIntensity: 0.06
});
const peaMaterial = standard(0xc8ef64, {
emissive: 0x83a931,
emissiveIntensity: 0.15
});
const stemMaterial = standard(0x3f7e4f);
const leafMaterial = standard(0x4f9b55);
const trunkMaterial = standard(0x8b5c3e);
const cloudMaterial = standard(0xf7fbec, {
roughness: 1
});
const sunMaterial = standard(0xffdc58, {
emissive: 0xffb52e,
emissiveIntensity: 0.4
});
const woodMaterials = [
standard(0xc98a50),
standard(0xd99a5c),
standard(0xbf7e48),
standard(0xe1a665)
];
// Grass and soil.
block(scene, 70, 0.3, 70, 0, -1.25, 0, grassMaterial, false, true);
block(scene, 14.5, 0.08, 19.5, 0, -1.06, 0, soilMaterial, false, true);
// Table legs and braces.
for (const x of [-5.3, 5.3]) {
for (const z of [-7.65, 7.65]) {
block(scene, 0.62, 0.7, 0.62, x, -0.76, z, woodMaterials[2]);
}
}
block(scene, 12.7, 0.24, 0.34, 0, -0.51, -7.65, woodMaterials[2]);
block(scene, 12.7, 0.24, 0.34, 0, -0.51, 7.65, woodMaterials[2]);
block(scene, 0.34, 0.24, 15.7, -5.3, -0.51, 0, woodMaterials[2]);
block(scene, 0.34, 0.24, 15.7, 5.3, -0.51, 0, woodMaterials[2]);
// Individual wooden table planks.
const plankCount = 9;
const tableWidth = 12.4;
const plankWidth = tableWidth / plankCount;
for (let i = 0; i < plankCount; i++) {
const x = -tableWidth / 2 + (i + 0.5) * plankWidth;
block(
scene,
plankWidth - 0.035,
0.42,
17.4,
x,
-0.21,
0,
woodMaterials[i % woodMaterials.length]
);
}
const HALF_WIDTH = 5.25;
const HALF_LENGTH = 7.85;
const PLAYER_Z = 6.25;
const BOT_Z = -6.25;
const PADDLE_WIDTH = 1.75;
const PADDLE_DEPTH = 0.58;
const BALL_RADIUS = 0.32;
const MATCH_TARGET = 7;
// Playing mat.
block(scene, 10.95, 0.08, 15.95, 0, 0.04, 0, courtMaterial);
// Clearly voxelized boundary rails.
const railCount = 20;
const railLength = 15.9 / railCount;
for (let i = 0; i < railCount; i++) {
const z = -15.9 / 2 + (i + 0.5) * railLength;
block(scene, 0.25, 0.24, railLength - 0.035, -5.39, 0.17, z, creamMaterial);
block(scene, 0.25, 0.24, railLength - 0.035, 5.39, 0.17, z, creamMaterial);
}
for (let i = 0; i < 14; i++) {
const x = -5.25 + (i + 0.5) * (10.5 / 14);
block(scene, 10.5 / 14 - 0.035, 0.24, 0.25, x, 0.17, -7.98, creamMaterial);
block(scene, 10.5 / 14 - 0.035, 0.24, 0.25, x, 0.17, 7.98, creamMaterial);
}
// Bright corner markers.
for (const x of [-5.39, 5.39]) {
for (const z of [-7.98, 7.98]) {
block(scene, 0.42, 0.34, 0.42, x, 0.2, z, yellowMaterial);
}
}
// Dashed center line.
for (let z = -6.5; z <= 6.5; z += 1.3) {
block(scene, 0.13, 0.025, 0.64, 0, 0.095, z, creamMaterial, false, false);
}
// Small block-letter labels made from actual cubes.
const glyphs = {
Y: ["101", "101", "010", "010", "010"],
O: ["111", "101", "101", "101", "111"],
U: ["101", "101", "101", "101", "111"],
B: ["110", "101", "110", "101", "110"],
T: ["111", "010", "010", "010", "010"]
};
function voxelText(text, centerX, z, material, cell = 0.16) {
const textWidth = (text.length * 4 - 1) * cell;
const startX = centerX - textWidth / 2;
[...text].forEach((character, characterIndex) => {
const pattern = glyphs[character];
if (!pattern) return;
pattern.forEach((row, rowIndex) => {
[...row].forEach((filled, columnIndex) => {
if (filled !== "1") return;
block(
scene,
cell * 0.86,
0.035,
cell * 0.86,
startX + (characterIndex * 4 + columnIndex + 0.5) * cell,
0.095,
z + (2 - rowIndex) * cell,
material,
false,
false
);
});
});
});
}
voxelText("YOU", 0, 7.38, creamMaterial);
voxelText("BOT", 0, -7.38, creamMaterial);
// Voxel trees.
function makeTree(x, z, scale = 1) {
const tree = new THREE.Group();
tree.position.set(x, -1.1, z);
tree.scale.setScalar(scale);
block(tree, 0.55, 1.8, 0.55, 0, 0.9, 0, trunkMaterial);
const leafOffsets = [
[-0.7, 0, -0.7], [0, 0, -0.7], [0.7, 0, -0.7],
[-0.7, 0, 0], [0, 0, 0], [0.7, 0, 0],
[-0.7, 0, 0.7], [0, 0, 0.7], [0.7, 0, 0.7],
[-0.45, 0.72, -0.4], [0.45, 0.72, 0.4]
];
for (const [lx, ly, lz] of leafOffsets) {
block(
tree,
0.92,
0.85,
0.92,
lx,
2.05 + ly,
lz,
leafMaterial
);
}
scene.add(tree);
return tree;
}
makeTree(-9.1, -6.4, 1.05);
makeTree(-10.4, 3.9, 0.9);
makeTree(-7.3, 10.5, 0.85);
makeTree(7.4, -10.2, 1.0);
makeTree(10.4, -3.2, 0.82);
// Small voxel flowers.
const flowerColors = [
standard(0xf47788),
standard(0xf5ca52),
standard(0xb48ae0),
standard(0xffffff)
];
for (let i = 0; i < 20; i++) {
const side = i % 2 === 0 ? -1 : 1;
const x = side * (6.8 + Math.random() * 2.3);
const z = -9 + Math.random() * 18;
const flowerMaterial = flowerColors[i % flowerColors.length];
block(scene, 0.09, 0.55, 0.09, x, -0.79, z, stemMaterial);
block(scene, 0.32, 0.08, 0.14, x + 0.12, -0.7, z, leafMaterial);
block(scene, 0.27, 0.25, 0.27, x, -0.42, z, flowerMaterial);
}
// Block sun and clouds.
const sunGroup = new THREE.Group();
sunGroup.position.set(-10.5, 5.5, -13.5);
block(sunGroup, 2.3, 2.3, 1.1, 0, 0, 0, sunMaterial, false, false);
block(sunGroup, 1.05, 3.25, 1.1, 0, 0, 0, sunMaterial, false, false);
block(sunGroup, 3.25, 1.05, 1.1, 0, 0, 0, sunMaterial, false, false);
scene.add(sunGroup);
const clouds = [];
function makeCloud(x, y, z, scale = 1) {
const cloud = new THREE.Group();
cloud.position.set(x, y, z);
cloud.scale.setScalar(scale);
block(cloud, 2.1, 0.72, 0.9, 0, 0, 0, cloudMaterial, false, false);
block(cloud, 1.25, 1.2, 0.9, -0.65, 0.45, 0, cloudMaterial, false, false);
block(cloud, 1.45, 1.55, 0.9, 0.2, 0.5, 0, cloudMaterial, false, false);
block(cloud, 1.15, 0.95, 0.9, 1.0, 0.3, 0, cloudMaterial, false, false);
cloud.userData.baseX = x;
scene.add(cloud);
clouds.push(cloud);
}
makeCloud(-5.5, 5.7, -13.5, 1.05);
makeCloud(6.2, 6.5, -15, 0.8);
// -------------------------------------------------------------------------
// Paddles and voxel pea
// -------------------------------------------------------------------------
function makePaddle(material, accentMaterial) {
const group = new THREE.Group();
const segments = [
[-0.59, 0.52],
[0, 0.57],
[0.59, 0.52]
];
for (const [x, width] of segments) {
block(group, width, 0.29, PADDLE_DEPTH, x, 0, 0, material);
}
// Raised square details preserve the blocky silhouette.
block(group, 0.22, 0.09, 0.26, -0.59, 0.18, 0, accentMaterial);
block(group, 0.24, 0.09, 0.26, 0, 0.18, 0, accentMaterial);
block(group, 0.22, 0.09, 0.26, 0.59, 0.18, 0, accentMaterial);
scene.add(group);
return group;
}
const playerPaddle = makePaddle(orangeMaterial, creamMaterial);
const botPaddle = makePaddle(purpleMaterial, creamMaterial);
const ballGroup = new THREE.Group();
block(ballGroup, 0.43, 0.43, 0.43, 0, 0, 0, peaMaterial);
block(ballGroup, 0.14, 0.14, 0.14, 0.25, 0, 0, peaMaterial);
block(ballGroup, 0.14, 0.14, 0.14, -0.25, 0, 0, peaMaterial);
block(ballGroup, 0.14, 0.14, 0.14, 0, 0.25, 0, peaMaterial);
block(ballGroup, 0.14, 0.14, 0.14, 0, -0.25, 0, peaMaterial);
scene.add(ballGroup);
const shadowMaterial = new THREE.MeshBasicMaterial({
color: 0x173b35,
transparent: true,
opacity: 0.2,
depthWrite: false
});
const ballShadow = block(
scene,
0.46,
0.012,
0.46,
0,
0.091,
0,
shadowMaterial,
false,
false
);
const trail = [];
const trailHistory = [];
for (let i = 0; i < 8; i++) {
const material = new THREE.MeshBasicMaterial({
color: 0xdfff78,
transparent: true,
opacity: 0.28 * (1 - i / 9),
depthWrite: false
});
const mesh = new THREE.Mesh(unitBox, material);
const size = 0.18 - i * 0.012;
mesh.scale.set(size, size, size);
mesh.visible = false;
scene.add(mesh);
trail.push(mesh);
}
// -------------------------------------------------------------------------
// Game state and predictable fixed-step rules
// -------------------------------------------------------------------------
const score = {
player: 0,
bot: 0
};
const ballPosition = new THREE.Vector3(0, 0.4, 0);
const ballVelocity = new THREE.Vector3();
let playerX = 0;
let botX = 0;
let phase = "ready";
let pointTimer = 0;
let rallyHits = 0;
let ballSpeed = 7.5;
let nextServeDirection = -1;
let serveSequence = 0;
let playerFlash = 0;
let botFlash = 0;
const PLAYER_SPEED = 9;
const POINTER_SPEED = 12;
const BOT_SPEED = 5.25;
const keys = {
left: false,
right: false
};
let pointerSteering = false;
let pointerTargetX = 0;
let lastKeyboardInput = -Infinity;
function clamp(value, minimum, maximum) {
return Math.max(minimum, Math.min(maximum, value));
}
function moveToward(current, target, maxDistance) {
const difference = target - current;
if (Math.abs(difference) <= maxDistance) return target;
return current + Math.sign(difference) * maxDistance;
}
function setMessage(title, subtitle, tone = "") {
messageTitleElement.textContent = title;
messageSubElement.textContent = subtitle;
messageElement.dataset.tone = tone;
messageElement.classList.remove("pop");
void messageElement.offsetWidth;
messageElement.classList.add("pop");
}
function updateScore() {
playerScoreElement.textContent = score.player;
botScoreElement.textContent = score.bot;
}
function updateButtons() {
serveButton.disabled = phase !== "ready";
serveButton.textContent = phase === "over" ? "MATCH OVER" : "SERVE";
}
function clearTrail() {
trailHistory.length = 0;
trail.forEach((mesh) => {
mesh.visible = false;
});
}
function resetBall(showMessage = true) {
phase = "ready";
pointTimer = 0;
rallyHits = 0;
ballSpeed = 7.5;
ballPosition.set(0, 0.4, 0);
ballVelocity.set(0, 0, 0);
clearTrail();
if (showMessage) {
setMessage(
"READY TO SERVE",
"Press Space, tap the table, or use Serve.",
""
);
}
updateButtons();
}
function newMatch() {
score.player = 0;
score.bot = 0;
nextServeDirection = -1;
serveSequence = 0;
playerX = 0;
botX = 0;
updateScore();
resetBall();
}
function serve() {
if (phase !== "ready") return;
phase = "play";
rallyHits = 0;
ballSpeed = 7.5;
// A repeating pattern keeps serves deterministic.
const horizontalSpeeds = [1.5, -1.5, 0];
const horizontal = horizontalSpeeds[serveSequence % horizontalSpeeds.length];
serveSequence++;
const vertical = nextServeDirection * Math.sqrt(
Math.max(0, ballSpeed * ballSpeed - horizontal * horizontal)
);
ballPosition.set(0, 0.4, 0);
ballVelocity.set(horizontal, 0, vertical);
setMessage(
"RALLY LIVE",
"Hit off-center to steer the pea. Keep it inside the cream rails.",
""
);
updateButtons();
}
function spawnBurst(position, color, count = 7, power = 2.3) {
for (let i = 0; i < count; i++) {
const material = new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: 1
});
const mesh = new THREE.Mesh(unitBox, material);
const size = 0.07 + Math.random() * 0.13;
mesh.scale.set(size, size, size);
mesh.position.copy(position);
scene.add(mesh);
particles.push({
mesh,
velocity: new THREE.Vector3(
(Math.random() - 0.5) * power,
1.1 + Math.random() * power,
(Math.random() - 0.5) * power
),
life: 0.45 + Math.random() * 0.35,
maxLife: 0.8
});
}
}
const particles = [];
function updateParticles(dt) {
for (let i = particles.length - 1; i >= 0; i--) {
const particle = particles[i];
particle.life -= dt;
particle.velocity.y -= 6 * dt;
particle.mesh.position.addScaledVector(particle.velocity, dt);
particle.mesh.rotation.x += dt * 5;
particle.mesh.rotation.z += dt * 4;
particle.mesh.material.opacity = clamp(particle.life / particle.maxLife, 0, 1);
if (particle.life <= 0) {
scene.remove(particle.mesh);
particle.mesh.material.dispose();
particles.splice(i, 1);
}
}
}
function paddleHit(isPlayer) {
const paddleX = isPlayer ? playerX : botX;
const offset = clamp(
(ballPosition.x - paddleX) / (PADDLE_WIDTH / 2),
-1,
1
);
const angle = offset * 0.78;
ballSpeed = Math.min(ballSpeed + 0.48, 13.5);
const horizontalSpeed = Math.sin(angle) * ballSpeed;
const verticalSpeed = Math.cos(angle) * ballSpeed;
ballVelocity.set(
horizontalSpeed,
0,
isPlayer ? -verticalSpeed : verticalSpeed
);
rallyHits++;
if (isPlayer) {
playerFlash = 1;
} else {
botFlash = 1;
}
spawnBurst(ballPosition, isPlayer ? 0xffd35a : 0xd9c6ff, 6, 2.1);
}
function awardPoint(winner) {
if (phase !== "play") return;
ballVelocity.set(0, 0, 0);
if (winner === "player") {
score.player++;
nextServeDirection = -1;
} else {
score.bot++;
nextServeDirection = 1;
}
updateScore();
const burstPosition = new THREE.Vector3(
clamp(ballPosition.x, -HALF_WIDTH, HALF_WIDTH),
0.3,
clamp(ballPosition.z, -HALF_LENGTH, HALF_LENGTH)
);
spawnBurst(
burstPosition,
winner === "player" ? 0xff9b68 : 0xc3a9ff,
20,
4
);
if (score.player >= MATCH_TARGET || score.bot >= MATCH_TARGET) {
phase = "over";
const playerWon = score.player >= MATCH_TARGET;
setMessage(
playerWon ? "GARDEN CHAMPION!" : "THE BOT WINS THIS ONE",
playerWon
? "Great rallying. Press Space or New Match to play again."
: "Try aiming away from the center of the bot's paddle.",
playerWon ? "player" : "bot"
);
} else {
phase = "point";
pointTimer = 1.25;
setMessage(
winner === "player" ? "POINT: YOU!" : "POINT: GARDEN BOT",
`${score.player} – ${score.bot}. A fresh rally is being prepared.`,
winner
);
}
updateButtons();
}
function updatePlayer(dt) {
const now = performance.now();
const keyboardActive = now - lastKeyboardInput < 180;
const direction = (keys.right ? 1 : 0) - (keys.left ? 1 : 0);
if (direction !== 0 && keyboardActive) {
playerX += direction * PLAYER_SPEED * dt;
} else if (pointerSteering) {
playerX = moveToward(
playerX,
pointerTargetX,
POINTER_SPEED * dt
);
}
const paddleLimit = HALF_WIDTH - PADDLE_WIDTH / 2;
playerX = clamp(playerX, -paddleLimit, paddleLimit);
}
function updateBot(dt) {
let targetX = 0;
if (phase === "play" && ballVelocity.z < 0) {
const timeToBot = (BOT_Z - ballPosition.z) / ballVelocity.z;
if (timeToBot > 0 && timeToBot < 2.5) {
const predictedX = ballPosition.x + ballVelocity.x * timeToBot;
targetX = clamp(
predictedX * 0.94,
-HALF_WIDTH + PADDLE_WIDTH / 2,
HALF_WIDTH - PADDLE_WIDTH / 2
);
}
}
botX = moveToward(botX, targetX, BOT_SPEED * dt);
const paddleLimit = HALF_WIDTH - PADDLE_WIDTH / 2;
botX = clamp(botX, -paddleLimit, paddleLimit);
}
function integrateBall(dt) {
const currentSpeed = Math.max(
ballVelocity.length(),
0.001
);
// Substeps prevent the pea from skipping through a paddle or wall.
const substeps = Math.max(
1,
Math.ceil(currentSpeed * dt / (BALL_RADIUS * 0.55))
);
const step = dt / substeps;
for (let i = 0; i < substeps; i++) {
if (phase !== "play") break;
const previousZ = ballPosition.z;
ballPosition.addScaledVector(ballVelocity, step);
// Side-wall reflection.
if (ballPosition.x - BALL_RADIUS < -HALF_WIDTH) {
ballPosition.x = -HALF_WIDTH + BALL_RADIUS;
ballVelocity.x = Math.abs(ballVelocity.x);
spawnBurst(ballPosition, 0xfff1b5, 3, 1.2);
} else if (ballPosition.x + BALL_RADIUS > HALF_WIDTH) {
ballPosition.x = HALF_WIDTH - BALL_RADIUS;
ballVelocity.x = -Math.abs(ballVelocity.x);
spawnBurst(ballPosition, 0xfff1b5, 3, 1.2);
}
const overlapsPlayer =
Math.abs(ballPosition.x - playerX) <=
PADDLE_WIDTH / 2 + BALL_RADIUS;
const overlapsBot =
Math.abs(ballPosition.x - botX) <=
PADDLE_WIDTH / 2 + BALL_RADIUS;
// The player paddle is on the positive-Z side.
const playerFace = PLAYER_Z - PADDLE_DEPTH / 2;
const playerCrossed =
previousZ + BALL_RADIUS <= playerFace + 0.025 &&
ballPosition.z + BALL_RADIUS >= playerFace;
if (
ballVelocity.z > 0 &&
overlapsPlayer &&
playerCrossed
) {
ballPosition.z = playerFace - BALL_RADIUS - 0.002;
paddleHit(true);
}
// The bot paddle is on the negative-Z side.
const botFace = BOT_Z + PADDLE_DEPTH / 2;
const botCrossed =
previousZ - BALL_RADIUS >= botFace - 0.025 &&
ballPosition.z - BALL_RADIUS <= botFace;
if (
ballVelocity.z < 0 &&
overlapsBot &&
botCrossed
) {
ballPosition.z = botFace + BALL_RADIUS + 0.002;
paddleHit(false);
}
// Crossing either goal line scores a point.
if (ballPosition.z - BALL_RADIUS > HALF_LENGTH) {
awardPoint("bot");
break;
}
if (ballPosition.z + BALL_RADIUS < -HALF_LENGTH) {
awardPoint("player");
break;
}
}
}
function updateGame(dt) {
updatePlayer(dt);
updateBot(dt);
if (phase === "play") {
integrateBall(dt);
} else if (phase === "point") {
pointTimer -= dt;
if (pointTimer <= 0) {
resetBall();
}
}
updateParticles(dt);
}
// -------------------------------------------------------------------------
// Input: mouse, touch, and keyboard
// -------------------------------------------------------------------------
const raycaster = new THREE.Raycaster();
const pointerNdc = new THREE.Vector2();
const pointerHit = new THREE.Vector3();
const controlPlane = new THREE.Plane(
new THREE.Vector3(0, 0, 1),
-PLAYER_Z
);
function updatePointer(event) {
const rect = renderer.domElement.getBoundingClientRect();
pointerNdc.set(
((event.clientX - rect.left) / rect.width) * 2 - 1,
-((event.clientY - rect.top) / rect.height) * 2 + 1
);
raycaster.setFromCamera(pointerNdc, camera);
if (raycaster.ray.intersectPlane(controlPlane, pointerHit)) {
const paddleLimit = HALF_WIDTH - PADDLE_WIDTH / 2;
pointerTargetX = clamp(pointerHit.x, -paddleLimit, paddleLimit);
}
}
renderer.domElement.addEventListener("pointermove", (event) => {
updatePointer(event);
pointerSteering = true;
});
renderer.domElement.addEventListener("pointerdown", (event) => {
updatePointer(event);
pointerSteering = true;
renderer.domElement.setPointerCapture(event.pointerId);
serve();
});
renderer.domElement.addEventListener("pointerup", (event) => {
if (event.pointerType !== "mouse") {
pointerSteering = false;
}
});
renderer.domElement.addEventListener("pointercancel", () => {
pointerSteering = false;
});
renderer.domElement.addEventListener("pointerleave", (event) => {
if (event.pointerType === "mouse") {
pointerSteering = false;
}
});
window.addEventListener("keydown", (event) => {
const controlledCodes = [
"ArrowLeft",
"ArrowRight",
"KeyA",
"KeyD",
"Space",
"KeyR",
"KeyN"
];
if (controlledCodes.includes(event.code)) {
event.preventDefault();
}
if (event.code === "ArrowLeft" || event.code === "KeyA") {
keys.left = true;
lastKeyboardInput = performance.now();
}
if (event.code === "ArrowRight" || event.code === "KeyD") {
keys.right = true;
lastKeyboardInput = performance.now();
}
if (event.code === "Space" && !event.repeat) {
if (phase === "ready") {
serve();
} else if (phase === "over") {
newMatch();
serve();
}
}
if (event.code === "KeyR" && !event.repeat) {
resetBall();
}
if (event.code === "KeyN" && !event.repeat) {
newMatch();
}
});
window.addEventListener("keyup", (event) => {
if (event.code === "ArrowLeft" || event.code === "KeyA") {
keys.left = false;
}
if (event.code === "ArrowRight" || event.code === "KeyD") {
keys.right = false;
}
});
serveButton.addEventListener("click", () => {
if (phase === "over") {
newMatch();
}
serve();
});
resetButton.addEventListener("click", () => {
resetBall();
});
newButton.addEventListener("click", () => {
newMatch();
});
// -------------------------------------------------------------------------
// Animation and responsive camera
// -------------------------------------------------------------------------
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
// Orthographic framing keeps the voxel court crisp and readable.
const halfHeight = Math.max(10.4, 9.2 / aspect);
camera.left = -halfHeight * aspect;
camera.right = halfHeight * aspect;
camera.top = halfHeight;
camera.bottom = -halfHeight;
camera.updateProjectionMatrix();
renderer.setSize(width, height);
}
window.addEventListener("resize", resize);
resize();
function updateVisuals(dt) {
playerPaddle.position.set(playerX, 0.285, PLAYER_Z);
botPaddle.position.set(botX, 0.285, BOT_Z);
ballGroup.position.copy(ballPosition);
if (phase === "play") {
ballGroup.rotation.x += dt * 2.4;
ballGroup.rotation.y += dt * 3.7;
ballGroup.rotation.z += dt * 1.8;
}
ballShadow.position.x = ballPosition.x;
ballShadow.position.z = ballPosition.z;
if (phase === "play") {
trailHistory.unshift(ballPosition.clone());
if (trailHistory.length > trail.length + 1) {
trailHistory.pop();
}
trail.forEach((mesh, index) => {
const source = trailHistory[index + 1] || ballPosition;
mesh.visible = true;
mesh.position.copy(source);
mesh.position.y = 0.4;
});
} else {
trail.forEach((mesh) => {
mesh.visible = false;
});
}
playerFlash = Math.max(0, playerFlash - dt * 5);
botFlash = Math.max(0, botFlash - dt * 5);
orangeMaterial.emissiveIntensity = 0.06 + playerFlash * 0.75;
purpleMaterial.emissiveIntensity = 0.06 + botFlash * 0.75;
peaMaterial.emissiveIntensity = 0.15 + Math.sin(performance.now() * 0.004) * 0.04;
clouds.forEach((cloud, index) => {
cloud.position.x += dt * (0.18 + index * 0.04);
if (cloud.position.x > 12) {
cloud.position.x = -12;
}
});
rallyCountElement.textContent = String(rallyHits);
speedValueElement.textContent = ballSpeed.toFixed(1);
}
const FIXED_STEP = 1 / 120;
let previousTime = performance.now();
let accumulator = 0;
function animate(now) {
requestAnimationFrame(animate);
const frameTime = Math.min((now - previousTime) / 1000, 0.05);
previousTime = now;
accumulator += frameTime;
while (accumulator >= FIXED_STEP) {
updateGame(FIXED_STEP);
accumulator -= FIXED_STEP;
}
updateVisuals(frameTime);
renderer.render(scene, camera);
}
newMatch();
requestAnimationFrame(animate);
</script>
</body>
</html>
```