Update index.html
Browse files- 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
|
7 |
<style>
|
8 |
-
body { margin: 0;
|
9 |
canvas { display: block; }
|
10 |
</style>
|
11 |
</head>
|
12 |
<body>
|
13 |
-
|
14 |
-
|
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 |
-
|
20 |
-
|
21 |
-
|
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 |
-
//
|
29 |
-
|
30 |
-
|
31 |
-
controls.dampingFactor = 0.05;
|
32 |
-
controls.minDistance = 20;
|
33 |
-
controls.maxDistance = 200;
|
34 |
|
35 |
-
//
|
36 |
-
const
|
37 |
-
|
38 |
-
|
39 |
-
|
|
|
40 |
|
41 |
-
//
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
|
47 |
-
//
|
48 |
-
const
|
49 |
-
|
|
|
50 |
|
51 |
-
|
52 |
-
|
53 |
-
|
|
|
|
|
|
|
|
|
54 |
|
55 |
-
//
|
56 |
-
|
57 |
-
|
58 |
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
|
|
63 |
|
64 |
-
|
65 |
-
|
66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
-
//
|
69 |
-
|
70 |
-
|
71 |
-
rocket.
|
72 |
-
|
73 |
|
74 |
-
|
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>
|