eaglelandsonce commited on
Commit
e4cc7ca
Β·
verified Β·
1 Parent(s): 36db671

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +70 -63
index.html CHANGED
@@ -3,88 +3,95 @@
3
  <head>
4
  <meta charset="UTF-8">
5
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
- <title>Rocket Orbiting Earth</title>
7
  <style>
8
- body { margin: 0; overflow: hidden; background-color: black; }
9
  canvas { display: block; }
10
  </style>
11
  </head>
12
  <body>
13
- <!-- Load Three.js -->
14
- <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r152/three.min.js"></script>
15
- <!-- Load OrbitControls -->
16
- <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r152/examples/js/controls/OrbitControls.js"></script>
17
-
18
  <script>
19
- console.log("πŸš€ Initializing Three.js Rocket Orbit Simulation");
20
-
21
- // βœ… 1. Scene, Camera, and Renderer
22
- const scene = new THREE.Scene();
23
- const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 500);
24
- const renderer = new THREE.WebGLRenderer();
25
- renderer.setSize(window.innerWidth, window.innerHeight);
26
- document.body.appendChild(renderer.domElement);
27
 
28
- // βœ… 2. Add OrbitControls (for camera movement)
29
- const controls = new THREE.OrbitControls(camera, renderer.domElement);
30
- controls.enableDamping = true;
31
- controls.dampingFactor = 0.05;
32
- controls.minDistance = 20;
33
- controls.maxDistance = 200;
34
 
35
- // βœ… 3. Create Earth (Blue Sphere)
36
- const earthGeometry = new THREE.SphereGeometry(10, 32, 32);
37
- const earthMaterial = new THREE.MeshStandardMaterial({ color: 0x2233ff });
38
- const earth = new THREE.Mesh(earthGeometry, earthMaterial);
39
- scene.add(earth);
 
40
 
41
- // βœ… 4. Create Rocket (Red Sphere)
42
- const rocketGeometry = new THREE.SphereGeometry(1, 16, 16);
43
- const rocketMaterial = new THREE.MeshStandardMaterial({ color: 0xff0000 });
44
- const rocket = new THREE.Mesh(rocketGeometry, rocketMaterial);
45
- scene.add(rocket);
 
 
 
 
 
 
46
 
47
- // βœ… 5. Lighting (Improved for Better Visibility)
48
- const ambientLight = new THREE.AmbientLight(0xffffff, 0.5);
49
- scene.add(ambientLight);
 
50
 
51
- const pointLight = new THREE.PointLight(0xffffff, 1.5, 100);
52
- pointLight.position.set(30, 30, 30);
53
- scene.add(pointLight);
 
 
 
 
54
 
55
- // βœ… 6. Camera Position
56
- camera.position.set(0, 30, 50);
57
- camera.lookAt(earth.position);
58
 
59
- // βœ… 7. Orbit Parameters
60
- const orbitRadius = 25; // Distance from Earth
61
- const orbitSpeed = 0.01; // Speed of orbit
62
- let angle = 0; // Angle of orbit
 
63
 
64
- // βœ… 8. Animation Loop
65
- function animate() {
66
- requestAnimationFrame(animate);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
 
68
- // Update Rocket Position (Circular Orbit)
69
- angle += orbitSpeed;
70
- rocket.position.x = orbitRadius * Math.cos(angle);
71
- rocket.position.z = orbitRadius * Math.sin(angle);
72
- rocket.position.y = 5 * Math.sin(angle * 0.5); // Small vertical oscillation
73
 
74
- controls.update();
75
- renderer.render(scene, camera);
76
  }
77
 
78
  animate(); // Start animation
79
-
80
- // βœ… 9. Window Resize Handling
81
- window.addEventListener("resize", () => {
82
- camera.aspect = window.innerWidth / window.innerHeight;
83
- camera.updateProjectionMatrix();
84
- renderer.setSize(window.innerWidth, window.innerHeight);
85
- });
86
-
87
- console.log("βœ… Simulation Running Successfully");
88
  </script>
89
  </body>
90
  </html>
 
3
  <head>
4
  <meta charset="UTF-8">
5
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>2D Rocket Launch and Orbit Simulation</title>
7
  <style>
8
+ body { margin: 0; background-color: black; overflow: hidden; }
9
  canvas { display: block; }
10
  </style>
11
  </head>
12
  <body>
13
+ <canvas id="canvas"></canvas>
14
+
 
 
 
15
  <script>
16
+ // Get canvas and context
17
+ const canvas = document.getElementById("canvas");
18
+ const ctx = canvas.getContext("2d");
 
 
 
 
 
19
 
20
+ // Set canvas size
21
+ canvas.width = window.innerWidth;
22
+ canvas.height = window.innerHeight;
 
 
 
23
 
24
+ // Define Earth
25
+ const earth = {
26
+ x: canvas.width / 2,
27
+ y: canvas.height - 100,
28
+ radius: 50
29
+ };
30
 
31
+ // Define Rocket
32
+ let rocket = {
33
+ x: earth.x,
34
+ y: earth.y - earth.radius - 10, // Start above Earth
35
+ radius: 5,
36
+ velocityY: 0,
37
+ velocityX: 0,
38
+ isLaunching: false,
39
+ inOrbit: false,
40
+ orbitAngle: 0
41
+ };
42
 
43
+ // Launch Parameters
44
+ const launchSpeed = -2.5; // Initial launch speed
45
+ const gravity = 0.05; // Simulated gravity pulling rocket down
46
+ const orbitSpeed = 0.02; // Speed in orbit
47
 
48
+ // Listen for spacebar press to launch
49
+ document.addEventListener("keydown", (event) => {
50
+ if (event.code === "Space" && !rocket.isLaunching && !rocket.inOrbit) {
51
+ rocket.isLaunching = true;
52
+ rocket.velocityY = launchSpeed;
53
+ }
54
+ });
55
 
56
+ // Animation loop
57
+ function animate() {
58
+ ctx.clearRect(0, 0, canvas.width, canvas.height);
59
 
60
+ // Draw Earth
61
+ ctx.fillStyle = "blue";
62
+ ctx.beginPath();
63
+ ctx.arc(earth.x, earth.y, earth.radius, 0, Math.PI * 2);
64
+ ctx.fill();
65
 
66
+ // Rocket Launching Phase
67
+ if (rocket.isLaunching && !rocket.inOrbit) {
68
+ rocket.y += rocket.velocityY;
69
+ rocket.velocityY += gravity; // Simulate gravity
70
+
71
+ // Check if rocket reaches orbit height
72
+ if (rocket.y < earth.y - 150) {
73
+ rocket.isLaunching = false;
74
+ rocket.inOrbit = true;
75
+ rocket.orbitAngle = 0; // Reset angle for orbit
76
+ }
77
+ }
78
+ // Orbit Phase
79
+ else if (rocket.inOrbit) {
80
+ rocket.orbitAngle += orbitSpeed;
81
+ rocket.x = earth.x + 150 * Math.cos(rocket.orbitAngle);
82
+ rocket.y = earth.y - 150 * Math.sin(rocket.orbitAngle);
83
+ }
84
 
85
+ // Draw Rocket
86
+ ctx.fillStyle = "red";
87
+ ctx.beginPath();
88
+ ctx.arc(rocket.x, rocket.y, rocket.radius, 0, Math.PI * 2);
89
+ ctx.fill();
90
 
91
+ requestAnimationFrame(animate);
 
92
  }
93
 
94
  animate(); // Start animation
 
 
 
 
 
 
 
 
 
95
  </script>
96
  </body>
97
  </html>