Portrait 3D Game Series · Three.js Arcade Starter
How to Build a 3D Wall Flip Game in Three.js
Last updated
Want to build your own version of Wall Flip? This step-by-step Three.js tutorial walks through the exact arcade loop behind our portrait mobile game — one-tap wall-to-wall jump arcs, timing-based spike dodging, pooled hazards and gems, and rising difficulty with zero external physics engine.
Build & Customize Faster
Want to Modify This Game and Publish One Yourself?
Change the flip time, swap the orb and wall colors, add new hazards, or build your own neon theme. Grab the free standalone starter code below, or join Indie Forge Studio Membership to unlock our interactive Live Code Editor, premium Three.js tutorials, and ad-free play across every game.
How Does a Three.js Portrait Wall Flip Game Work? (Quick Answer)
A Three.js Wall Flip game needs just one input: tap to jump to the opposite wall. Move the player along a short smoothstep arc between the two wall X positions in about 0.34 seconds while the shaft scrolls upward, ignore taps mid-flip so timing matters, and test each spike by comparing its height and wall side to the player. Gems, rising speed and pooled hazards turn that loop into an endless arcade climber.
What You Need
- Any modern browser and a plain text editor — no build step or bundler required.
- Basic familiarity with a Three.js scene, camera, and
requestAnimationFrameloop. - Want to see the finished game first? Play Wall Flip on Indie Forge Studio, or read the companion Rocket Lander portrait tutorial.
Step 1: Portrait Shaft Scene, Aspect-Aware Camera & Recycled Rungs
Wall Flip lives in a tall, narrow shaft, so the camera must keep both walls on screen on a phone in portrait and on a wide desktop. Compute the camera distance from the aspect ratio (dist = 3.7 / (tan(fov/2) * aspect)), cap devicePixelRatio at 1.75, and recycle a fixed set of neon rungs around the player so the shaft feels endless with only a few dozen meshes.
const renderer = new THREE.WebGLRenderer({ canvas, antialias: true, powerPreference: 'high-performance' });
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 1.75));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.setSize(window.innerWidth, window.innerHeight);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x05030f);
scene.fog = new THREE.FogExp2(0x05030f, 0.032);
const camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 0.1, 140);
function frameCamera() {
camera.aspect = window.innerWidth / window.innerHeight;
// keep both walls visible in portrait and landscape
camera.userData.dist = Math.max(10.5, 3.7 / (Math.tan(THREE.MathUtils.degToRad(camera.fov / 2)) * camera.aspect));
camera.updateProjectionMatrix();
}
frameCamera();
// Recycled neon rungs on both walls (they scroll as the camera climbs)
const RUNG_GAP = 2.4, RUNGS = 26;
const rungMat = new THREE.MeshBasicMaterial({ color: 0x2ad1d6 });
const rungs = [];
for (let i = 0; i < RUNGS; i++) {
for (const s of [-1, 1]) {
const r = new THREE.Mesh(new THREE.BoxGeometry(0.12, 0.12, 3.02), rungMat);
r.position.x = s * (WALL - 0.02);
r.userData.i = i;
scene.add(r);
rungs.push(r);
}
}
Step 2: The Orb, Pooled Spikes & Gems
The player is one emissive sphere with a spinning halo ring. Hazards come from fixed pools of 14 spikes and 14 gems that are reused as the camera climbs: spawnRow() places a spike on a random wall and a gem on the opposite wall, so every row asks the player a one-tap question. Flipping scale.x keeps every spike tip pointed into the shaft.
// Player orb with glow ring
const player = new THREE.Group();
player.add(new THREE.Mesh(new THREE.SphereGeometry(0.42, 20, 16), new THREE.MeshStandardMaterial({ color: 0xfff1b8, emissive: 0xffb020, emissiveIntensity: 0.6, roughness: 0.3 })));
const halo = new THREE.Mesh(new THREE.TorusGeometry(0.62, 0.05, 8, 28), new THREE.MeshBasicMaterial({ color: 0xffc94a }));
player.add(halo);
scene.add(player);
// Pooled hazards (spikes) and gems
const POOL = 14;
const spikeGeo = new THREE.ConeGeometry(0.5, 1.5, 4);
spikeGeo.rotateZ(-Math.PI / 2); // tip points toward +x (inside the shaft for the left wall)
const spikeMat = new THREE.MeshStandardMaterial({ color: 0xff3b6b, emissive: 0xaa1030, roughness: 0.4 });
const gemGeo = new THREE.OctahedronGeometry(0.4);
const gemMat = new THREE.MeshStandardMaterial({ color: 0x4ade80, emissive: 0x168a4a, roughness: 0.25 });
const spikes = [], gems = [];
for (let i = 0; i < POOL; i++) {
const sp = new THREE.Mesh(spikeGeo, spikeMat); sp.userData = { side: 1, y: -99 }; scene.add(sp); spikes.push(sp);
const gm = new THREE.Mesh(gemGeo, gemMat); gm.userData = { x: 0, y: -99, on: false }; scene.add(gm); gems.push(gm);
}
let spawnY = 7, spikeCursor = 0, gemCursor = 0;
function spawnRow() {
const side = Math.random() < 0.5 ? -1 : 1;
const sp = spikes[spikeCursor]; spikeCursor = (spikeCursor + 1) % POOL;
sp.userData.side = side; sp.userData.y = spawnY;
sp.scale.x = -side; // flip so the tip always points into the shaft
sp.position.set(side * (WALL - 0.55), spawnY, 0);
if (Math.random() < 0.75) {
const gm = gems[gemCursor]; gemCursor = (gemCursor + 1) % POOL;
gm.userData.x = -side * (WALL - 0.9); gm.userData.y = spawnY + 1.2; gm.userData.on = true; gm.visible = true;
}
spawnY += 3.6 + Math.random() * 2.4;
}
Step 3: One-Tap Wall Flip: Smoothstep Jump Arc & Unified Input
Every input (tap, click, Space, arrow keys) calls the same flip() function. A flip is a short timed arc: store the start and end wall X, advance flipT from 0 to 1 over 0.34s, and ease X with smoothstep while the orb keeps climbing slightly faster so the jump feels like a launch. Inputs during a flip are ignored, which is what makes timing matter.
// One input: flip to the opposite wall (only when we are resting on a wall)
function flip(dir) {
if (flipT < 1 || status !== 'play') return;
const target = dir || -side;
if (target === side) return;
side = target; flipFrom = px; flipTo = side * (WALL - 0.55); flipT = 0;
beep(380, 0.12, 'triangle', 260);
}
window.addEventListener('keydown', (e) => {
if (!interactive) return;
const k = e.key.toLowerCase();
if (k === ' ' || k === 'w' || k === 'arrowup') { flip(0); e.preventDefault(); }
else if (k === 'a' || k === 'arrowleft') flip(-1);
else if (k === 'd' || k === 'arrowright') flip(1);
});
canvas.addEventListener('pointerdown', () => { if (interactive) flip(0); });
Step 4: Timing, Spike Hits & Gem Pickups
Collision is pure comparison, no physics engine. A spike is dangerous when the orb is within 0.8 units of its height and either resting on that wall or mid-flip while still within 1.0 of the wall. Gems are collected by a simple box test. The background autopilot uses the same data to look ahead and flip early, which is a handy way to test your spacing.
// Hazards and pickups
for (let i = 0; i < POOL; i++) {
const s = spikes[i].userData;
if (s.y < py - 12) continue;
const near = Math.abs(py - s.y) < 0.8;
const touching = flipT >= 1 ? side === s.side : Math.abs(px - s.side * (WALL - 0.55)) < 1.0;
if (interactive && near && touching) {
status = 'crashed'; resetTimer = 1.3; best = Math.max(best, score);
beep(170, 0.34, 'sawtooth', -110);
updateHud('SPIKED! Height ' + Math.floor(py) + 'm');
break;
}
const g = gems[i].userData;
if (g.on && Math.abs(py - g.y) < 0.8 && Math.abs(px - g.x) < 1.2) {
g.on = false; gems[i].visible = false; gemCount++;
beep(740, 0.1, 'triangle', 420);
}
}
Step 5: Rising Difficulty, Score & Zero-Asset Web Audio
The climb speed grows with height (3.6 + min(3.2, py * 0.012)), so early flips are forgiving and later rows demand tighter timing. Score is height plus 25 per gem, and every flip, gem and crash tone is synthesized with AudioContext oscillators, so the whole game ships with zero image or audio downloads.
const climb = 3.6 + Math.min(3.2, py * 0.012);
// ...
score = Math.floor(py) + gemCount * 25;
function beep(freq, dur, type = 'triangle', slide = 0) {
try {
if (!actx) actx = new (window.AudioContext || window.webkitAudioContext)();
if (actx.state === 'suspended') actx.resume();
const o = actx.createOscillator(), g = actx.createGain(), t = actx.currentTime;
o.type = type; o.frequency.setValueAtTime(freq, t);
if (slide) o.frequency.exponentialRampToValueAtTime(Math.max(40, freq + slide), t + dur);
g.gain.setValueAtTime(0.06, t); g.gain.exponentialRampToValueAtTime(0.0001, t + dur);
o.connect(g).connect(actx.destination); o.start(t); o.stop(t + dur + 0.02);
} catch (e) {}
}
Complete Single-File Starter Code
Save as wall-flip.html and open in any browser, or click Launch to test immediately.
<!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>Wall Flip 3D Starter</title>
<style>
html, body { margin: 0; height: 100%; overflow: hidden; background: #05030f; touch-action: none; font-family: system-ui, sans-serif; }
#bg-canvas { position: fixed; inset: 0; width: 100%; height: 100%; }
#demo-hud { position: fixed; top: 14px; left: 50%; transform: translateX(-50%); padding: 8px 18px; border-radius: 12px;
background: rgba(6,9,18,.8); border: 1px solid rgba(255,255,255,.18); color: #e5e7eb; font-size: 14px; text-align: center; }
</style>
<script type="importmap">{ "imports": { "three": "https://unpkg.com/three@0.186.1/build/three.module.js" } }</script>
</head>
<body>
<canvas id="bg-canvas"></canvas>
<div id="demo-hud"></div>
<script type="module">
import * as THREE from 'three';
const canvas = document.getElementById('bg-canvas');
const renderer = new THREE.WebGLRenderer({ canvas, antialias: true, powerPreference: 'high-performance' });
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 1.75));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.setSize(window.innerWidth, window.innerHeight);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x05030f);
scene.fog = new THREE.FogExp2(0x05030f, 0.032);
const camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 0.1, 140);
function frameCamera() {
camera.aspect = window.innerWidth / window.innerHeight;
// keep both walls visible in portrait and landscape
camera.userData.dist = Math.max(10.5, 3.7 / (Math.tan(THREE.MathUtils.degToRad(camera.fov / 2)) * camera.aspect));
camera.updateProjectionMatrix();
}
frameCamera();
scene.add(new THREE.HemisphereLight(0x88c8ff, 0x1a1030, 0.9));
const keyLight = new THREE.DirectionalLight(0xffffff, 1.2);
keyLight.position.set(4, 10, 12);
scene.add(keyLight);
// Shaft: two neon walls at x = -WALL and +WALL
const WALL = 2.6;
const wallMat = new THREE.MeshStandardMaterial({ color: 0x141a33, roughness: 0.7, metalness: 0.3 });
const walls = [-1, 1].map((s) => {
const m = new THREE.Mesh(new THREE.BoxGeometry(0.6, 60, 3), wallMat);
m.position.set(s * (WALL + 0.3), 0, 0);
scene.add(m);
return m;
});
// Recycled neon rungs on both walls (they scroll as the camera climbs)
const RUNG_GAP = 2.4, RUNGS = 26;
const rungMat = new THREE.MeshBasicMaterial({ color: 0x2ad1d6 });
const rungs = [];
for (let i = 0; i < RUNGS; i++) {
for (const s of [-1, 1]) {
const r = new THREE.Mesh(new THREE.BoxGeometry(0.12, 0.12, 3.02), rungMat);
r.position.x = s * (WALL - 0.02);
r.userData.i = i;
scene.add(r);
rungs.push(r);
}
}
// Player orb with glow ring
const player = new THREE.Group();
player.add(new THREE.Mesh(new THREE.SphereGeometry(0.42, 20, 16), new THREE.MeshStandardMaterial({ color: 0xfff1b8, emissive: 0xffb020, emissiveIntensity: 0.6, roughness: 0.3 })));
const halo = new THREE.Mesh(new THREE.TorusGeometry(0.62, 0.05, 8, 28), new THREE.MeshBasicMaterial({ color: 0xffc94a }));
player.add(halo);
scene.add(player);
// Pooled hazards (spikes) and gems
const POOL = 14;
const spikeGeo = new THREE.ConeGeometry(0.5, 1.5, 4);
spikeGeo.rotateZ(-Math.PI / 2); // tip points toward +x (inside the shaft for the left wall)
const spikeMat = new THREE.MeshStandardMaterial({ color: 0xff3b6b, emissive: 0xaa1030, roughness: 0.4 });
const gemGeo = new THREE.OctahedronGeometry(0.4);
const gemMat = new THREE.MeshStandardMaterial({ color: 0x4ade80, emissive: 0x168a4a, roughness: 0.25 });
const spikes = [], gems = [];
for (let i = 0; i < POOL; i++) {
const sp = new THREE.Mesh(spikeGeo, spikeMat); sp.userData = { side: 1, y: -99 }; scene.add(sp); spikes.push(sp);
const gm = new THREE.Mesh(gemGeo, gemMat); gm.userData = { x: 0, y: -99, on: false }; scene.add(gm); gems.push(gm);
}
let spawnY = 7, spikeCursor = 0, gemCursor = 0;
function spawnRow() {
const side = Math.random() < 0.5 ? -1 : 1;
const sp = spikes[spikeCursor]; spikeCursor = (spikeCursor + 1) % POOL;
sp.userData.side = side; sp.userData.y = spawnY;
sp.scale.x = -side; // flip so the tip always points into the shaft
sp.position.set(side * (WALL - 0.55), spawnY, 0);
if (Math.random() < 0.75) {
const gm = gems[gemCursor]; gemCursor = (gemCursor + 1) % POOL;
gm.userData.x = -side * (WALL - 0.9); gm.userData.y = spawnY + 1.2; gm.userData.on = true; gm.visible = true;
}
spawnY += 3.6 + Math.random() * 2.4;
}
// Pooled trail (zero per-frame allocations)
const MAX_T = 40;
const tPos = new Float32Array(MAX_T * 3).fill(-99);
const tLife = new Float32Array(MAX_T);
const tGeo = new THREE.BufferGeometry();
tGeo.setAttribute('position', new THREE.BufferAttribute(tPos, 3));
scene.add(new THREE.Points(tGeo, new THREE.PointsMaterial({ color: 0xffd36b, size: 0.2, transparent: true, opacity: 0.8, blending: THREE.AdditiveBlending, depthWrite: false })));
let tCursor = 0;
function emitTrail(x, y) {
tPos[tCursor * 3] = x; tPos[tCursor * 3 + 1] = y; tPos[tCursor * 3 + 2] = 0;
tLife[tCursor] = 0.45; tCursor = (tCursor + 1) % MAX_T;
}
// Synthesized Web Audio
let actx = null;
function beep(freq, dur, type = 'triangle', slide = 0) {
try {
if (!actx) actx = new (window.AudioContext || window.webkitAudioContext)();
if (actx.state === 'suspended') actx.resume();
const o = actx.createOscillator(), g = actx.createGain(), t = actx.currentTime;
o.type = type; o.frequency.setValueAtTime(freq, t);
if (slide) o.frequency.exponentialRampToValueAtTime(Math.max(40, freq + slide), t + dur);
g.gain.setValueAtTime(0.06, t); g.gain.exponentialRampToValueAtTime(0.0001, t + dur);
o.connect(g).connect(actx.destination); o.start(t); o.stop(t + dur + 0.02);
} catch (e) {}
}
// Game state
let interactive = false;
let status = 'play', resetTimer = 0, score = 0, best = 0, gemCount = 0;
let side = -1, px = -(WALL - 0.55), py = 0, flipT = 1, flipFrom = px, flipTo = px, clock = 0;
const FLIP_TIME = 0.34;
const hudEl = document.getElementById('demo-hud');
function updateHud(msg) {
if (!hudEl) return;
hudEl.innerHTML = 'HEIGHT <strong>' + Math.floor(py) + 'm</strong> · GEMS <strong>' + gemCount + '</strong> · BEST <strong>' + best + '</strong>' +
(msg ? '<div style="margin-top:4px;color:#2ad1d6;font-weight:700;">' + msg + '</div>' : '');
}
function resetRound() {
side = -1; px = -(WALL - 0.55); py = 0; flipT = 1; score = 0; gemCount = 0; status = 'play'; resetTimer = 0;
spawnY = 7;
spikes.forEach((s) => { s.userData.y = -99; s.position.y = -99; });
gems.forEach((g) => { g.userData.on = false; g.visible = false; });
for (let i = 0; i < 4; i++) spawnRow();
updateHud('Tap, Click or Space to flip walls');
}
for (let i = 0; i < 4; i++) spawnRow();
// One input: flip to the opposite wall (only when we are resting on a wall)
function flip(dir) {
if (flipT < 1 || status !== 'play') return;
const target = dir || -side;
if (target === side) return;
side = target; flipFrom = px; flipTo = side * (WALL - 0.55); flipT = 0;
beep(380, 0.12, 'triangle', 260);
}
window.addEventListener('keydown', (e) => {
if (!interactive) return;
const k = e.key.toLowerCase();
if (k === ' ' || k === 'w' || k === 'arrowup') { flip(0); e.preventDefault(); }
else if (k === 'a' || k === 'arrowleft') flip(-1);
else if (k === 'd' || k === 'arrowright') flip(1);
});
canvas.addEventListener('pointerdown', () => { if (interactive) flip(0); });
window.addEventListener('resize', () => {
frameCamera();
renderer.setSize(window.innerWidth, window.innerHeight);
});
interactive = true;
let last = performance.now();
function animate(now) {
requestAnimationFrame(animate);
const dt = Math.min(0.05, (now - last) / 1000);
last = now; clock += dt;
if (status === 'play') {
const climb = 3.6 + Math.min(3.2, py * 0.012);
if (flipT < 1) {
flipT = Math.min(1, flipT + dt / FLIP_TIME);
const e = flipT * flipT * (3 - 2 * flipT); // smoothstep: ease out of one wall, into the next
px = THREE.MathUtils.lerp(flipFrom, flipTo, e);
py += climb * 1.35 * dt;
} else {
py += climb * dt;
}
// Background autopilot: flip away from the next spike on our wall, never dies
if (!interactive && flipT >= 1) {
for (let i = 0; i < POOL; i++) {
const s = spikes[i].userData;
if (s.side === side && s.y > py - 0.5 && s.y - py < 3.4) { flip(0); break; }
}
}
// Hazards and pickups
for (let i = 0; i < POOL; i++) {
const s = spikes[i].userData;
if (s.y < py - 12) continue;
const near = Math.abs(py - s.y) < 0.8;
const touching = flipT >= 1 ? side === s.side : Math.abs(px - s.side * (WALL - 0.55)) < 1.0;
if (interactive && near && touching) {
status = 'crashed'; resetTimer = 1.3; best = Math.max(best, score);
beep(170, 0.34, 'sawtooth', -110);
updateHud('SPIKED! Height ' + Math.floor(py) + 'm');
break;
}
const g = gems[i].userData;
if (g.on && Math.abs(py - g.y) < 0.8 && Math.abs(px - g.x) < 1.2) {
g.on = false; gems[i].visible = false; gemCount++;
beep(740, 0.1, 'triangle', 420);
}
}
score = Math.floor(py) + gemCount * 25;
if (status === 'play') updateHud();
// Keep hazards flowing above the camera
while (spawnY < py + 24) spawnRow();
emitTrail(px, py);
} else {
resetTimer -= dt;
if (resetTimer <= 0) resetRound();
}
player.position.set(px, py, 0);
halo.rotation.set(clock * 2.1, clock * 1.4, 0);
for (let i = 0; i < POOL; i++) {
gems[i].rotation.y = clock * 2.4;
gems[i].position.set(gems[i].userData.x, gems[i].userData.y + Math.sin(clock * 3 + i) * 0.12, 0);
}
// Wall + rung recycling around the player
walls.forEach((w) => { w.position.y = py; });
const base = Math.floor(py / RUNG_GAP) * RUNG_GAP;
for (let j = 0; j < rungs.length; j++) {
const r = rungs[j];
r.position.y = base + (r.userData.i - RUNGS / 2) * RUNG_GAP;
}
for (let i = 0; i < MAX_T; i++) {
if (tLife[i] > 0) tLife[i] -= dt; else tPos[i * 3 + 1] = -99;
}
tGeo.attributes.position.needsUpdate = true;
const camY = py + 3.2;
camera.position.set(0, camY, camera.userData.dist);
camera.lookAt(0, camY, 0);
renderer.render(scene, camera);
}
resetRound();
requestAnimationFrame(animate);
</script>
</body>
</html>
Frequently Asked Questions
Do I need a physics engine to build a 3D Wall Flip game?
No. A wall-to-wall jump is a timed arc, not a rigid-body simulation. Easing the X position with smoothstep while the orb climbs at a fixed speed gives tighter mobile feel, loads instantly and avoids any WASM physics download.
How do you make a one-tap Wall Flip feel fair?
Ignore taps while a flip is in progress, keep the flip time short (about 0.34 seconds), and spawn spikes at least 3.6 units apart. Start slow and ramp the climb speed with height so players learn the timing before it gets tight.
How does spike collision work in Wall Flip?
Each spike stores its wall side and height. If the orb is within 0.8 units of that height and is either resting on that wall or still within 1.0 unit of it during a flip, the run ends. Gems use a similar small box test.
Can I modify this Wall Flip code and publish my own version?
Yes. Copy or launch the standalone starter code above, change the flip time, spike spacing or orb look, or become an Indie Forge Studio member to unlock the interactive Live Code Editor and premium Three.js tutorials.