|
1 | 1 | import { Application, Vector2, Palette, Keyboard } from "vectorjs"; |
2 | 2 |
|
| 3 | +const enable_orbit_paths = false; |
| 4 | +const enable_starfield = true; |
| 5 | + |
| 6 | + |
3 | 7 | const screenWidth = 1200; |
4 | 8 | const screenHeight = 1200; |
5 | 9 | const sunPos = new Vector2(screenWidth / 2, screenHeight / 2); |
6 | 10 | const fpsPos = new Vector2(10, 10); |
7 | 11 |
|
8 | | -// Define planets with varied speeds, orbital radii, and attached moons |
| 12 | +// 1. Generate a static background starfield |
| 13 | +const starCount = 200; |
| 14 | +const stars = Array.from({ length: starCount }, () => ({ |
| 15 | + pos: new Vector2(Math.random() * screenWidth, Math.random() * screenHeight), |
| 16 | + size: Math.random() < 0.8 ? 1 : 2, // Most stars are 1px, some are 2px |
| 17 | + color: Math.random() < 0.2 ? Palette.LIGHTGRAY : Palette.WHITE // Slight variety |
| 18 | +})); |
| 19 | + |
9 | 20 | const planets = [ |
10 | 21 | { name: "Mercury", radius: 70, size: 6, speed: 2.5, color: Palette.GRAY, moons: [] }, |
11 | 22 | { name: "Venus", radius: 110, size: 10, speed: 1.8, color: Palette.ORANGE, moons: [] }, |
@@ -80,74 +91,74 @@ const planets = [ |
80 | 91 |
|
81 | 92 | const app = new Application(screenWidth, screenHeight, "Solar System Simulation"); |
82 | 93 |
|
83 | | -// Simulation State |
84 | 94 | let isRunning = true; |
85 | 95 | let time = 0; |
86 | 96 |
|
87 | 97 | app.run({ |
88 | | - |
89 | 98 | onUpdate(ctx) { |
90 | | - // Toggle running state when Spacebar is pressed (fires once per press) |
91 | 99 | if (ctx.isKeyPressed(Keyboard.KEY_SPACE)) { |
92 | 100 | isRunning = !isRunning; |
93 | 101 | } |
94 | 102 |
|
95 | | - // Reset the animation |
96 | 103 | if (ctx.isKeyPressed(Keyboard.KEY_R)) { |
97 | 104 | time = 0; |
98 | 105 | } |
99 | 106 |
|
100 | | - // Advance simulation time only while running (assuming ~60 FPS) |
101 | 107 | if (isRunning) { |
102 | 108 | time += 1 / 60; |
103 | 109 | } |
104 | 110 | }, |
105 | 111 |
|
106 | 112 | onDraw(render) { |
107 | | - |
108 | 113 | render.clearBackground(Palette.BLACK); |
109 | 114 |
|
110 | | - // Solar Simulator |
111 | 115 | render.withLayer2D((ctx) => { |
112 | 116 | ctx.drawFPS(fpsPos); |
113 | 117 |
|
114 | | - // 1. Draw orbital paths for each planet (dotted circles) |
115 | | - planets.forEach((planet) => { |
116 | | - const totalDots = 64; // Number of dots along the orbit path |
117 | | - for (let i = 0; i < totalDots; i++) { |
118 | | - const dotAngle = (i / totalDots) * Math.PI * 2; |
119 | | - const dotPos = new Vector2( |
120 | | - sunPos.x + Math.cos(dotAngle) * planet.radius, |
121 | | - sunPos.y + Math.sin(dotAngle) * planet.radius |
122 | | - ); |
123 | | - |
124 | | - ctx.shapes.drawCircle(dotPos, 1.5, { |
125 | | - color: Palette.GRAY |
| 118 | + // 2. Draw the starfield |
| 119 | + if (enable_starfield) { |
| 120 | + stars.forEach((star) => { |
| 121 | + ctx.shapes.drawCircle(star.pos, star.size, { |
| 122 | + color: star.color |
126 | 123 | }); |
127 | | - } |
128 | | - }); |
| 124 | + }); |
| 125 | + } |
| 126 | + |
| 127 | + // Draw orbital paths |
| 128 | + if (enable_orbit_paths) { |
| 129 | + planets.forEach((planet) => { |
| 130 | + const totalDots = 64; |
| 131 | + for (let i = 0; i < totalDots; i++) { |
| 132 | + const dotAngle = (i / totalDots) * Math.PI * 2; |
| 133 | + const dotPos = new Vector2( |
| 134 | + sunPos.x + Math.cos(dotAngle) * planet.radius, |
| 135 | + sunPos.y + Math.sin(dotAngle) * planet.radius |
| 136 | + ); |
| 137 | + |
| 138 | + ctx.shapes.drawCircle(dotPos, 1.5, { |
| 139 | + color: Palette.GRAY |
| 140 | + }); |
| 141 | + } |
| 142 | + }); |
| 143 | + } |
129 | 144 |
|
130 | | - // 2. Draw the central Sun |
| 145 | + // Draw Sun |
131 | 146 | ctx.shapes.drawCircle(sunPos, 35, { |
132 | 147 | color: Palette.YELLOW |
133 | 148 | }); |
134 | 149 |
|
135 | | - // 3. Render each planet and its moons |
| 150 | + // Draw planets and moons |
136 | 151 | planets.forEach((planet) => { |
137 | | - |
138 | | - // Calculate planet position based on its distinct speed |
139 | 152 | const angle = time * planet.speed; |
140 | 153 | const planetPos = new Vector2( |
141 | 154 | sunPos.x + Math.cos(angle) * planet.radius, |
142 | 155 | sunPos.y + Math.sin(angle) * planet.radius |
143 | 156 | ); |
144 | 157 |
|
145 | | - // Draw planet |
146 | 158 | ctx.shapes.drawCircle(planetPos, planet.size, { |
147 | 159 | color: planet.color |
148 | 160 | }); |
149 | 161 |
|
150 | | - // Draw moons orbiting around the planet |
151 | 162 | planet.moons.forEach((moon) => { |
152 | 163 | const moonAngle = time * moon.speed; |
153 | 164 | const moonPos = new Vector2( |
|
0 commit comments