Sure! Below is a single HTML file containing a very basic flight simulator with:
A large flying area (canvas).
Simple buildings as obstacles.
Basic keyboard controls to pitch, roll, and throttle.
Basic 3D-ish perspective rendering using simple 2D graphics.
A simple "plane" represented by a triangle.
Basic physics for flying forward and turning.
You can open this file in any modern browser and fly around with arrow keys and W/S for throttle.
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Basic Flight Simulator</title>
<style>
body, html {
margin: 0; padding: 0; overflow: hidden; background: #87ceeb;
font-family: monospace;
}
canvas {
display: block;
background: linear-gradient(to top, #4a7a0f 0%, #87ceeb 70%);
}
#info {
position: absolute;
top: 5px; left: 5px;
background: rgba(255,255,255,0.7);
padding: 5px 10px;
border-radius: 5px;
font-size: 14px;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<div id="info">
Controls:<br>
Arrow Left/Right: Roll<br>
Arrow Up/Down: Pitch<br>
W/S: Throttle Up/Down<br>
Space: Reset
</div>
<script>
(() => {
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
let W, H;
// Resize canvas to fill window
function resize() {
W = window.innerWidth;
H = window.innerHeight;
canvas.width = W;
canvas.height = H;
}
window.addEventListener('resize', resize);
resize();
// Plane state
const plane = {
pos: { x: 0, y: 100, z: 0 }, // y = altitude
pitch: 0, // radians, nose up/down
roll: 0, // radians, tilt left/right
yaw: 0, // radians, heading
speed: 0,
throttle: 0,
};
// Constants
const MAX_SPEED = 5;
const MIN_ALTITUDE = 10;
const MAX_ALTITUDE = 500;
const GRAVITY = 0.01;
const THROTTLE_STEP = 0.02;
const PITCH_STEP = 0.02;
const ROLL_STEP = 0.03;
// Controls state
const keys = {
left: false,
right: false,
up: false,
down: false,
w: false,
s: false,
space: false,
};
window.addEventListener('keydown', e => {
switch(e.code) {
case 'ArrowLeft': keys.left = true; break;
case 'ArrowRight': keys.right = true; break;
case 'ArrowUp': keys.up = true; break;
case 'ArrowDown': keys.down = true; break;
case 'KeyW': keys.w = true; break;
case 'KeyS': keys.s = true; break;
case 'Space': keys.space = true; break;
}
});
window.addEventListener('keyup', e => {
switch(e.code) {
case 'ArrowLeft': keys.left = false; break;
case 'ArrowRight': keys.right = false; break;
case 'ArrowUp': keys.up = false; break;
case 'ArrowDown': keys.down = false; break;
case 'KeyW': keys.w = false; break;
case 'KeyS': keys.s = false; break;
case 'Space': keys.space = false; break;
}
});
// Buildings - array of {x,z,width,depth,height}
const buildings = [];
const BUILDING_COUNT = 200;
const AREA_SIZE = 2000;
function generateBuildings() {
buildings.length = 0;
for(let i=0; i<BUILDING_COUNT; i++) {
const x = (Math.random() - 0.5) * AREA_SIZE;
const z = (Math.random() - 0.5) * AREA_SIZE;
const width = 20 + Math.random() * 40;
const depth = 20 + Math.random() * 40;
const height = 50 + Math.random() * 200;
buildings.push({x,z,width,depth,height});
}
}
generateBuildings();
// Projection parameters
const FOV = Math.PI / 3;
const NEAR = 0.1;
const FAR = 1000;
// Simple 3D to 2D projection
function project3D(x, y, z) {
// Camera is at plane.pos, looking in plane.yaw direction
// We'll transform world coords into camera space
// Translate relative to plane
let dx = x - plane.pos.x;
let dy = y - plane.pos.y;
let dz = z - plane.pos.z;
// Rotate around Y axis by -yaw (to align with camera forward)
const cosYaw = Math.cos(-plane.yaw);
const sinYaw = Math.sin(-plane.yaw);
let cx = dx * cosYaw - dz * sinYaw;
let cz = dx * sinYaw + dz * cosYaw;
let cy = dy;
// Rotate around X axis by pitch
const cosPitch = Math.cos(-plane.pitch);
const sinPitch = Math.sin(-plane.pitch);
let cy2 = cy * cosPitch - cz * sinPitch;
let cz2 = cy * sinPitch + cz * cosPitch;
cy = cy2;
cz = cz2;
// Rotate around Z axis by roll
const cosRoll = Math.cos(-plane.roll);
const sinRoll = Math.sin(-plane.roll);
let cx2 = cx * cosRoll - cy * sinRoll;
let cy3 = cx * sinRoll + cy * cosRoll;
cx = cx2;
cy = cy3;
// Perspective projection
if (cz <= NEAR) return null; // behind camera or too close
const scale = (H/2) / (Math.tan(FOV/2) * cz);
const sx = W/2 + cx * scale;
const sy = H/2 - cy * scale;
return {x: sx, y: sy, depth: cz};
}
// Draw building as a simple box (top-down + height)
function drawBuilding(b) {
// Draw 8 corners of the building box
const corners = [
{x: b.x - b.width/2, y: 0, z: b.z - b.depth/2},
{x: b.x + b.width/2, y: 0, z: b.z - b.depth/2},
{x: b.x + b.width/2, y: 0, z: b.z + b.depth/2},
{x: b.x - b.width/2, y: 0, z: b.z + b.depth/2},
{x: b.x - b.width/2, y: b.height, z: b.z - b.depth/2},
{x: b.x + b.width/2, y: b.height, z: b.z - b.depth/2},
{x: b.x + b.width/2, y: b.height, z: b.z + b.depth/2},
{x: b.x - b.width/2, y: b.height, z: b.z + b.depth/2},
];
// Project corners
const p = corners.map(c => project3D(c.x, c.y, c.z));
if (p.some(c => c === null)) return; // skip if any corner behind camera
// Draw walls (4 vertical faces)
ctx.fillStyle = '#555';
ctx.strokeStyle = '#222';
ctx.lineWidth = 1;
function drawFace(i1,i2,i3,i4) {
ctx.beginPath();
ctx.moveTo(p[i1].x, p[i1].y);
ctx.lineTo(p[i2].x, p[i2].y);
ctx.lineTo(p[i3].x, p[i3].y);
ctx.lineTo(p[i4].x, p[i4].y);
ctx.closePath();
ctx.fill();
ctx.stroke();
}
// Front face
drawFace(0,1,5,4);
// Right face
drawFace(1,2,6,5);
// Back face
drawFace(2,3,7,6);
// Left face
drawFace(3,0,4,7);
// Draw roof
ctx.fillStyle = '#777';
ctx.beginPath();
ctx.moveTo(p[4].x, p[4].y);
ctx.lineTo(p[5].x, p[5].y);
ctx.lineTo(p[6].x, p[6].y);
ctx.lineTo(p[7].x, p[7].y);
ctx.closePath();
ctx.fill();
ctx.stroke();
}
// Draw plane as a simple triangle in center bottom
function drawPlane() {
const size = 20;
ctx.save();
ctx.translate(W/2, H*0.8);
ctx.rotate(-plane.roll);
ctx.fillStyle = 'red';
ctx.strokeStyle = 'black';
ctx.lineWidth = 2;
ctx.beginPath();
ctx.moveTo(0, -size);
ctx.lineTo(size/2, size);
ctx.lineTo(-size/2, size);
ctx.closePath();
ctx.fill();
ctx.stroke();
ctx.restore();
}
// Update plane physics
function updatePlane() {
// Controls
if (keys.left) plane.roll -= ROLL_STEP;
if (keys.right) plane.roll += ROLL_STEP;
if (keys.up) plane.pitch += PITCH_STEP;
if (keys.down) plane.pitch -= PITCH_STEP;
if (keys.w) plane.throttle = Math.min(1, plane.throttle + THROTTLE_STEP);
if (keys.s) plane.throttle = Math.max(0, plane.throttle - THROTTLE_STEP);
if (keys.space) resetPlane();
// Limit pitch and roll
plane.pitch = Math.min(Math.max(plane.pitch, -Math.PI/6), Math.PI/6);
plane.roll = Math.min(Math.max(plane.roll, -Math.PI/3), Math.PI/3);
// Speed depends on throttle
plane.speed += (plane.throttle * MAX_SPEED - plane.speed) * 0.1;
// Update yaw based on roll (bank turns)
plane.yaw += plane.roll * 0.01;
// Update position
plane.pos.x += Math.sin(plane.yaw) * plane.speed;
plane.pos.z += Math.cos(plane.yaw) * plane.speed;
// Update altitude based on pitch and speed
plane.pos.y += Math.sin(plane.pitch) * plane.speed * 0.5;
// Gravity pulls down if no throttle
if (plane.throttle < 0.1) {
plane.pos.y -= GRAVITY * (1 - plane.throttle);
}
// Clamp altitude
if (plane.pos.y < MIN_ALTITUDE) plane.pos.y = MIN_ALTITUDE;
if (plane.pos.y > MAX_ALTITUDE) plane.pos.y = MAX_ALTITUDE;
}
// Reset plane to start position
function resetPlane() {
plane.pos = { x: 0, y: 100, z: 0 };
plane.pitch = 0;
plane.roll = 0;
plane.yaw = 0;
plane.speed = 0;
plane.throttle = 0;
}
resetPlane();
// Main loop
function loop() {
updatePlane();
// Clear
ctx.clearRect(0, 0, W, H);
// Draw ground grid
ctx.strokeStyle = '#3a5f0b';
ctx.lineWidth = 1;
const gridSize = 100;
for(let i = -AREA_SIZE/2; i <= AREA_SIZE/2; i += gridSize) {
// Lines parallel to X axis
let p1 = project3D(-AREA_SIZE/2, 0, i);
let p2 = project3D(AREA_SIZE/2, 0, i);
if (p1 && p2) {
ctx.beginPath();
ctx.moveTo(p1.x, p1.y);
ctx.lineTo(p2.x, p2.y);
ctx.stroke();
}
// Lines parallel to Z axis
p1 = project3D(i, 0, -AREA_SIZE/2);
p2 = project3D(i, 0, AREA_SIZE/2);
if (p1 && p2) {
ctx.beginPath();
ctx.moveTo(p1.x, p1.y);
ctx.lineTo(p2.x, p2.y);
ctx.stroke();
}
}
// Draw buildings sorted by distance (far to near)
buildings.sort((a,b) => {
const da = Math.hypot(a.x - plane.pos.x, a.z - plane.pos.z);
const db = Math.hypot(b.x - plane.pos.x, b.z - plane.pos.z);
return db - da;
});
for (const b of buildings) {
drawBuilding(b);
}
// Draw plane HUD info
ctx.fillStyle = 'black';
ctx.font = '16px monospace';
ctx.fillText(`Altitude: ${plane.pos.y.toFixed(1)} m`, 10, H - 60);
ctx.fillText(`Speed: ${(plane.speed*20).toFixed(1)} km/h`, 10, H - 40);
ctx.fillText(`Throttle: ${(plane.throttle*100).toFixed(0)}%`, 10, H - 20);
// Draw plane icon
drawPlane();
requestAnimationFrame(loop);
}
loop();
})();
</script>
</body>
</html>
How to use:
Save the above code as flight_simulator.html.
Open it in a modern browser (Chrome, Firefox, Edge).
Use arrow keys to pitch and roll.
Use W/S to increase/decrease throttle.
Press Space to reset position.
Fly around a large area with simple buildings.
This is a very basic and minimal flight sim, but it demonstrates the core concepts: 3D projection, simple physics, keyboard controls, and rendering buildings. You can expand it with textures, better physics, collision detection, etc. if you want!