Prompt specification
Build or customize the Three.js 3D scene "Wheel Radius Distance Challenge" 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: Wheel Radius Distance Challenge
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/wheel-radius-distance-challenge-x3640
Scene Description Prompt:
Create a game matching a target travel distance by choosing wheel radius and rotation count. Use the circumference relation and verify the final position from actual motion. Use Three.js from cdnjs in a single HTML file, with visible voxel blocks.
Working Three.js Starter (wheel-radius-distance-challenge-x3640):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Voxel Roll — Target Distance Game</title>
<style>
:root {
--ink: #173042;
--muted: #617789;
--panel: rgba(248, 252, 252, 0.94);
--line: rgba(23, 48, 66, 0.12);
--teal: #168f8a;
--teal-dark: #0d6d6a;
--orange: #f47b45;
--yellow: #ffd166;
--green: #37a66a;
--red: #d95852;
--shadow: 0 18px 50px rgba(31, 64, 80, 0.18);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #b9ddf0;
color: var(--ink);
font-family: Inter, ui-rounded, "SF Pro Rounded", system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
button {
border: 0;
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
cursor: grab;
}
#scene.dragging {
cursor: grabbing;
}
.topbar {
position: fixed;
z-index: 5;
top: 18px;
left: 20px;
right: 20px;
display: flex;
align-items: center;
justify-content: space-between;
gap: 18px;
pointer-events: none;
}
.brand,
.stats {
display: flex;
align-items: center;
gap: 12px;
pointer-events: auto;
}
.brand {
padding: 11px 15px;
border: 1px solid rgba(255, 255, 255, 0.55);
border-radius: 18px;
background: rgba(250, 253, 253, 0.86);
box-shadow: var(--shadow);
backdrop-filter: blur(12px);
}
.brand-icon {
width: 38px;
height: 38px;
position: relative;
transform: rotate(-7deg);
}
.brand-icon .cube {
position: absolute;
width: 15px;
height: 15px;
border-radius: 3px;
box-shadow: inset -3px -3px 0 rgba(0, 0, 0, 0.12);
}
.brand-icon .cube:nth-child(1) {
left: 2px;
top: 14px;
background: var(--orange);
}
.brand-icon .cube:nth-child(2) {
left: 18px;
top: 7px;
background: var(--yellow);
}
.brand-icon .cube:nth-child(3) {
left: 19px;
top: 23px;
background: var(--teal);
}
.brand-title {
font-weight: 900;
font-size: 17px;
letter-spacing: -0.03em;
line-height: 1.05;
}
.brand-subtitle {
margin-top: 3px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.stats {
padding: 9px;
border-radius: 18px;
background: rgba(250, 253, 253, 0.9);
border: 1px solid rgba(255, 255, 255, 0.6);
box-shadow: var(--shadow);
backdrop-filter: blur(12px);
}
.stat {
min-width: 74px;
padding: 2px 11px;
border-right: 1px solid var(--line);
}
.stat:last-child {
border-right: 0;
}
.stat-label {
display: block;
color: var(--muted);
font-size: 9px;
font-weight: 900;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.stat-value {
display: block;
margin-top: 2px;
font-size: 18px;
font-weight: 900;
letter-spacing: -0.04em;
}
.panel {
position: fixed;
z-index: 6;
left: 20px;
top: 92px;
width: min(350px, calc(100vw - 40px));
max-height: calc(100vh - 116px);
overflow: auto;
padding: 20px;
border: 1px solid rgba(255, 255, 255, 0.65);
border-radius: 24px;
background: var(--panel);
box-shadow: var(--shadow);
backdrop-filter: blur(18px);
}
.panel.shake {
animation: shake 0.32s ease;
}
@keyframes shake {
0%, 100% { transform: translateX(0); }
25% { transform: translateX(-7px); }
50% { transform: translateX(6px); }
75% { transform: translateX(-3px); }
}
.eyebrow {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
color: var(--teal-dark);
font-size: 10px;
font-weight: 900;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.round-pill {
padding: 5px 8px;
border-radius: 999px;
background: rgba(22, 143, 138, 0.1);
color: var(--teal-dark);
letter-spacing: 0.08em;
}
h1 {
margin: 12px 0 5px;
font-size: 29px;
line-height: 1.02;
letter-spacing: -0.055em;
}
.intro {
margin: 0;
color: var(--muted);
font-size: 13px;
line-height: 1.5;
}
.target-card {
position: relative;
overflow: hidden;
margin: 18px 0;
padding: 16px;
border-radius: 18px;
color: white;
background:
radial-gradient(circle at 90% 10%, rgba(255, 209, 102, 0.48), transparent 35%),
linear-gradient(135deg, #167f7b, #115d66);
box-shadow: 0 12px 24px rgba(13, 109, 106, 0.2);
}
.target-card::after {
content: "";
position: absolute;
right: -9px;
bottom: -17px;
width: 70px;
height: 70px;
border: 9px solid rgba(255, 255, 255, 0.1);
border-radius: 14px;
transform: rotate(18deg);
}
.target-label {
font-size: 10px;
font-weight: 900;
letter-spacing: 0.14em;
text-transform: uppercase;
opacity: 0.8;
}
.target-value {
margin-top: 5px;
font-size: 35px;
font-weight: 950;
letter-spacing: -0.055em;
line-height: 1;
}
.target-value small {
font-size: 15px;
font-weight: 800;
opacity: 0.78;
letter-spacing: 0;
}
.section-title {
margin: 17px 0 9px;
font-size: 11px;
font-weight: 900;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.control {
margin-top: 15px;
}
.control-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-bottom: 8px;
font-size: 13px;
font-weight: 800;
}
.control-value {
min-width: 66px;
padding: 5px 9px;
border-radius: 9px;
background: #eaf3f2;
color: var(--teal-dark);
text-align: center;
font-variant-numeric: tabular-nums;
}
input[type="range"] {
width: 100%;
height: 7px;
margin: 0;
appearance: none;
border-radius: 999px;
outline: none;
background: linear-gradient(
to right,
var(--teal) 0%,
var(--teal) var(--fill, 50%),
#dbe7e7 var(--fill, 50%),
#dbe7e7 100%
);
}
input[type="range"]::-webkit-slider-thumb {
width: 21px;
height: 21px;
appearance: none;
border: 4px solid white;
border-radius: 50%;
background: var(--teal);
box-shadow: 0 3px 9px rgba(23, 48, 66, 0.25);
cursor: pointer;
}
input[type="range"]::-moz-range-thumb {
width: 14px;
height: 14px;
border: 4px solid white;
border-radius: 50%;
background: var(--teal);
box-shadow: 0 3px 9px rgba(23, 48, 66, 0.25);
cursor: pointer;
}
.range-ends {
display: flex;
justify-content: space-between;
margin-top: 5px;
color: #8293a0;
font-size: 10px;
font-weight: 700;
}
.formula {
margin-top: 18px;
padding: 14px;
border: 1px solid var(--line);
border-radius: 16px;
background: rgba(255, 255, 255, 0.68);
}
.formula-main {
text-align: center;
font-size: 18px;
font-weight: 950;
letter-spacing: -0.03em;
}
.formula-main span {
color: var(--teal);
}
.calculation {
display: grid;
grid-template-columns: 1fr auto;
gap: 6px 14px;
margin-top: 11px;
font-size: 12px;
}
.calculation .label {
color: var(--muted);
}
.calculation .value {
text-align: right;
font-weight: 900;
font-variant-numeric: tabular-nums;
}
.planned-distance {
margin-top: 11px;
padding-top: 10px;
border-top: 1px dashed var(--line);
display: flex;
justify-content: space-between;
align-items: center;
gap: 12px;
font-size: 12px;
font-weight: 800;
}
#plannedValue {
font-size: 16px;
font-weight: 950;
font-variant-numeric: tabular-nums;
}
.planned-distance.match #plannedValue {
color: var(--green);
}
.planned-distance.miss #plannedValue {
color: var(--red);
}
.feedback {
min-height: 39px;
margin-top: 13px;
padding: 10px 12px;
border-radius: 12px;
color: var(--muted);
background: #edf3f3;
font-size: 12px;
line-height: 1.4;
font-weight: 700;
}
.feedback.good {
color: #236c48;
background: #e6f6ed;
}
.feedback.bad {
color: #9b3e39;
background: #fbe9e7;
}
.actions {
display: grid;
grid-template-columns: 1fr auto;
gap: 9px;
margin-top: 12px;
}
.primary,
.secondary {
min-height: 46px;
border-radius: 13px;
padding: 0 16px;
font-weight: 900;
cursor: pointer;
transition: transform 0.15s ease, filter 0.15s ease, opacity 0.15s ease;
}
.primary {
color: white;
background: linear-gradient(135deg, #f58b50, #ef6f43);
box-shadow: 0 8px 18px rgba(239, 111, 67, 0.26);
}
.secondary {
color: var(--ink);
background: #e6eeee;
}
.primary:hover,
.secondary:hover {
transform: translateY(-1px);
filter: brightness(1.03);
}
.primary:active,
.secondary:active {
transform: translateY(1px);
}
.primary:disabled {
cursor: default;
opacity: 0.6;
transform: none;
}
.small-actions {
display: flex;
justify-content: space-between;
gap: 10px;
margin-top: 10px;
}
.text-button {
padding: 5px 2px;
color: var(--teal-dark);
background: transparent;
font-size: 11px;
font-weight: 900;
cursor: pointer;
}
.camera-help {
position: fixed;
z-index: 4;
right: 20px;
bottom: 18px;
padding: 9px 12px;
border-radius: 12px;
background: rgba(250, 253, 253, 0.82);
border: 1px solid rgba(255, 255, 255, 0.6);
color: #526979;
box-shadow: 0 8px 25px rgba(31, 64, 80, 0.12);
font-size: 11px;
font-weight: 800;
pointer-events: none;
}
.modal {
position: fixed;
z-index: 20;
inset: 0;
display: grid;
place-items: center;
padding: 20px;
background: rgba(18, 40, 51, 0.25);
backdrop-filter: blur(5px);
}
.modal[hidden] {
display: none;
}
.result-card {
width: min(460px, 100%);
overflow: hidden;
border-radius: 25px;
background: #f8fbfb;
box-shadow: 0 25px 80px rgba(16, 44, 57, 0.3);
animation: pop 0.25s ease-out;
}
@keyframes pop {
from {
opacity: 0;
transform: translateY(15px) scale(0.96);
}
to {
opacity: 1;
transform: translateY(0) scale(1);
}
}
.result-head {
position: relative;
padding: 22px 24px 20px;
color: white;
background:
radial-gradient(circle at 85% 20%, rgba(255, 209, 102, 0.45), transparent 32%),
linear-gradient(135deg, #168f8a, #11636a);
}
.result-badge {
display: inline-flex;
align-items: center;
gap: 7px;
padding: 6px 10px;
border-radius: 999px;
background: rgba(255, 255, 255, 0.16);
font-size: 10px;
font-weight: 900;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.result-title {
margin: 10px 0 3px;
font-size: 29px;
letter-spacing: -0.05em;
}
.result-subtitle {
margin: 0;
opacity: 0.78;
font-size: 12px;
}
.result-body {
padding: 20px 24px 24px;
}
.verification {
padding: 14px;
border: 1px solid rgba(55, 166, 106, 0.22);
border-radius: 15px;
background: #eaf8ef;
}
.verification-title {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
color: #257348;
font-size: 12px;
font-weight: 950;
text-transform: uppercase;
letter-spacing: 0.08em;
}
.verified {
padding: 4px 8px;
border-radius: 999px;
color: white;
background: var(--green);
font-size: 10px;
}
.motion-grid {
display: grid;
grid-template-columns: 1fr auto;
gap: 8px 16px;
margin-top: 12px;
font-size: 12px;
}
.motion-grid .label {
color: var(--muted);
}
.motion-grid .value {
text-align: right;
font-weight: 900;
font-variant-numeric: tabular-nums;
}
.result-actions {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 10px;
margin-top: 18px;
}
@media (max-width: 760px) {
.topbar {
top: 10px;
left: 10px;
right: 10px;
}
.brand {
padding: 9px 11px;
}
.brand-icon {
display: none;
}
.brand-title {
font-size: 14px;
}
.brand-subtitle {
font-size: 8px;
}
.stats {
padding: 6px;
}
.stat {
min-width: 51px;
padding: 2px 7px;
}
.stat-value {
font-size: 15px;
}
.stat-label {
font-size: 7px;
}
.panel {
top: auto;
left: 10px;
right: 10px;
bottom: 10px;
width: auto;
max-height: 54vh;
padding: 16px;
border-radius: 21px;
}
h1 {
font-size: 24px;
}
.target-card {
margin: 12px 0;
padding: 13px;
}
.target-value {
font-size: 29px;
}
.camera-help {
display: none;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive voxel rolling game"></canvas>
<header class="topbar">
<div class="brand">
<div class="brand-icon" aria-hidden="true">
<span class="cube"></span>
<span class="cube"></span>
<span class="cube"></span>
</div>
<div>
<div class="brand-title">VOXEL ROLL</div>
<div class="brand-subtitle">Target distance lab</div>
</div>
</div>
<div class="stats">
<div class="stat">
<span class="stat-label">Round</span>
<span class="stat-value" id="roundValue">1</span>
</div>
<div class="stat">
<span class="stat-label">Score</span>
<span class="stat-value" id="scoreValue">0</span>
</div>
<div class="stat">
<span class="stat-label">Best</span>
<span class="stat-value" id="bestValue">0</span>
</div>
</div>
</header>
<aside class="panel" id="panel">
<div class="eyebrow">
Distance challenge
<span class="round-pill" id="statusPill">Set up</span>
</div>
<h1>Match the target.</h1>
<p class="intro">
Choose a wheel radius and a number of rotations. Use the no-slip
rolling equation to land exactly on the finish line.
</p>
<section class="target-card">
<div class="target-label">Target distance</div>
<div class="target-value">
<span id="targetValue">0.000</span> <small>m</small>
</div>
</section>
<div class="section-title">1. Choose wheel radius</div>
<div class="control">
<div class="control-head">
<label for="radiusInput">Radius</label>
<output class="control-value" id="radiusOutput">0.65 m</output>
</div>
<input
id="radiusInput"
type="range"
min="0.35"
max="1.15"
step="0.05"
value="0.65"
>
<div class="range-ends">
<span>0.35 m</span>
<span>1.15 m</span>
</div>
</div>
<div class="section-title">2. Choose rotation count</div>
<div class="control">
<div class="control-head">
<label for="rotationInput">Full rotations</label>
<output class="control-value" id="rotationOutput">6 turns</output>
</div>
<input
id="rotationInput"
type="range"
min="1"
max="16"
step="1"
value="6"
>
<div class="range-ends">
<span>1 turn</span>
<span>16 turns</span>
</div>
</div>
<section class="formula">
<div class="formula-main">
D = <span>2πr</span> × N
</div>
<div class="calculation">
<span class="label">Circumference</span>
<span class="value" id="circumferenceValue">0.000 m</span>
<span class="label">Circumference × turns</span>
<span class="value" id="distanceValue">0.000 m</span>
</div>
<div class="planned-distance" id="plannedDistance">
<span>Your planned travel</span>
<span id="plannedValue">0.000 m</span>
</div>
</section>
<div class="feedback" id="feedback">
Adjust the controls until your planned travel equals the target.
</div>
<div class="actions">
<button class="primary" id="rollButton">Check & Roll</button>
<button class="secondary" id="hintButton" title="Use one hint">Hint</button>
</div>
<div class="small-actions">
<button class="text-button" id="newTargetButton">Generate new target</button>
<button class="text-button" id="resetButton">Reset setup</button>
</div>
</aside>
<div class="camera-help">Drag to orbit · Scroll to zoom</div>
<div class="modal" id="resultModal" hidden>
<section class="result-card" role="dialog" aria-modal="true" aria-labelledby="resultTitle">
<div class="result-head">
<div class="result-badge">✓ Motion verified</div>
<h2 class="result-title" id="resultTitle">Perfect landing!</h2>
<p class="result-subtitle" id="resultSubtitle">Your rover reached the target.</p>
</div>
<div class="result-body">
<div class="verification">
<div class="verification-title">
Actual position check
<span class="verified">Verified</span>
</div>
<div class="motion-grid">
<span class="label">Starting position</span>
<span class="value" id="startPosition">0.0000 m</span>
<span class="label">Final position</span>
<span class="value" id="finalPosition">0.0000 m</span>
<span class="label">Target position</span>
<span class="value" id="resultTarget">0.0000 m</span>
<span class="label">Motion error</span>
<span class="value" id="motionError">0.0000 m</span>
</div>
</div>
<div class="result-actions">
<button class="secondary" id="replayButton">Replay run</button>
<button class="primary" id="nextButton">Next target</button>
</div>
</div>
</section>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// DOM and game state
// -------------------------------------------------------------------------
const canvas = document.getElementById("scene");
const panel = document.getElementById("panel");
const radiusInput = document.getElementById("radiusInput");
const rotationInput = document.getElementById("rotationInput");
const radiusOutput = document.getElementById("radiusOutput");
const rotationOutput = document.getElementById("rotationOutput");
const targetValue = document.getElementById("targetValue");
const circumferenceValue = document.getElementById("circumferenceValue");
const distanceValue = document.getElementById("distanceValue");
const plannedValue = document.getElementById("plannedValue");
const plannedDistance = document.getElementById("plannedDistance");
const feedback = document.getElementById("feedback");
const rollButton = document.getElementById("rollButton");
const hintButton = document.getElementById("hintButton");
const newTargetButton = document.getElementById("newTargetButton");
const resetButton = document.getElementById("resetButton");
const statusPill = document.getElementById("statusPill");
const roundValue = document.getElementById("roundValue");
const scoreValue = document.getElementById("scoreValue");
const bestValue = document.getElementById("bestValue");
const resultModal = document.getElementById("resultModal");
const resultTitle = document.getElementById("resultTitle");
const resultSubtitle = document.getElementById("resultSubtitle");
const startPosition = document.getElementById("startPosition");
const finalPosition = document.getElementById("finalPosition");
const resultTarget = document.getElementById("resultTarget");
const motionError = document.getElementById("motionError");
const replayButton = document.getElementById("replayButton");
const nextButton = document.getElementById("nextButton");
const radiusChoices = [
0.40, 0.45, 0.50, 0.55, 0.60,
0.65, 0.70, 0.75, 0.80, 0.85, 0.90
];
const game = {
round: 1,
score: 0,
best: 0,
attempts: 0,
hintUsed: false,
rolling: false,
target: {
distance: 0,
radius: 0.65,
rotations: 6
}
};
try {
game.best = Number(localStorage.getItem("voxel-roll-best") || 0);
bestValue.textContent = game.best;
} catch (_) {
// Local storage is optional.
}
// -------------------------------------------------------------------------
// Three.js scene
// -------------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: false
});
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.05;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xb9ddf0);
scene.fog = new THREE.Fog(0xb9ddf0, 34, 105);
const camera = new THREE.PerspectiveCamera(
48,
window.innerWidth / window.innerHeight,
0.1,
180
);
const hemisphere = new THREE.HemisphereLight(0xeaf8ff, 0x758f5a, 2.15);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff0cc, 2.5);
sun.position.set(8, 18, 10);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -13;
sun.shadow.camera.right = 13;
sun.shadow.camera.top = 13;
sun.shadow.camera.bottom = -13;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 55;
sun.shadow.bias = -0.0008;
scene.add(sun);
const sunTarget = new THREE.Object3D();
scene.add(sunTarget);
sun.target = sunTarget;
let world = null;
let rover = null;
let wheelSpins = [];
const cameraControl = {
azimuth: -0.42,
elevation: 0.59,
distance: 22,
targetDistance: 22,
focusX: 0,
focusY: 0.8,
focusZ: 0,
dragging: false,
lastX: 0,
lastY: 0
};
// -------------------------------------------------------------------------
// Utility functions
// -------------------------------------------------------------------------
function clamp(value, min, max) {
return Math.max(min, Math.min(max, value));
}
function randomChoice(array) {
return array[Math.floor(Math.random() * array.length)];
}
function seededRandom(seed) {
let value = seed >>> 0;
return function () {
value += 0x6D2B79F5;
let t = value;
t = Math.imul(t ^ (t >>> 15), t | 1);
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
}
function disposeObject(object) {
if (!object) return;
object.traverse((child) => {
if (child.geometry) child.geometry.dispose();
if (child.material) {
const materials = Array.isArray(child.material)
? child.material
: [child.material];
materials.forEach((material) => {
if (material.map) material.map.dispose();
material.dispose();
});
}
});
}
function addBox(parent, size, color, position, castShadow = false) {
const geometry = new THREE.BoxGeometry(size.x, size.y, size.z);
const material = new THREE.MeshStandardMaterial({
color,
roughness: 0.82,
metalness: 0.02
});
const mesh = new THREE.Mesh(geometry, material);
mesh.position.set(position.x, position.y, position.z);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function createLabel(text, accent = "#173042") {
const labelCanvas = document.createElement("canvas");
labelCanvas.width = 512;
labelCanvas.height = 160;
const context = labelCanvas.getContext("2d");
context.clearRect(0, 0, 512, 160);
context.fillStyle = "rgba(248, 252, 252, 0.94)";
context.fillRect(4, 4, 504, 152);
context.strokeStyle = accent;
context.lineWidth = 10;
context.strokeRect(5, 5, 502, 150);
context.fillStyle = accent;
context.textAlign = "center";
context.textBaseline = "middle";
context.font = "900 44px system-ui, sans-serif";
context.fillText(text, 256, 82, 465);
const texture = new THREE.CanvasTexture(labelCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(3.2, 1, 1);
return sprite;
}
// -------------------------------------------------------------------------
// Voxel world construction
// -------------------------------------------------------------------------
function buildWorld() {
if (world) {
scene.remove(world);
disposeObject(world);
}
world = new THREE.Group();
scene.add(world);
const distance = game.target.distance;
const roadStart = -6;
const roadEnd = Math.ceil(distance) + 7;
const roadLength = roadEnd - roadStart;
const random = seededRandom(Math.floor(game.target.distance * 1000) + game.round * 77);
// Solid earth beneath the visible voxel surface.
addBox(
world,
new THREE.Vector3(roadLength + 8, 0.5, 22),
0x638c54,
new THREE.Vector3(roadStart + roadLength / 2, -0.48, 0)
);
// Individual road voxels make the course length easy to read.
for (let x = roadStart; x < roadEnd; x++) {
const alternating = Math.floor(x) % 2 === 0;
const edgeShade = Math.abs((x % 7) / 7);
const roadColor = new THREE.Color(0xd9e2dc);
roadColor.offsetHSL(0, 0, alternating ? 0.01 : -0.035);
roadColor.offsetHSL(0, 0, -edgeShade * 0.025);
addBox(
world,
new THREE.Vector3(0.965, 0.2, 3.25),
roadColor.getHex(),
new THREE.Vector3(x + 0.5, -0.1, 0)
);
}
// Raised grass voxels on both sides.
for (let x = roadStart; x < roadEnd; x += 1.35) {
for (const side of [-1, 1]) {
for (let row = 0; row < 2; row++) {
const z = side * (2.65 + row * 1.4);
const height = 0.22 + random() * 0.16;
const color = new THREE.Color(0x82b968);
color.offsetHSL((random() - 0.5) * 0.025, 0, (random() - 0.5) * 0.09);
addBox(
world,
new THREE.Vector3(1.28, height, 1.32),
color.getHex(),
new THREE.Vector3(x + random() * 0.15, height / 2 - 0.2, z)
);
}
}
}
// Voxel trees.
const treeCount = Math.floor(distance / 3.5) + 7;
for (let i = 0; i < treeCount; i++) {
const x = roadStart + random() * (roadLength - 2);
const side = random() > 0.5 ? 1 : -1;
const z = side * (4.4 + random() * 5.6);
const trunkHeight = 0.85 + random() * 0.65;
const leafSize = 0.8 + random() * 0.55;
addBox(
world,
new THREE.Vector3(0.32, trunkHeight, 0.32),
0x8d6040,
new THREE.Vector3(x, trunkHeight / 2 - 0.15, z),
true
);
addBox(
world,
new THREE.Vector3(leafSize, 0.72, leafSize),
0x4f9860,
new THREE.Vector3(x, trunkHeight + 0.18, z),
true
);
addBox(
world,
new THREE.Vector3(leafSize * 0.72, 0.66, leafSize * 0.72),
0x61aa69,
new THREE.Vector3(x + 0.1, trunkHeight + 0.75, z - 0.06),
true
);
}
// Distant stacked voxel buildings.
for (let i = 0; i < 8; i++) {
const x = roadStart + 3 + random() * Math.max(4, distance + 1);
const side = i % 2 === 0 ? -1 : 1;
const z = side * (9.2 + random() * 2.5);
const width = 1.4 + random() * 1.8;
const depth = 1.4 + random() * 1.4;
const floors = 2 + Math.floor(random() * 3);
const buildingColors = [0xe6b774, 0xd78967, 0x91a9bb, 0xb6a4c8];
for (let floor = 0; floor < floors; floor++) {
addBox(
world,
new THREE.Vector3(width, 0.82, depth),
buildingColors[i % buildingColors.length],
new THREE.Vector3(
x + (floor % 2) * 0.18,
0.2 + floor * 0.82,
z
)
);
}
addBox(
world,
new THREE.Vector3(width * 0.7, 0.22, depth * 0.7),
0x526d80,
new THREE.Vector3(x + 0.18, 0.2 + floors * 0.82, z)
);
}
// Block clouds.
for (let i = 0; i < 9; i++) {
const x = roadStart + random() * (roadLength + 10);
const y = 7.5 + random() * 4;
const z = -11 + random() * 22;
const parts = 2 + Math.floor(random() * 3);
for (let p = 0; p < parts; p++) {
addBox(
world,
new THREE.Vector3(1.5 + random(), 0.72, 1.1 + random() * 0.5),
0xf5fbf7,
new THREE.Vector3(x + p * 1.2, y + (p % 2) * 0.3, z)
);
}
}
// Start marker.
for (const z of [-2.1, 2.1]) {
for (let level = 0; level < 4; level++) {
addBox(
world,
new THREE.Vector3(0.45, 0.45, 0.45),
level % 2 === 0 ? 0xffffff : 0x173042,
new THREE.Vector3(0, 0.16 + level * 0.45, z)
);
}
}
const startLabel = createLabel("START", "#168f8a");
startLabel.position.set(0, 2.45, 2.8);
world.add(startLabel);
// Checker finish line.
const finishX = distance;
for (let column = 0; column < 2; column++) {
for (let row = 0; row < 8; row++) {
addBox(
world,
new THREE.Vector3(0.46, 0.06, 0.39),
(column + row) % 2 === 0 ? 0xffffff : 0x173042,
new THREE.Vector3(
finishX - 0.23 + column * 0.46,
0.035,
-1.4 + row * 0.4
)
);
}
}
// Voxel finish arch.
for (const z of [-2.05, 2.05]) {
for (let level = 0; level < 7; level++) {
addBox(
world,
new THREE.Vector3(0.48, 0.48, 0.48),
level % 2 === 0 ? 0xffd166 : 0xf47b45,
new THREE.Vector3(finishX, 0.17 + level * 0.48, z)
);
}
}
for (let i = 0; i < 9; i++) {
addBox(
world,
new THREE.Vector3(0.5, 0.42, 0.48),
i % 2 === 0 ? 0xffffff : 0x168f8a,
new THREE.Vector3(finishX, 3.52, -2 + i * 0.5)
);
}
const finishLabel = createLabel(
`TARGET ${distance.toFixed(2)} m`,
"#e46c3f"
);
finishLabel.scale.set(4.5, 1.4, 1);
finishLabel.position.set(finishX, 4.65, 0);
world.add(finishLabel);
// Distance posts and labels.
for (let x = 5; x < distance - 2; x += 5) {
addBox(
world,
new THREE.Vector3(0.16, 1.05, 0.16),
0x688695,
new THREE.Vector3(x, 0.43, 2.2)
);
addBox(
world,
new THREE.Vector3(0.58, 0.42, 0.12),
0xffd166,
new THREE.Vector3(x, 1.02, 2.2)
);
}
const midpointLabel = createLabel(`${(distance / 2).toFixed(1)} m`, "#526979");
midpointLabel.scale.set(2.2, 0.69, 1);
midpointLabel.position.set(distance / 2, 1.7, 2.8);
world.add(midpointLabel);
}
// -------------------------------------------------------------------------
// Voxel rover construction
// -------------------------------------------------------------------------
function buildRover() {
if (rover) {
scene.remove(rover);
disposeObject(rover);
}
rover = new THREE.Group();
rover.position.set(0, 0, 0);
scene.add(rover);
wheelSpins = [];
const radius = Number(radiusInput.value);
const wheelbase = Math.max(1.5, radius * 2.15);
const bodyLength = wheelbase + 0.7;
// Chassis.
addBox(
rover,
new THREE.Vector3(bodyLength, 0.3, 1.45),
0xf47b45,
new THREE.Vector3(0, radius + 0.3, 0),
true
);
addBox(
rover,
new THREE.Vector3(bodyLength * 0.42, 0.38, 1.18),
0xffc857,
new THREE.Vector3(-bodyLength * 0.17, radius + 0.62, 0),
true
);
// Cargo and antenna.
addBox(
rover,
new THREE.Vector3(bodyLength * 0.22, 0.42, 0.82),
0x168f8a,
new THREE.Vector3(bodyLength * 0.27, radius + 0.61, 0),
true
);
addBox(
rover,
new THREE.Vector3(0.08, 0.85, 0.08),
0x173042,
new THREE.Vector3(bodyLength * 0.32, radius + 1.2, 0),
true
);
addBox(
rover,
new THREE.Vector3(0.2, 0.2, 0.2),
0xffd166,
new THREE.Vector3(bodyLength * 0.32, radius + 1.67, 0),
true
);
// Axle.
addBox(
rover,
new THREE.Vector3(0.16, 0.16, 2.05),
0x50616c,
new THREE.Vector3(0, radius + 0.05, 0)
);
// Pixel wheels made from visible voxel blocks.
const blockSize = clamp(radius * 0.27, 0.15, 0.25);
const ringRadius = radius - blockSize * 0.55;
const blockCount = 16;
for (const x of [-wheelbase / 2, wheelbase / 2]) {
for (const z of [-0.91, 0.91]) {
const wheelRoot = new THREE.Group();
wheelRoot.position.set(x, radius + 0.04, z);
rover.add(wheelRoot);
const spin = new THREE.Group();
wheelRoot.add(spin);
wheelSpins.push(spin);
for (let i = 0; i < blockCount; i++) {
const angle = (i / blockCount) * Math.PI * 2;
const block = addBox(
spin,
new THREE.Vector3(blockSize, blockSize, 0.28),
i % 4 === 0 ? 0x304653 : 0x20313c,
new THREE.Vector3(
Math.cos(angle) * ringRadius,
Math.sin(angle) * ringRadius,
0
),
true
);
block.rotation.z = angle;
}
// Hub and four block spokes.
addBox(
spin,
new THREE.Vector3(radius * 0.42, radius * 0.42, 0.34),
0xffd166,
new THREE.Vector3(0, 0, 0),
true
);
for (let i = 0; i < 4; i++) {
const angle = (i / 4) * Math.PI * 2;
const spoke = addBox(
spin,
new THREE.Vector3(radius * 0.82, radius * 0.14, 0.31),
0xf47b45,
new THREE.Vector3(
Math.cos(angle) * radius * 0.22,
Math.sin(angle) * radius * 0.22,
0
),
true
);
spoke.rotation.z = angle;
}
}
}
}
// -------------------------------------------------------------------------
// UI calculations
// -------------------------------------------------------------------------
function currentSetup() {
const radius = Number(radiusInput.value);
const rotations = Number(rotationInput.value);
const circumference = 2 * Math.PI * radius;
const distance = circumference * rotations;
return { radius, rotations, circumference, distance };
}
function updateRangeFill(input) {
const min = Number(input.min);
const max = Number(input.max);
const value = Number(input.value);
input.style.setProperty(
"--fill",
`${((value - min) / (max - min)) * 100}%`
);
}
function updateCalculation() {
const setup = currentSetup();
const isMatch = Math.abs(setup.distance - game.target.distance) < 0.012;
radiusOutput.textContent = `${setup.radius.toFixed(2)} m`;
rotationOutput.textContent = `${setup.rotations} turns`;
circumferenceValue.textContent = `${setup.circumference.toFixed(3)} m`;
distanceValue.textContent = `${setup.distance.toFixed(3)} m`;
plannedValue.textContent = `${setup.distance.toFixed(3)} m`;
plannedDistance.classList.toggle("match", isMatch);
plannedDistance.classList.toggle("miss", !isMatch);
updateRangeFill(radiusInput);
updateRangeFill(rotationInput);
}
function setFeedback(message, type = "") {
feedback.textContent = message;
feedback.className = `feedback ${type}`.trim();
}
function shakePanel() {
panel.classList.remove("shake");
void panel.offsetWidth;
panel.classList.add("shake");
}
function updateStats() {
roundValue.textContent = game.round;
scoreValue.textContent = game.score;
bestValue.textContent = game.best;
}
// -------------------------------------------------------------------------
// Round management
// -------------------------------------------------------------------------
function newRound() {
if (game.rolling) return;
game.round++;
game.attempts = 0;
game.hintUsed = false;
const secretRadius = randomChoice(radiusChoices);
const secretRotations = 3 + Math.floor(Math.random() * 8);
game.target = {
distance: 2 * Math.PI * secretRadius * secretRotations,
radius: secretRadius,
rotations: secretRotations
};
radiusInput.value = "0.65";
rotationInput.value = "6";
targetValue.textContent = game.target.distance.toFixed(3);
statusPill.textContent = "Set up";
rollButton.textContent = "Check & Roll";
rollButton.disabled = false;
hintButton.disabled = false;
resultModal.hidden = true;
setFeedback("Adjust the controls until your planned travel equals the target.");
buildWorld();
buildRover();
updateCalculation();
updateStats();
cameraControl.targetDistance = clamp(14 + game.target.distance * 0.28, 16, 32);
cameraControl.distance = cameraControl.targetDistance;
cameraControl.focusX = game.target.distance * 0.48;
}
function resetRound() {
if (game.rolling) return;
game.attempts = 0;
game.hintUsed = false;
radiusInput.value = "0.65";
rotationInput.value = "6";
statusPill.textContent = "Set up";
setFeedback("Setup reset. Find the exact wheel-and-turn combination.");
buildRover();
updateCalculation();
}
function useHint() {
if (game.rolling) return;
game.hintUsed = true;
radiusInput.value = game.target.radius.toFixed(2);
buildRover();
updateCalculation();
setFeedback(
`Hint: a working radius is ${game.target.radius.toFixed(2)} m. ` +
"Now divide the target by that wheel’s circumference.",
"good"
);
}
// -------------------------------------------------------------------------
// Actual-motion verification
// -------------------------------------------------------------------------
let activeRun = null;
function startRoll() {
if (game.rolling) return;
const setup = currentSetup();
game.attempts++;
const error = Math.abs(setup.distance - game.target.distance);
if (error >= 0.012) {
shakePanel();
statusPill.textContent = "Adjust";
setFeedback(
`Not quite: your setup is ${error.toFixed(3)} m away from the target.`,
"bad"
);
return;
}
game.rolling = true;
resultModal.hidden = true;
statusPill.textContent = "Rolling";
rollButton.disabled = true;
hintButton.disabled = true;
setFeedback("Rolling now. Position is being measured from wheel motion.", "good");
rover.position.x = 0;
wheelSpins.forEach((spin) => {
spin.rotation.z = 0;
});
const totalAngle = setup.rotations * Math.PI * 2;
const duration = clamp(1.65 + setup.rotations * 0.14, 2.0, 4.0);
activeRun = {
startedAt: performance.now(),
duration,
totalAngle,
previousAngle: 0,
radius: setup.radius,
startX: rover.position.x,
traveled: 0
};
}
function finishRun() {
const setup = currentSetup();
const startX = activeRun.startX;
// Read the rover's real final world position rather than reusing the formula.
rover.updateMatrixWorld(true);
const finalWorldPosition = new THREE.Vector3();
rover.getWorldPosition(finalWorldPosition);
const finalX = finalWorldPosition.x;
const actualDistance = finalX - startX;
const verificationError = Math.abs(actualDistance - game.target.distance);
const points = Math.max(
40,
100 - (game.attempts - 1) * 15 - (game.hintUsed ? 20 : 0)
);
game.score += points;
if (game.score > game.best) {
game.best = game.score;
try {
localStorage.setItem("voxel-roll-best", String(game.best));
} catch (_) {
// Local storage is optional.
}
}
updateStats();
game.rolling = false;
activeRun = null;
rollButton.disabled = false;
hintButton.disabled = false;
rollButton.textContent = "Roll again";
statusPill.textContent = "Verified";
const stars = game.hintUsed ? 2 : game.attempts === 1 ? 3 : game.attempts <= 3 ? 2 : 1;
resultTitle.textContent = "Perfect landing!";
resultSubtitle.textContent =
`+${points} points · ${"★".repeat(stars)}${"☆".repeat(3 - stars)} · ` +
`${setup.rotations} turns of a ${setup.radius.toFixed(2)} m wheel`;
startPosition.textContent = `${startX.toFixed(4)} m`;
finalPosition.textContent = `${finalX.toFixed(4)} m`;
resultTarget.textContent = `${game.target.distance.toFixed(4)} m`;
motionError.textContent = `${verificationError.toFixed(4)} m`;
setFeedback(
`Verified! Actual travel: ${actualDistance.toFixed(4)} m; ` +
`measurement error: ${verificationError.toFixed(4)} m.`,
"good"
);
resultModal.hidden = false;
}
function updateRun(now) {
if (!activeRun) return;
const rawProgress = clamp(
(now - activeRun.startedAt) / activeRun.duration,
0,
1
);
// Smooth visual acceleration and deceleration.
const easedProgress =
rawProgress * rawProgress * (3 - 2 * rawProgress);
const currentAngle = activeRun.totalAngle * easedProgress;
const angleDelta = currentAngle - activeRun.previousAngle;
// Actual no-slip motion is integrated from wheel angular displacement:
// distance increment = radius × wheel angle increment.
activeRun.traveled += angleDelta * activeRun.radius;
activeRun.previousAngle = currentAngle;
rover.position.x = activeRun.startX + activeRun.traveled;
// The visible wheel turns through exactly the selected number of rotations.
wheelSpins.forEach((spin) => {
spin.rotation.z = -currentAngle;
});
if (rawProgress >= 1) {
finishRun();
}
}
// -------------------------------------------------------------------------
// Custom camera controls implemented without addons
// -------------------------------------------------------------------------
function updateCamera() {
const distance = cameraControl.distance;
const elevation = cameraControl.elevation;
const azimuth = cameraControl.azimuth;
camera.position.set(
cameraControl.focusX +
distance * Math.cos(elevation) * Math.sin(azimuth),
cameraControl.focusY + distance * Math.sin(elevation),
cameraControl.focusZ +
distance * Math.cos(elevation) * Math.cos(azimuth)
);
camera.lookAt(
cameraControl.focusX,
cameraControl.focusY,
cameraControl.focusZ
);
}
canvas.addEventListener("pointerdown", (event) => {
cameraControl.dragging = true;
cameraControl.lastX = event.clientX;
cameraControl.lastY = event.clientY;
canvas.classList.add("dragging");
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!cameraControl.dragging) return;
const deltaX = event.clientX - cameraControl.lastX;
const deltaY = event.clientY - cameraControl.lastY;
cameraControl.lastX = event.clientX;
cameraControl.lastY = event.clientY;
cameraControl.azimuth -= deltaX * 0.006;
cameraControl.elevation = clamp(
cameraControl.elevation + deltaY * 0.0045,
0.18,
1.25
);
});
function endCameraDrag(event) {
cameraControl.dragging = false;
canvas.classList.remove("dragging");
if (event && canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endCameraDrag);
canvas.addEventListener("pointercancel", endCameraDrag);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraControl.distance = clamp(
cameraControl.distance * Math.exp(event.deltaY * 0.001),
8,
48
);
},
{ passive: false }
);
// -------------------------------------------------------------------------
// Input events
// -------------------------------------------------------------------------
radiusInput.addEventListener("input", () => {
buildRover();
updateCalculation();
if (!game.rolling) {
statusPill.textContent = "Set up";
setFeedback("Radius updated. Recalculate the distance as needed.");
}
});
rotationInput.addEventListener("input", () => {
updateCalculation();
if (!game.rolling) {
statusPill.textContent = "Set up";
setFeedback("Rotation count updated. Recalculate the distance as needed.");
}
});
rollButton.addEventListener("click", () => {
if (!game.rolling) startRoll();
});
hintButton.addEventListener("click", useHint);
newTargetButton.addEventListener("click", newRound);
resetButton.addEventListener("click", resetRound);
replayButton.addEventListener("click", () => {
resultModal.hidden = true;
startRoll();
});
nextButton.addEventListener("click", newRound);
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
// -------------------------------------------------------------------------
// Initial round and render loop
// -------------------------------------------------------------------------
game.target = {
distance: 2 * Math.PI * 0.65 * 6,
radius: 0.65,
rotations: 6
};
targetValue.textContent = game.target.distance.toFixed(3);
buildWorld();
buildRover();
updateCalculation();
updateStats();
cameraControl.focusX = game.target.distance * 0.48;
cameraControl.targetDistance = clamp(
14 + game.target.distance * 0.28,
16,
32
);
cameraControl.distance = cameraControl.targetDistance;
function animate(now) {
requestAnimationFrame(animate);
updateRun(now);
updateCamera();
// Keep the shadow light centered near the moving rover.
const lightX = rover ? rover.position.x : 0;
sun.position.x = lightX + 8;
sunTarget.position.set(lightX, 0, 0);
sunTarget.updateMatrixWorld();
renderer.render(scene, camera);
}
requestAnimationFrame(animate);
</script>
</body>
</html>
```