Random Part Generation

Howdy,

I’m looking to create a galaxy by using multiple meshes to represent planets, wondering what the best possible way to generate these planets in a circle style, like;

Galaxy Design

https://gyazo.com/14d06c9a2c67d1768ab86fe8bc0e1e60

or as a real galaxy layout would be?

Would appreciate any help.
Cheers

This method might be a bit complicated but its fairly straight forward, And that method is taking advantage of the math.noise function to create a Perlin noise pattern (for a more in depth explanation check out https://flafla2.github.io/2014/08/09/perlinnoise.html) or (Basics of using Perlin Noise (2D Based)) But what it basically does is, Create a pattern that is very smooth in transitioning without any sharp cuts. With that being said you can use it to create cool smooth patterns which is exactly what everyone would be looking for in terrain creation.
So here is a simple Example on how to utilize it.

for x = 1, 100 do
    for z = 1, 100 do
        local height = (math.noise(x / 20, z / 20) + 2) * 50 -- this will create the pattern with that smooth transition to CFrame the Y value of the Generated parts to
        local p = Instance.new("Part", workspace)
        p.Locked, p.Anchored = true, true
        p.Size = Vector3.new(4, height, 4)
        p.CFrame = CFrame.new(4 * x, height / 2, 4 * z) -- CFraming the Instance to our generated Height 
        wait()
    end
end

Also, Its really fun to mess aound with the values and expect random outcomes everytime, Screw around with it. Hope that helped! (if you need further assistance don’t hesitate to pm me)

5 Likes

A naive approach is to pick a random rotation and distance, and stick a planet there.

I think, however, that you are going to be wanting a minimum distance between planets in order to avoid them clipping inside each other.

Thus, the problem becomes placing points within a circle, with no two points closer than some arbitrary distance to each other. Therefore, generate a random rotation and distance, and check every other point already made.

That however is n! and will scale terribly, so it’s better to divide the area into a grid for a spatial search and just check within the tile or any neighbouring tiles depending on whether you point you generated is at the edge of a tile.

Source code link (Commented)

https://pastebin.com/raw/BM1L95c8

Result

https://i.imgur.com/qlUQtYl.png

2 Likes