akhaliq HF Staff commited on
Commit
011ac1b
Β·
verified Β·
1 Parent(s): b79e6f7

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +176 -87
index.html CHANGED
@@ -3,11 +3,12 @@
3
  <head>
4
  <meta charset="UTF-8">
5
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
- <title>Three.js Flight Simulator</title>
7
  <style>
8
- body { margin: 0; background-color: #87CEEB; overflow: hidden; }
9
  canvas { display: block; }
10
- #controls { position: absolute; top: 10px; left: 10px; background: rgba(255,255,255,0.7); padding: 10px; font-family: monospace; }
 
11
  </style>
12
  </head>
13
  <body>
@@ -16,86 +17,144 @@
16
  <b>Flight Simulator Controls:</b><br>
17
  W / ↑ : Pitch Up (Climb)<br>
18
  S / ↓ : Pitch Down (Dive)<br>
19
- A / ← : Yaw Left (Turn Left)<br>
20
- D / β†’ : Yaw Right (Turn Right)<br>
21
- Q / E : Roll Left / Right (Bank)<br>
22
- ↑↑ Speed up (W pressed faster)<br>
23
- ↓↓ Slow down (S pressed slower)<br>
24
- Mouse Look (move mouse to look around)
 
 
 
 
 
 
 
25
  </div>
26
 
27
  <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
28
  <script>
29
- // Our scene
30
  let scene = new THREE.Scene();
31
- scene.background = new THREE.Color(0x87CEEB); // Sky blue
32
-
33
- // Camera = our eyes in the plane
34
- let camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
35
 
36
- // Renderer
37
- let renderer = new THREE.WebGLRenderer();
38
- renderer.setSize(window.innerWidth, window.innerHeight);
39
- document.body.appendChild(renderer.domElement);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
 
41
- // Simple ground (big plane)
42
- let groundGeom = new THREE.PlaneGeometry(100, 100);
43
- let groundMat = new THREE.MeshLambertMaterial({ color: 0x32CD32 }); // Lime green
 
 
 
 
 
 
44
  let ground = new THREE.Mesh(groundGeom, groundMat);
45
- ground.rotation.x = -Math.PI / 2; // Lay it flat
46
- ground.position.y = -2; // A bit below zero
47
  scene.add(ground);
48
 
49
- // Simple airplane (just for show, a box + 2 triangles for "wings")
50
- let planeGeom = new THREE.BufferGeometry();
51
- const planeVertices = new Float32Array([
52
- // Fuselage (box)
53
- -1, -0.2, 1, 1, -0.2, 1, 1, 0.2, 1, -1, 0.2, 1, // front face
54
- -1, -0.2, -1, 1, -0.2, -1, 1, 0.2, -1, -1, 0.2, -1, // back face
55
- -1, -0.2, -1, -1, -0.2, 1, -1, 0.2, 1, -1, 0.2, -1, // left face
56
- 1, -0.2, -1, 1, -0.2, 1, 1, 0.2, 1, 1, 0.2, -1, // right face
57
- -1, -0.2, -1, 1, -0.2, -1, 1, -0.2, 1, -1, -0.2, 1, // bottom face
58
- -1, 0.2, -1, 1, 0.2, -1, 1, 0.2, 1, -1, 0.2, 1, // top face
59
- // Left wing (triangle)
60
- -1, 0, 1, -2, 0, 0.5, -1, 0, -1,
61
- // Right wing (triangle)
62
- 1, 0, 1, 2, 0, 0.5, 1, 0, -1,
 
 
 
 
 
 
 
 
63
  ]);
64
- let posAttr = new THREE.BufferAttribute(planeVertices, 3);
65
- planeGeom.setAttribute('position', posAttr);
66
- let planeMat = new THREE.MeshBasicMaterial({ color: 0xFFFFFF }); // White plane
67
- let plane = new THREE.Mesh(planeGeom, planeMat);
68
- scene.add(plane);
69
- camera.position.y = 2; // Sit in the "cockpit"
70
- camera.position.z = 5; // A bit forward
71
- plane.add(camera); // Camera is now child of plane, so it moves with it
72
-
73
- // Lighting (2 lights, ambient + directional)
74
- let ambientLight = new THREE.AmbientLight(0x444444);
 
 
 
 
 
 
 
 
75
  scene.add(ambientLight);
76
  let dirLight = new THREE.DirectionalLight(0xFFFFFF, 0.8);
77
- dirLight.position.set(5, 10, 5);
 
 
 
 
 
78
  scene.add(dirLight);
79
 
80
- // Flight dynamics variables
81
- let pitch = 0; // Up/down nose
82
- let yaw = 0; // Left/right turn
83
- let roll = 0; // Left/right tilt (banking)
84
- let speed = 0.05; // Forward movement speed
85
- let maxSpeed = 0.2;
86
- let minSpeed = 0.01;
87
 
88
- // Mouse look variables
89
- let mouseDown = false;
90
- let lastMouseX, lastMouseY;
91
- let sensitivity = 0.005;
 
 
 
 
 
 
 
 
 
 
 
92
 
93
- // Key controls state
94
  let keys = {
95
  w: false, s: false, a: false, d: false, q: false, e: false
96
  };
97
-
98
- // Handle key presses
99
  document.addEventListener('keydown', (e) => {
100
  switch (e.key) {
101
  case 'w': case 'ArrowUp': keys.w = true; break;
@@ -117,7 +176,10 @@ document.addEventListener('keyup', (e) => {
117
  }
118
  });
119
 
120
- // Handle mouse for looking around
 
 
 
121
  document.addEventListener('mousedown', (e) => {
122
  mouseDown = true;
123
  lastMouseX = e.clientX;
@@ -130,43 +192,70 @@ document.addEventListener('mousemove', (e) => {
130
  let dy = e.clientY - lastMouseY;
131
  yaw -= dx * sensitivity;
132
  pitch -= dy * sensitivity;
133
- pitch = Math.max(-Math.PI/2, Math.min(Math.PI/2, pitch)); // limit straight up/down
134
  lastMouseX = e.clientX;
135
  lastMouseY = e.clientY;
136
  }
137
  });
138
 
139
- // Main animation loop
 
 
 
 
 
 
 
140
  function animate() {
141
  requestAnimationFrame(animate);
142
 
143
- // Controls logic
144
- if (keys.w) pitch -= 0.01; // Nose down to climb
145
- if (keys.s) pitch += 0.01; // Nose up to dive
146
- if (keys.a) yaw -= 0.02; // Turn left
147
- if (keys.d) yaw += 0.02; // Turn right
148
- if (keys.q) roll -= 0.02; // Roll left
149
- if (keys.e) roll += 0.02; // Roll right
150
-
151
- // Speed adjustment (very basic)
152
- if (keys.w && speed < maxSpeed) speed *= 1.01; // Accelerate
153
- if (keys.s && speed > minSpeed) speed *= 0.99; // Brake
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
154
 
155
  // Limit angles
156
- pitch = Math.max(-Math.PI/2, Math.min(Math.PI/2, pitch));
157
  roll = Math.max(-Math.PI/4, Math.min(Math.PI/4, roll));
158
 
159
- // Move plane forward (always toward its blue axis, which is -Z in its local space)
160
- plane.translateZ(-speed); // Negative because Three.js uses right-hand coord system
161
-
162
- // Apply rotations (order matters: roll, pitch, yaw - like aircraft conventions)
163
- plane.rotation.z = roll;
164
- plane.rotation.x = pitch;
165
- plane.rotation.y = yaw;
166
 
167
- // Just for visual niceness: move the directional light with camera a bit
168
- dirLight.position.copy(camera.getWorldPosition(new THREE.Vector3()).add(new THREE.Vector3(5, 10, 5)));
169
 
 
170
  renderer.render(scene, camera);
171
  }
172
  animate();
 
3
  <head>
4
  <meta charset="UTF-8">
5
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>Enhanced Three.js Flight Simulator</title>
7
  <style>
8
+ body { margin: 0; background-color: #000; overflow: hidden; }
9
  canvas { display: block; }
10
+ #controls { position: absolute; top: 10px; left: 10px; background: rgba(255,255,255,0.7); padding: 10px; font-family: monospace; font-size: 14px; }
11
+ #instruments { position: absolute; top: 10px; right: 10px; background: rgba(0,0,0,0.7); color: #0F0; padding: 10px; font-family: monospace; font-size: 14px; }
12
  </style>
13
  </head>
14
  <body>
 
17
  <b>Flight Simulator Controls:</b><br>
18
  W / ↑ : Pitch Up (Climb)<br>
19
  S / ↓ : Pitch Down (Dive)<br>
20
+ A / ← : Yaw Left (Turn Left, but use roll for real turns)<br>
21
+ D / β†’ : Yaw Right (Turn Right, but use roll for real turns)<br>
22
+ Q / E : Roll Left/Right (Bank for turns)<br>
23
+ ↑↑ Speed up (gradually)<br>
24
+ ↓↓ Slow down (gradually)<br>
25
+ Mouse Look (drag to change view direction)
26
+ </div>
27
+
28
+ <div id="instruments">
29
+ <b>Flight Instruments:</b><br>
30
+ Alt: <span id="altimeter">0 ft</span><br>
31
+ Airspeed: <span id="airspeed">0 kts</span><br>
32
+ Heading: <span id="heading">0Β°</span>
33
  </div>
34
 
35
  <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
36
  <script>
37
+ // Scene
38
  let scene = new THREE.Scene();
 
 
 
 
39
 
40
+ // Skybox (gradient sky)
41
+ let skyGeom = new THREE.SphereGeometry(500, 32, 32);
42
+ let skyMat = new THREE.ShaderMaterial({
43
+ vertexShader: `
44
+ varying vec3 vWorldPosition;
45
+ void main() {
46
+ vec4 worldPosition = modelMatrix * vec4(position, 1.0);
47
+ vWorldPosition = worldPosition.xyz;
48
+ gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
49
+ }
50
+ `,
51
+ fragmentShader: `
52
+ varying vec3 vWorldPosition;
53
+ void main() {
54
+ float heightFactor = (vWorldPosition.y + 250.0) / 500.0;
55
+ heightFactor = clamp(heightFactor, 0.0, 1.0);
56
+ vec3 topColor = vec3(0.1, 0.4, 0.8); // Light blue
57
+ vec3 bottomColor = vec3(0.8, 0.6, 0.2); // Light brown
58
+ gl_FragColor = vec4(mix(bottomColor, topColor, heightFactor), 1.0);
59
+ }
60
+ `,
61
+ side: THREE.BackSide
62
+ });
63
+ let sky = new THREE.Mesh(skyGeom, skyMat);
64
+ scene.add(sky);
65
 
66
+ // Ground (simple hills)
67
+ let groundGeom = new THREE.PlaneGeometry(200, 200, 64, 64);
68
+ for (let i = 0; i < groundGeom.attributes.position.count; i++) {
69
+ let x = groundGeom.attributes.position.getX(i);
70
+ let z = groundGeom.attributes.position.getZ(i);
71
+ let y = Math.sin(x * 0.1) * Math.cos(z * 0.1) * 5.0; // Simple hills
72
+ groundGeom.attributes.position.setY(i, y);
73
+ }
74
+ let groundMat = new THREE.MeshLambertMaterial({ color: 0x228B22 }); // Forest green
75
  let ground = new THREE.Mesh(groundGeom, groundMat);
76
+ ground.rotation.x = -Math.PI / 2;
77
+ ground.receiveShadow = true;
78
  scene.add(ground);
79
 
80
+ // Airplane (slightly nicer mesh)
81
+ let planeGeom = new THREE.Group();
82
+ let fuselageGeom = new THREE.BoxGeometry(2, 0.4, 6);
83
+ let fuselageMat = new THREE.MeshLambertMaterial({ color: 0xFFFFFF });
84
+ let fuselage = new THREE.Mesh(fuselageGeom, fuselageMat);
85
+ fuselage.castShadow = true;
86
+ let wingGeom = new THREE.BufferGeometry();
87
+ const wingVertices = new Float32Array([
88
+ -2, 0, 1, 2, 0, 1, 2, 0, -2,
89
+ -2, 0, 1, 2, 0, -2, -2, 0, -2,
90
+ ]);
91
+ let posAttr = new THREE.BufferAttribute(wingVertices, 3);
92
+ wingGeom.setAttribute('position', posAttr);
93
+ let wingMat = new THREE.MeshLambertMaterial({ color: 0xFFFFFF });
94
+ let wingLeft = new THREE.Mesh(wingGeom, wingMat);
95
+ wingLeft.position.x = -0.5;
96
+ wingLeft.castShadow = true;
97
+ let wingRight = wingLeft.clone();
98
+ wingRight.position.x = 0.5;
99
+ let tailGeom = new THREE.BufferGeometry();
100
+ const tailVertices = new Float32Array([
101
+ 0, 0.5, -3, -0.5, 0, -3, 0.5, 0, -3
102
  ]);
103
+ let tailAttr = new THREE.BufferAttribute(tailVertices, 3);
104
+ tailGeom.setAttribute('position', tailAttr);
105
+ let tailMat = new THREE.MeshLambertMaterial({ color: 0xFFFFFF });
106
+ let tail = new THREE.Mesh(tailGeom, tailMat);
107
+ tail.castShadow = true;
108
+ planeGeom.add(fuselage);
109
+ planeGeom.add(wingLeft);
110
+ planeGeom.add(wingRight);
111
+ planeGeom.add(tail);
112
+ scene.add(planeGeom);
113
+
114
+ // Camera
115
+ let camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
116
+ planeGeom.add(camera);
117
+ camera.position.y = 1.5;
118
+ camera.position.z = 2;
119
+
120
+ // Lighting
121
+ let ambientLight = new THREE.AmbientLight(0x333333);
122
  scene.add(ambientLight);
123
  let dirLight = new THREE.DirectionalLight(0xFFFFFF, 0.8);
124
+ dirLight.position.set(10, 20, 10);
125
+ dirLight.castShadow = true;
126
+ dirLight.shadow.mapSize.width = 2048;
127
+ dirLight.shadow.mapSize.height = 2048;
128
+ dirLight.shadow.camera.near = 1;
129
+ dirLight.shadow.camera.far = 100;
130
  scene.add(dirLight);
131
 
132
+ // Renderer
133
+ let renderer = new THREE.WebGLRenderer({ antialias: true });
134
+ renderer.setSize(window.innerWidth, window.innerHeight);
135
+ renderer.shadowMap.enabled = true;
136
+ document.body.appendChild(renderer.domElement);
 
 
137
 
138
+ // Flight dynamics
139
+ let pitch = 0; // radians
140
+ let yaw = 0;
141
+ let roll = 0;
142
+ let velocity = new THREE.Vector3(0, 0, 0); // m/s
143
+ let airspeed = 0; // knots
144
+ let altitude = 0; // meters
145
+ let heading = 0; // degrees
146
+ let liftForce = 0;
147
+ let dragForce = 0;
148
+ let gravity = new THREE.Vector3(0, -9.81, 0); // m/sΒ²
149
+ let mass = 1000; // kg (just a number)
150
+ let wingLiftCoefficient = 10.0; // Made-up lift coeff
151
+ let dragCoefficient = 0.5; // Parasitic drag
152
+ let maxStallAngle = Math.PI / 6; // 30 degrees
153
 
154
+ // Controls state
155
  let keys = {
156
  w: false, s: false, a: false, d: false, q: false, e: false
157
  };
 
 
158
  document.addEventListener('keydown', (e) => {
159
  switch (e.key) {
160
  case 'w': case 'ArrowUp': keys.w = true; break;
 
176
  }
177
  });
178
 
179
+ // Mouse look
180
+ let mouseDown = false;
181
+ let lastMouseX, lastMouseY;
182
+ let sensitivity = 0.005;
183
  document.addEventListener('mousedown', (e) => {
184
  mouseDown = true;
185
  lastMouseX = e.clientX;
 
192
  let dy = e.clientY - lastMouseY;
193
  yaw -= dx * sensitivity;
194
  pitch -= dy * sensitivity;
195
+ pitch = Math.max(-Math.PI/2, Math.min(Math.PI/2, pitch));
196
  lastMouseX = e.clientX;
197
  lastMouseY = e.clientY;
198
  }
199
  });
200
 
201
+ // Update instruments display
202
+ function updateInstruments() {
203
+ document.getElementById('altimeter').innerText = Math.round(altitude * 3.28084) + ' ft'; // meters to feet
204
+ document.getElementById('airspeed').innerText = Math.round(airspeed) + ' kts';
205
+ document.getElementById('heading').innerText = Math.round(THREE.Math.radToDeg(yaw)) % 360 + 'Β°';
206
+ }
207
+
208
+ // Main loop
209
  function animate() {
210
  requestAnimationFrame(animate);
211
 
212
+ // Aerodynamics
213
+ let velocityDir = new THREE.Vector3(0, 0, -1); // Plane's forward in local space
214
+ velocityDir.applyQuaternion(planeGeom.quaternion).normalize();
215
+ airspeed = velocity.length() * 1.94384; // m/s to knots (approx)
216
+ let angleOfAttack = Math.acos(velocityDir.dot(new THREE.Vector3(0, 1, 0).applyQuaternion(planeGeom.quaternion)));
217
+ liftForce = wingLiftCoefficient * airspeed * airspeed * Math.sin(angleOfAttack);
218
+ if (angleOfAttack > maxStallAngle) liftForce *= (1 - (angleOfAttack - maxStallAngle) / (Math.PI / 2 - maxStallAngle)); // Stall
219
+ dragForce = dragCoefficient * airspeed * airspeed;
220
+
221
+ // Forces
222
+ let lift = new THREE.Vector3(0, liftForce, 0).applyQuaternion(planeGeom.quaternion); // Lift always perpendicular to wings
223
+ let drag = velocityDir.clone().multiplyScalar(-dragForce);
224
+ let totalForce = new THREE.Vector3().add(gravity).add(lift).add(drag).divideScalar(mass);
225
+
226
+ // Update velocity & position (Verlet-ish integration)
227
+ velocity.add(totalForce.multiplyScalar(1/60)); // dt ~= 1/60s
228
+ planeGeom.position.add(velocity.clone().multiplyScalar(1/60));
229
+
230
+ // Collision with ground (very crude)
231
+ altitude = planeGeom.position.y - groundGeom.attributes.position.getY(0); // approx
232
+ if (altitude < 0) {
233
+ planeGeom.position.y += -altitude; // push back up
234
+ velocity.y = Math.max(0, velocity.y * 0.8); // dampen bounce
235
+ }
236
+
237
+ // Controls
238
+ if (keys.w) pitch -= 0.005;
239
+ if (keys.s) pitch += 0.005;
240
+ if (keys.a) yaw -= 0.01;
241
+ if (keys.d) yaw += 0.01;
242
+ if (keys.q) roll -= 0.02;
243
+ if (keys.e) roll += 0.02;
244
 
245
  // Limit angles
246
+ pitch = Math.max(-Math.PI/3, Math.min(Math.PI/4, pitch)); // less extreme pitch
247
  roll = Math.max(-Math.PI/4, Math.min(Math.PI/4, roll));
248
 
249
+ // Apply rotations (order matters)
250
+ planeGeom.rotation.order = 'ZXY'; // Roll, Pitch, Yaw (aviation standard)
251
+ planeGeom.rotation.z = roll;
252
+ planeGeom.rotation.x = pitch;
253
+ planeGeom.rotation.y = yaw;
 
 
254
 
255
+ // Update camera direction (following plane's rotation)
256
+ heading = THREE.Math.radToDeg(yaw) % 360;
257
 
258
+ updateInstruments();
259
  renderer.render(scene, camera);
260
  }
261
  animate();