Skip to content

Commit ad124bc

Browse files
example: add starfield background and toggle for orbital paths in simulation
1 parent 86b742a commit ad124bc

1 file changed

Lines changed: 39 additions & 28 deletions

File tree

examples/2d/solar2d.js

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
import { Application, Vector2, Palette, Keyboard } from "vectorjs";
22

3+
const enable_orbit_paths = false;
4+
const enable_starfield = true;
5+
6+
37
const screenWidth = 1200;
48
const screenHeight = 1200;
59
const sunPos = new Vector2(screenWidth / 2, screenHeight / 2);
610
const fpsPos = new Vector2(10, 10);
711

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+
920
const planets = [
1021
{ name: "Mercury", radius: 70, size: 6, speed: 2.5, color: Palette.GRAY, moons: [] },
1122
{ name: "Venus", radius: 110, size: 10, speed: 1.8, color: Palette.ORANGE, moons: [] },
@@ -80,74 +91,74 @@ const planets = [
8091

8192
const app = new Application(screenWidth, screenHeight, "Solar System Simulation");
8293

83-
// Simulation State
8494
let isRunning = true;
8595
let time = 0;
8696

8797
app.run({
88-
8998
onUpdate(ctx) {
90-
// Toggle running state when Spacebar is pressed (fires once per press)
9199
if (ctx.isKeyPressed(Keyboard.KEY_SPACE)) {
92100
isRunning = !isRunning;
93101
}
94102

95-
// Reset the animation
96103
if (ctx.isKeyPressed(Keyboard.KEY_R)) {
97104
time = 0;
98105
}
99106

100-
// Advance simulation time only while running (assuming ~60 FPS)
101107
if (isRunning) {
102108
time += 1 / 60;
103109
}
104110
},
105111

106112
onDraw(render) {
107-
108113
render.clearBackground(Palette.BLACK);
109114

110-
// Solar Simulator
111115
render.withLayer2D((ctx) => {
112116
ctx.drawFPS(fpsPos);
113117

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
126123
});
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+
}
129144

130-
// 2. Draw the central Sun
145+
// Draw Sun
131146
ctx.shapes.drawCircle(sunPos, 35, {
132147
color: Palette.YELLOW
133148
});
134149

135-
// 3. Render each planet and its moons
150+
// Draw planets and moons
136151
planets.forEach((planet) => {
137-
138-
// Calculate planet position based on its distinct speed
139152
const angle = time * planet.speed;
140153
const planetPos = new Vector2(
141154
sunPos.x + Math.cos(angle) * planet.radius,
142155
sunPos.y + Math.sin(angle) * planet.radius
143156
);
144157

145-
// Draw planet
146158
ctx.shapes.drawCircle(planetPos, planet.size, {
147159
color: planet.color
148160
});
149161

150-
// Draw moons orbiting around the planet
151162
planet.moons.forEach((moon) => {
152163
const moonAngle = time * moon.speed;
153164
const moonPos = new Vector2(

0 commit comments

Comments
 (0)