Meta Description" name="description" />
<!DOCTYPE html>
<html>
<head>
<title>Rocket Solar System</title>
<style>
body { margin:0; overflow:hidden; }
button {
position:absolute;
padding:10px 15px;
font-size:16px;
cursor:pointer;
z-index:1;
}
#engineBtn { top:10px; left:10px; }
#fuelBtn { top:10px; left:130px; }
#launchBtn { top:10px; left:240px; }
#altitude {
position:absolute;
top:60px;
left:10px;
font-size:20px;
color:white;
z-index:1;
}
</style>
</head>
<body>
<button id="engineBtn">Add Engine</button>
<button id="fuelBtn">Add Fuel</button>
<button id="launchBtn">Launch</button>
<div id="altitude">Height: 0 m</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/three@0.128/examples/js/controls/OrbitControls.js"></script>
<script>
let scene = new THREE.Scene();
scene.background = new THREE.Color(0x87CEEB);
let camera = new THREE.PerspectiveCamera(75, window.innerWidth/window.innerHeight, 0.1, 100000);
camera.position.set(0,10,30);
let renderer = new THREE.WebGLRenderer({antialias:true});
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// ORBIT CONTROLS (Mouse Control)
let controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
// LIGHT
let light = new THREE.PointLight(0xffffff, 2);
light.position.set(0,5000,2000);
scene.add(light);
// GROUND
let ground = new THREE.Mesh(
new THREE.PlaneGeometry(500,500),
new THREE.MeshStandardMaterial({color:0x228B22})
);
ground.rotation.x = -Math.PI/2;
scene.add(ground);
// STARS
let starGeo = new THREE.BufferGeometry();
let starVertices = [];
for(let i=0;i<3000;i++){
starVertices.push(
THREE.MathUtils.randFloatSpread(40000),
THREE.MathUtils.randFloatSpread(40000),
THREE.MathUtils.randFloatSpread(40000)
);
}
starGeo.setAttribute('position',
new THREE.Float32BufferAttribute(starVertices,3));
let stars = new THREE.Points(
starGeo,
new THREE.PointsMaterial({color:0xffffff})
);
scene.add(stars);
// PLANET FUNCTION
function createPlanet(size,color,y){
let p = new THREE.Mesh(
new THREE.SphereGeometry(size,32,32),
new THREE.MeshStandardMaterial({color:color})
);
p.position.y = y;
scene.add(p);
return p;
}
let earth = createPlanet(200,0x2266dd,-300);
let mars = createPlanet(40,0xff3300,-2000);
let jupiter = createPlanet(80,0xff9966,-3500);
// ROCKET
let rocket = new THREE.Group();
let body = new THREE.Mesh(
new THREE.CylinderGeometry(0.5,0.5,4,32),
new THREE.MeshStandardMaterial({color:0xffffff})
);
body.position.y=2;
rocket.add(body);
let nose = new THREE.Mesh(
new THREE.ConeGeometry(0.5,1.5,32),
new THREE.MeshStandardMaterial({color:0xff0000})
);
nose.position.y=4.75;
rocket.add(nose);
rocket.position.y=0;
scene.add(rocket);
// PHYSICS
let thrust=0,fuel=0,velocity=0,gravity=0.02,launched=false;
document.getElementById("engineBtn").onclick=()=>{thrust+=0.3;}
document.getElementById("fuelBtn").onclick=()=>{fuel+=50;}
document.getElementById("launchBtn").onclick=()=>{
if(fuel>0 && thrust>0) launched=true;
else alert("Add Engine & Fuel First!");
};
function animate(){
requestAnimationFrame(animate);
if(launched){
if(fuel>0){
velocity+=thrust;
fuel-=0.4;
}
velocity-=gravity;
rocket.position.y+=velocity;
}
if(rocket.position.y>3000){
scene.background=new THREE.Color(0x000000);
}
earth.rotation.y+=0.001;
mars.rotation.y+=0.002;
jupiter.rotation.y+=0.0008;
document.getElementById("altitude").innerText=
"Height: "+rocket.position.y.toFixed(2)+" m";
controls.update();
renderer.render(scene,camera);
}
animate();
</script>
</body>
</html>3
3
129KB
619KB
2,617.0ms
208.0ms
2,617.0ms