• entries
23
21
• views
22696

# Random Galaxy Generation in Shipyard

1288 views

[color=rgb(51,51,51)][font=Georgia]

### It's come time in Shipyard to add more sectors to travel to, and to have them generate randomly rather than being hard-coded like the current world. I also wanted the sectors to display on a "galaxy map," where sectors were placed randomly rather than being on a grid. I started with this, creating an algorithm to place points on the map as if they were sectors, even though they didn't function at that time. After a couple hours of tweaking, here are some examples of galaxies it generated:

[/font][/color]

[color=rgb(51,51,51)][font=Georgia][/font][/color]

[color=rgb(51,51,51)][font=Georgia][/font][/color]

[color=rgb(51,51,51)][font=Georgia]

### These weren't produced by the final version, but rather an earlier version that kept a large minimum distance between sectors. The current version has a shorter minimum distance which creates more variation between worlds.

[/font][/color]

[color=rgb(51,51,51)][font=Georgia]

### My algorithm for galaxy generation isn't exactly simple, but I will try to explain it here. First, a starting sector (always called "Galactic Core") is placed in the center of the galaxy. Then, the generator picks a random angle (We'll call it a) and distance d between 12 and 32 pixels, and places a second sector d pixels away from the Galactic Core, at an angle of a. From there, it randomly selects one of the two existing sectors and repeats the process of creating a new sector d pixels away at an angle of a. It also checks to make sure that the new sector is at least 12 pixels away from all of its neighbors, and if it's too close, the new sector is not created. The same is true if the new sector is more than 96 pixels away from the center, to keep them in a roughly circular pattern. The process repeats until there are 50 sectors in the world, each no closer than 12 pixels to any other and no farther than 96 from the center. Finally, any sector within 32 pixels of another sector is considered to be within warp gate range, so a line is drawn between them to denote this.

[/font][/color]

[color=rgb(51,51,51)][font=Georgia]

### Here's an example of something generated by the final version:

[/font][/color]

[color=rgb(51,51,51)][font=Georgia][/font][/color]

[color=rgb(51,51,51)][font=Georgia]

### Once that was done, it was time to code the internal generation of sectors. This was, thankfully, much easier to do. All the internal generation algorithm had to do was place one each of the basic stations (warp gate, item shop, and shipyard), as well as a random number of other objects (planets, item crates, etc.) on a 2D grid. No real fancy stuff, just a random X and Y value for a predefined set of objects. Sectors also generate with a random "style" attribute which dictates which background image to use. This is, again, just a random integer with nothing fancy under the hood.

[/font][/color]

I like the results of your algorithm. I think I will stash this away to play with later.

This is a very interesting take on galaxy map generation. How to see more.

## Create an account

Register a new account