|
| 1 | +/* |
| 2 | +Toolbox Aid |
| 3 | +David Quesenberry |
| 4 | +04/15/2026 |
| 5 | +CubeExplorer3DScene.js |
| 6 | +*/ |
| 7 | +import { Scene } from '/src/engine/scene/index.js'; |
| 8 | +import { Theme, ThemeTokens } from '/src/engine/theme/index.js'; |
| 9 | +import { drawFrame, drawPanel } from '/src/engine/debug/index.js'; |
| 10 | +import { World } from '/src/engine/ecs/index.js'; |
| 11 | +import { stepWorldPhysics3D } from '/src/engine/systems/index.js'; |
| 12 | + |
| 13 | +const theme = new Theme(ThemeTokens); |
| 14 | +const BOX_EDGES = [ |
| 15 | + [0, 1], [1, 2], [2, 3], [3, 0], |
| 16 | + [4, 5], [5, 6], [6, 7], [7, 4], |
| 17 | + [0, 4], [1, 5], [2, 6], [3, 7], |
| 18 | +]; |
| 19 | + |
| 20 | +function clamp(value, min, max) { |
| 21 | + return Math.max(min, Math.min(max, value)); |
| 22 | +} |
| 23 | + |
| 24 | +function createBoxVertices(transform3D, size3D) { |
| 25 | + const x = transform3D.x; |
| 26 | + const y = transform3D.y; |
| 27 | + const z = transform3D.z; |
| 28 | + const w = size3D.width; |
| 29 | + const h = size3D.height; |
| 30 | + const d = size3D.depth; |
| 31 | + return [ |
| 32 | + { x, y, z }, |
| 33 | + { x: x + w, y, z }, |
| 34 | + { x: x + w, y: y + h, z }, |
| 35 | + { x, y: y + h, z }, |
| 36 | + { x, y, z: z + d }, |
| 37 | + { x: x + w, y, z: z + d }, |
| 38 | + { x: x + w, y: y + h, z: z + d }, |
| 39 | + { x, y: y + h, z: z + d }, |
| 40 | + ]; |
| 41 | +} |
| 42 | + |
| 43 | +function rotateToCameraSpace(point, cameraState) { |
| 44 | + const yaw = -(cameraState.rotation.y ?? 0); |
| 45 | + const pitch = -(cameraState.rotation.x ?? 0); |
| 46 | + |
| 47 | + let x = point.x - cameraState.position.x; |
| 48 | + let y = point.y - cameraState.position.y; |
| 49 | + let z = point.z - cameraState.position.z; |
| 50 | + |
| 51 | + const cosYaw = Math.cos(yaw); |
| 52 | + const sinYaw = Math.sin(yaw); |
| 53 | + const yawX = x * cosYaw - z * sinYaw; |
| 54 | + const yawZ = x * sinYaw + z * cosYaw; |
| 55 | + x = yawX; |
| 56 | + z = yawZ; |
| 57 | + |
| 58 | + const cosPitch = Math.cos(pitch); |
| 59 | + const sinPitch = Math.sin(pitch); |
| 60 | + const pitchY = y * cosPitch - z * sinPitch; |
| 61 | + const pitchZ = y * sinPitch + z * cosPitch; |
| 62 | + |
| 63 | + return { |
| 64 | + x, |
| 65 | + y: pitchY, |
| 66 | + z: pitchZ, |
| 67 | + }; |
| 68 | +} |
| 69 | + |
| 70 | +function projectPoint(point, cameraState, viewport) { |
| 71 | + const localPoint = rotateToCameraSpace(point, cameraState); |
| 72 | + if (localPoint.z <= 0.2) { |
| 73 | + return null; |
| 74 | + } |
| 75 | + |
| 76 | + const scale = viewport.focalLength / localPoint.z; |
| 77 | + return { |
| 78 | + x: viewport.centerX + localPoint.x * scale, |
| 79 | + y: viewport.centerY - localPoint.y * scale, |
| 80 | + depth: localPoint.z, |
| 81 | + }; |
| 82 | +} |
| 83 | + |
| 84 | +function drawWireBox(renderer, transform3D, size3D, cameraState, viewport, color) { |
| 85 | + const vertices = createBoxVertices(transform3D, size3D); |
| 86 | + const projected = vertices.map((vertex) => projectPoint(vertex, cameraState, viewport)); |
| 87 | + |
| 88 | + for (const [startIndex, endIndex] of BOX_EDGES) { |
| 89 | + const start = projected[startIndex]; |
| 90 | + const end = projected[endIndex]; |
| 91 | + if (!start || !end) { |
| 92 | + continue; |
| 93 | + } |
| 94 | + renderer.drawLine(start.x, start.y, end.x, end.y, color, 2); |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +export default class CubeExplorer3DScene extends Scene { |
| 99 | + constructor() { |
| 100 | + super(); |
| 101 | + this.world = new World(); |
| 102 | + this.moveSpeed = 7.5; |
| 103 | + this.cameraYaw = 0; |
| 104 | + this.cameraPitch = -0.25; |
| 105 | + this.turnSpeed = 1.8; |
| 106 | + this.pitchSpeed = 1.1; |
| 107 | + this.viewport = { |
| 108 | + x: 40, |
| 109 | + y: 170, |
| 110 | + width: 860, |
| 111 | + height: 320, |
| 112 | + focalLength: 460, |
| 113 | + }; |
| 114 | + this.worldBounds = { |
| 115 | + x: -8, |
| 116 | + y: -2, |
| 117 | + z: 2, |
| 118 | + width: 16, |
| 119 | + height: 8, |
| 120 | + depth: 22, |
| 121 | + }; |
| 122 | + this.lastPhysicsSummary = { movedEntities: 0, collisionCount: 0 }; |
| 123 | + |
| 124 | + this.playerId = this.world.createEntity(); |
| 125 | + this.world.addComponent(this.playerId, 'transform3D', { |
| 126 | + x: 0, |
| 127 | + y: 0, |
| 128 | + z: 8, |
| 129 | + previousX: 0, |
| 130 | + previousY: 0, |
| 131 | + previousZ: 8, |
| 132 | + }); |
| 133 | + this.world.addComponent(this.playerId, 'size3D', { width: 1.6, height: 1.6, depth: 1.6 }); |
| 134 | + this.world.addComponent(this.playerId, 'velocity3D', { x: 0, y: 0, z: 0 }); |
| 135 | + this.world.addComponent(this.playerId, 'collider3D', { enabled: true, solid: false }); |
| 136 | + this.world.addComponent(this.playerId, 'renderable3D', { color: '#7dd3fc' }); |
| 137 | + |
| 138 | + this.addSolidBox({ x: -3.5, y: -0.2, z: 10.5 }, { width: 2.2, height: 2.2, depth: 2.2 }, '#f87171'); |
| 139 | + this.addSolidBox({ x: 2.3, y: -0.2, z: 14.0 }, { width: 2.0, height: 3.0, depth: 2.0 }, '#fbbf24'); |
| 140 | + this.addSolidBox({ x: -1.0, y: -0.2, z: 18.0 }, { width: 3.0, height: 1.8, depth: 2.2 }, '#86efac'); |
| 141 | + } |
| 142 | + |
| 143 | + setCamera3D(camera3D) { |
| 144 | + this.camera3D = camera3D; |
| 145 | + this.syncCameraToPlayer(); |
| 146 | + } |
| 147 | + |
| 148 | + addSolidBox(transform3D, size3D, color = '#fca5a5') { |
| 149 | + const id = this.world.createEntity(); |
| 150 | + this.world.addComponent(id, 'transform3D', { |
| 151 | + x: transform3D.x, |
| 152 | + y: transform3D.y, |
| 153 | + z: transform3D.z, |
| 154 | + previousX: transform3D.x, |
| 155 | + previousY: transform3D.y, |
| 156 | + previousZ: transform3D.z, |
| 157 | + }); |
| 158 | + this.world.addComponent(id, 'size3D', { |
| 159 | + width: size3D.width, |
| 160 | + height: size3D.height, |
| 161 | + depth: size3D.depth, |
| 162 | + }); |
| 163 | + this.world.addComponent(id, 'solid3D', { enabled: true }); |
| 164 | + this.world.addComponent(id, 'renderable3D', { color }); |
| 165 | + } |
| 166 | + |
| 167 | + syncCameraToPlayer() { |
| 168 | + if (!this.camera3D) { |
| 169 | + return; |
| 170 | + } |
| 171 | + |
| 172 | + const playerTransform = this.world.requireComponent(this.playerId, 'transform3D'); |
| 173 | + const orbitDistance = 10; |
| 174 | + const cameraX = playerTransform.x + Math.sin(this.cameraYaw) * orbitDistance; |
| 175 | + const cameraZ = playerTransform.z - Math.cos(this.cameraYaw) * orbitDistance; |
| 176 | + const cameraY = playerTransform.y + 4.8; |
| 177 | + |
| 178 | + this.camera3D.setPosition({ |
| 179 | + x: cameraX, |
| 180 | + y: cameraY, |
| 181 | + z: cameraZ, |
| 182 | + }); |
| 183 | + this.camera3D.setRotation({ |
| 184 | + x: this.cameraPitch, |
| 185 | + y: this.cameraYaw, |
| 186 | + z: 0, |
| 187 | + }); |
| 188 | + } |
| 189 | + |
| 190 | + step3DPhysics(dt, engine) { |
| 191 | + const velocity = this.world.requireComponent(this.playerId, 'velocity3D'); |
| 192 | + const input = engine.input; |
| 193 | + |
| 194 | + velocity.x = 0; |
| 195 | + velocity.y = 0; |
| 196 | + velocity.z = 0; |
| 197 | + |
| 198 | + if (input?.isDown('KeyA')) velocity.x -= this.moveSpeed; |
| 199 | + if (input?.isDown('KeyD')) velocity.x += this.moveSpeed; |
| 200 | + if (input?.isDown('KeyR')) velocity.y += this.moveSpeed; |
| 201 | + if (input?.isDown('KeyF')) velocity.y -= this.moveSpeed; |
| 202 | + if (input?.isDown('KeyW')) velocity.z += this.moveSpeed; |
| 203 | + if (input?.isDown('KeyS')) velocity.z -= this.moveSpeed; |
| 204 | + |
| 205 | + const yawInput = (input?.isDown('ArrowRight') ? 1 : 0) - (input?.isDown('ArrowLeft') ? 1 : 0); |
| 206 | + const pitchInput = (input?.isDown('ArrowUp') ? 1 : 0) - (input?.isDown('ArrowDown') ? 1 : 0); |
| 207 | + this.cameraYaw += yawInput * this.turnSpeed * dt; |
| 208 | + this.cameraPitch = clamp(this.cameraPitch + pitchInput * this.pitchSpeed * dt, -0.85, 0.6); |
| 209 | + |
| 210 | + this.lastPhysicsSummary = stepWorldPhysics3D(this.world, dt, { |
| 211 | + worldBounds: this.worldBounds, |
| 212 | + }); |
| 213 | + this.syncCameraToPlayer(); |
| 214 | + } |
| 215 | + |
| 216 | + render(renderer) { |
| 217 | + drawFrame(renderer, theme, [ |
| 218 | + 'Sample 1601 - 3D Cube Explorer', |
| 219 | + 'Minimal 3D movement + AABB collision using an isolated physics loop', |
| 220 | + 'Move: W A S D | Vertical: R/F | Camera orbit: Arrow keys', |
| 221 | + 'Goal: navigate around blocking boxes while remaining inside world bounds', |
| 222 | + ]); |
| 223 | + |
| 224 | + renderer.strokeRect( |
| 225 | + this.viewport.x, |
| 226 | + this.viewport.y, |
| 227 | + this.viewport.width, |
| 228 | + this.viewport.height, |
| 229 | + '#d8d5ff', |
| 230 | + 2, |
| 231 | + ); |
| 232 | + |
| 233 | + const cameraState = this.camera3D?.getState?.() ?? { |
| 234 | + position: { x: 0, y: 3, z: -8 }, |
| 235 | + rotation: { x: -0.25, y: 0, z: 0 }, |
| 236 | + }; |
| 237 | + |
| 238 | + const projectionViewport = { |
| 239 | + centerX: this.viewport.x + this.viewport.width * 0.5, |
| 240 | + centerY: this.viewport.y + this.viewport.height * 0.5, |
| 241 | + focalLength: this.viewport.focalLength, |
| 242 | + }; |
| 243 | + |
| 244 | + for (let lineZ = 4; lineZ <= 24; lineZ += 2) { |
| 245 | + const start = projectPoint({ x: -8, y: -1, z: lineZ }, cameraState, projectionViewport); |
| 246 | + const end = projectPoint({ x: 8, y: -1, z: lineZ }, cameraState, projectionViewport); |
| 247 | + if (start && end) { |
| 248 | + renderer.drawLine(start.x, start.y, end.x, end.y, '#334155', 1); |
| 249 | + } |
| 250 | + } |
| 251 | + |
| 252 | + for (let lineX = -8; lineX <= 8; lineX += 2) { |
| 253 | + const start = projectPoint({ x: lineX, y: -1, z: 2 }, cameraState, projectionViewport); |
| 254 | + const end = projectPoint({ x: lineX, y: -1, z: 24 }, cameraState, projectionViewport); |
| 255 | + if (start && end) { |
| 256 | + renderer.drawLine(start.x, start.y, end.x, end.y, '#1f334d', 1); |
| 257 | + } |
| 258 | + } |
| 259 | + |
| 260 | + const entities = this.world.getEntitiesWith('transform3D', 'size3D', 'renderable3D').map((entityId) => ({ |
| 261 | + entityId, |
| 262 | + transform3D: this.world.requireComponent(entityId, 'transform3D'), |
| 263 | + size3D: this.world.requireComponent(entityId, 'size3D'), |
| 264 | + renderable3D: this.world.requireComponent(entityId, 'renderable3D'), |
| 265 | + })); |
| 266 | + |
| 267 | + entities.sort((left, right) => right.transform3D.z - left.transform3D.z); |
| 268 | + entities.forEach(({ transform3D, size3D, renderable3D }) => { |
| 269 | + drawWireBox( |
| 270 | + renderer, |
| 271 | + transform3D, |
| 272 | + size3D, |
| 273 | + cameraState, |
| 274 | + projectionViewport, |
| 275 | + renderable3D.color, |
| 276 | + ); |
| 277 | + }); |
| 278 | + |
| 279 | + const player = this.world.requireComponent(this.playerId, 'transform3D'); |
| 280 | + drawPanel(renderer, 620, 34, 300, 126, '3D Runtime', [ |
| 281 | + `Cube: x=${player.x.toFixed(2)} y=${player.y.toFixed(2)} z=${player.z.toFixed(2)}`, |
| 282 | + `Camera yaw: ${this.cameraYaw.toFixed(2)} pitch: ${this.cameraPitch.toFixed(2)}`, |
| 283 | + `Moved entities: ${this.lastPhysicsSummary.movedEntities}`, |
| 284 | + `Resolved collisions: ${this.lastPhysicsSummary.collisionCount}`, |
| 285 | + 'Physics loop: stepWorldPhysics3D (MovementSystem + AABB)', |
| 286 | + ]); |
| 287 | + } |
| 288 | +} |
0 commit comments