Generate starry sky (from seed?)

Hi, I’m working on a game inspired by the looks of Asteroids. The player remains at all times at the center of the screen, and is free to move, but I need a starry sky to make the movement visible.

I’m trying to generate a lot of stars to place in the background, but not something random, because I want that if the player goes back the stars are the same as before.
Also as far as it’s possible I’d like everything to be vector elements, no sprites (but not if that means compromising performance).

I've tried multiple things but the results are underwhelming. (Expand)

For example I tried using pairs of numbers (from Fibonacci sequence) to create and place circles (drawing object) or sprites, but I must hit some limit because it stops adding stars to the scene after less than a second. (Plus Fibonacci is too regular, the stars form a spiral :face_with_spiral_eyes:)

Can you suggest me a newbie-friendly approach? :slight_smile: Thanks!

As far as I know, GDevelop is not (yet) capable of handling vector formats.

My suggestion would be to have several different background images (perhaps 20 or so…however many you feel like creating) and then save them as either different sprites or different animations of a single sprite. You could flip them horizontally/vertically to create even more.

Then, when the player gets close enough to the top/left/right/bottom edge of the window, use the random action to pick one of those sprites/frames and add it there (if one doesn’t already exist).

You definitely won’t be able to import vector images (not truly supported by the rendering engine if I remember correctly, they just get converted to PNG), but you might be able to just use the shape painter and have it draw stars, so long are you’re looking for classic multi-pointed stars.

For random generation in a defined area, look at the random points in an area example.

Then, add a shape painter object.
In the shape painter settings, configure the color as you want (or see below), disable both “Clear each frame” and “draw relative” checkboxes, and then add it to the scene anywhere.

You can then have it generate stars, and even add randomization to it. Here’s a quick mockup from me:


https://game-previews.gdevelop-app.com/1658642903513-920890/index.html

Configured with random colors for the fill and border, as well as random number of points. This example creates one each frame, but you could easily put it into a “Repeat X times” event, and make that the subevent of an event with the “At the beginning of the scene” condition to have it pregenerate your stars.

1 Like

I kept working, I want to share my results.

This is a method which is fast but random: Stars generator (random method)

Click here to see more details

You can press R or touch anywhere to reset the scene (so there’s no need to reload the web page).
You can use WASD to move around, Q to zoom out, E to reset the zoom (no mobile controls, sry).
The time elapsed depends on the hardware, but it’s the quickest I’ve got so far. Also the timer displayed in the debug text is not accurate.

These are the events:

Additionally, this is the debug text in the demo:


(You have to install FPS counter in the Funcions/Behavior)

This method is slower but THE STARS ARE ALWAYS THE SAME: Stars generator (Pi method)

Click here to see more details

You can press R or touch anywhere to reset the scene (so there’s no need to reload the web page).
You can use WASD to move around, Q to zoom out, E to reset the zoom (no mobile controls, sry).
The time elapsed depends on the hardware, and the timer is accurate you see is actual time, so it’s very accurate.

HOW IT WORKS
The location of the stars is stored in a variable (Pi), which is a very long string (100,000 chars/digits).
This string is chopped multiple times, in 2 strings of length 3, which are used as the X and Y coordinates to draw each star.
I chose Pi because it’s irrational and non-repeating.

LIMITS
Gdevelop is slow in loading the long variable. Since each time a star is drawn the long variable gets 6 characters shorter, each time it’s a little bit faster (and you can see it using the debugging text), but I’m sure this is not the fastest approach.

These are the events:


(You have to install FPS counter in the Funcions/Behavior)