o3-mini· OpenAI
LMArena Human Preference 140K
cool, can we improve that a little bit? please ensure that there: 1. the spaceship is…
- User
cool, can we improve that a little bit?
please ensure that there:
- the spaceship is visible witho toggle to first perrson
- there are other space objects
- perhaps infinite expansion of space?
- Assistant
Below is an enhanced version of the space flight simulator. This version includes:
- View Toggle: Press V to switch between third-person (spaceship is fully visible) and first-person (cockpit-like view).
- Other Space Objects: Randomly generated planets/asteroids are spawned around you.
- Infinite Expansion: As you travel through space, objects that become too distant are removed and new ones are generated around you to simulate an endless space.
Just copy the entire code into an
index.htmlfile and open it in your browser.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Three.js Space Flight Simulator - Enhanced</title> <style> body { margin: 0; overflow: hidden; background: black; } #info { position: absolute; top: 10px; left: 10px; color: white; z-index: 1; font-family: sans-serif; line-height: 1.5; } </style> </head> <body> <div id="info"> <b>Space Flight Simulator Enhanced</b><br> WASD: Move<br> Mouse: Look<br> Space/Shift: Up/Down<br> V: Toggle View (First/Third Person)<br> Click to lock pointer </div> <script type="module"> import * as THREE from 'https://cdn.jsdelivr.net/npm/three@0.153.0/build/three.module.js'; // === Scene Setup === const scene = new THREE.Scene(); scene.background = new THREE.Color(0x000010); // Renderer const renderer = new THREE.WebGLRenderer({ antialias: true }); renderer.setSize(window.innerWidth, window.innerHeight); document.body.appendChild(renderer.domElement); // === Starfield Background === const starGeometry = new THREE.BufferGeometry(); const starCount = 3000; const starVertices = []; for (let i = 0; i < starCount; i++) { const x = (Math.random() - 0.5) * 4000; const y = (Math.random() - 0.5) * 4000; const z = (Math.random() - 0.5) * 4000; starVertices.push(x, y, z); } starGeometry.setAttribute('position', new THREE.Float32BufferAttribute(starVertices, 3)); const starMaterial = new THREE.PointsMaterial({ color: 0xffffff, size: 1 }); const stars = new THREE.Points(starGeometry, starMaterial); scene.add(stars); // === Lighting === const ambientLight = new THREE.AmbientLight(0x888888); scene.add(ambientLight); const pointLight = new THREE.PointLight(0xffffff, 1); pointLight.position.set(10, 10, 10); scene.add(pointLight); // === Spaceship Group & Mesh === // We use a group so we can move the ship and attach the camera relative to it. const spaceship = new THREE.Group(); scene.add(spaceship); // A simple cone represents the ship body. const shipGeometry = new THREE.ConeGeometry(1, 3, 16); const shipMaterial = new THREE.MeshPhongMaterial({ color: 0x00ffcc }); const shipMesh = new THREE.Mesh(shipGeometry, shipMaterial); shipMesh.rotation.x = Math.PI / 2; // So it points forward (negative-z) spaceship.add(shipMesh); // === Camera Setup === const camera = new THREE.PerspectiveCamera(75, window.innerWidth/window.innerHeight, 0.1, 10000); scene.add(camera); // Start in third-person mode. // In third-person mode, the camera is placed behind and above the ship. // In first-person, the camera sits at the ship's cockpit (with a slight offset). let viewMode = "third"; // === Controls State === let moveForward = false, moveBackward = false, moveLeft = false, moveRight = false, moveUp = false, moveDown = false; let canControl = false; let pitch = 0, yaw = 0; // === Pointer Lock === renderer.domElement.addEventListener('click', () => { renderer.domElement.requestPointerLock(); }); document.addEventListener('pointerlockchange', () => { canControl = (document.pointerLockElement === renderer.domElement); }); document.addEventListener('mousemove', (event) => { if (!canControl) return; yaw -= event.movementX * 0.002; pitch -= event.movementY * 0.002; // Limit pitch to avoid flipping pitch = Math.max(-Math.PI/2, Math.min(Math.PI/2, pitch)); }); // === Keyboard Handling === document.addEventListener('keydown', (event) => { switch (event.code) { case 'KeyW': moveForward = true; break; case 'KeyS': moveBackward = true; break; case 'KeyA': moveLeft = true; break; case 'KeyD': moveRight = true; break; case 'Space': moveUp = true; break; case 'ShiftLeft': moveDown = true; break; case 'KeyV': // Toggle between third-person and first-person view modes. viewMode = (viewMode === "third") ? "first" : "third"; break; } }); document.addEventListener('keyup', (event) => { switch (event.code) { case 'KeyW': moveForward = false; break; case 'KeyS': moveBackward = false; break; case 'KeyA': moveLeft = false; break; case 'KeyD': moveRight = false; break; case 'Space': moveUp = false; break; case 'ShiftLeft': moveDown = false; break; } }); // === Space Objects (Planets/Asteroids) === let spaceObjects = []; // Create a random space object (either a planet or an asteroid) function createSpaceObject() { const type = Math.random() < 0.5 ? "planet" : "asteroid"; let object; if (type === "planet") { const radius = Math.random() * 5 + 2; // radius between 2 and 7 const geometry = new THREE.SphereGeometry(radius, 32, 32); const color = new THREE.Color(Math.random(), Math.random(), Math.random()); const material = new THREE.MeshPhongMaterial({ color: color }); object = new THREE.Mesh(geometry, material); } else { // Asteroids: using an icosahedron for a rough shape const radius = Math.random() * 3 + 1; const geometry = new THREE.IcosahedronGeometry(radius, 1); const material = new THREE.MeshPhongMaterial({ color: 0x888888 }); object = new THREE.Mesh(geometry, material); } return object; } // Spawn objects around a given position. // minDist and maxDist determine how far from the position objects will be placed. function spawnSpaceObjectsAround(position, count = 10, minDist = 50, maxDist = 500) { for (let i = 0; i < count; i++) { const obj = createSpaceObject(); // Random spherical coordinates: const theta = Math.acos(THREE.MathUtils.randFloatSpread(2)); // polar angle [0, PI] const phi = THREE.MathUtils.randFloat(0, Math.PI * 2); // azimuthal angle [0, 2PI] const distance = THREE.MathUtils.randFloat(minDist, maxDist); const x = position.x + distance * Math.sin(theta) * Math.cos(phi); const y = position.y + distance * Math.sin(theta) * Math.sin(phi); const z = position.z + distance * Math.cos(theta); obj.position.set(x, y, z); scene.add(obj); spaceObjects.push(obj); } } // Initially spawn some objects around the starting position. spawnSpaceObjectsAround(spaceship.position, 30); // === Animation Loop === function animate() { requestAnimationFrame(animate); // --- Calculate Movement Direction --- const moveDir = new THREE.Vector3(); if (moveForward) moveDir.z -= 1; if (moveBackward) moveDir.z += 1; if (moveLeft) moveDir.x -= 1; if (moveRight) moveDir.x += 1; if (moveUp) moveDir.y += 1; if (moveDown) moveDir.y -= 1; if (moveDir.lengthSq() > 0) { moveDir.normalize(); } // --- Update Spaceship Rotation --- // The spaceship rotates based on mouse movement. spaceship.rotation.order = 'YXZ'; spaceship.rotation.y = yaw; spaceship.rotation.x = pitch; // --- Move the Spaceship in its local coordinate space --- const speed = 1; const localMove = moveDir.clone().applyEuler(spaceship.rotation); spaceship.position.add(localMove.multiplyScalar(speed)); // --- Update Camera Based on View Mode --- if (viewMode === "third") { // In third-person view, place the camera offset behind and above the spaceship. const thirdOffset = new THREE.Vector3(0, 3, 10); thirdOffset.applyEuler(spaceship.rotation); camera.position.copy(spaceship.position).add(thirdOffset); camera.lookAt(spaceship.position); } else { // In first-person view the camera sits at (or slightly inside) the spaceship. const firstOffset = new THREE.Vector3(0, 0.5, 0); // cockpit offset firstOffset.applyEuler(spaceship.rotation); camera.position.copy(spaceship.position).add(firstOffset); // Look in the forward direction in local space (negative z) const forward = new THREE.Vector3(0, 0, -1); forward.applyEuler(spaceship.rotation); camera.lookAt(camera.position.clone().add(forward)); } // --- Infinite Space: Manage Space Objects --- // Remove objects far from the spaceship. const currentPos = spaceship.position; spaceObjects = spaceObjects.filter(obj => { if (obj.position.distanceTo(currentPos) > 600) { scene.remove(obj); return false; } return true; }); // If we don’t have enough objects around, spawn more. if (spaceObjects.length < 30) { spawnSpaceObjectsAround(currentPos, 10); } renderer.render(scene, camera); } animate(); // --- Handle Window Resize --- window.addEventListener('resize', () => { camera.aspect = window.innerWidth / window.innerHeight; camera.updateProjectionMatrix(); renderer.setSize(window.innerWidth, window.innerHeight); }); </script> </body> </html>
How It Works
-
Control & Pointer Lock:
- Click on the canvas to lock the pointer and enable mouse-look.
- Use WASD, Space, and Shift for movement.
- Move the mouse to change your yaw (horizontal rotation) and pitch (vertical rotation).
-
Spaceship & Camera:
- The spaceship (a simple cone) is part of a group. The camera’s position is updated each frame based on the selected view mode:
- Third-Person View: The camera stays behind and above the spaceship so you see it fully.
- First-Person View: The camera moves to a cockpit-like position so you see where you’re going.
- Press V to toggle between the two.
- The spaceship (a simple cone) is part of a group. The camera’s position is updated each frame based on the selected view mode:
-
Infinite Space Objects:
- Random planets and asteroids are scattered around you.
- As you move, objects that are too far are removed and new ones are spawned near you to create the feeling of an infinite universe.
Enjoy exploring your endless universe, and feel free to expand on the code further with more features, models, or physics!
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.