Scripting Help | Galaxy Generator *I have no script to show publicaly

Hey there,
I want to make a “Galaxy Generator” but the density system* doesn’t work.
So I want to know how it works lets say I want to put a number between 1 it’s going to be big. But if I want to put a number further from one, it’s going to be smaller and smaller. How do I do that? So if I resume, “ ok so there is a function called getstarheight,this has an input parameter and its the distance of the star from center,so now with that ill get a height,more farther distance will give me a small number for height and less distance will give big number for height and the max number it can is 4.4” Sorry for the grammar. Thanks for reading!

*Density system = density is where near the galaxy center there are more stars and the edge of the galaxy is less stars.

1 Like

First you talk about star density, but then you also talk about height?

Anyway, you can scale your math.random output by a function that depends on the distance from the center and that has the behavior you want, i.e. starting at some fixed value when d=0, and approaching zero as d goes to infinity?

Lots of functions do that, like c * e^(-x) where c is the value at the center and e is Euler’s number. You can make it a bit more useful by scaling the input to allow you to also control where the function will equal 1:

c * e^(x * logn(1/c)/a)

where a is the distance where you want the function to equal 1:

In lua code that would look like this:

function falloffFunction(distance, valueAtCenter, oneAtDistance)
    local scaledDistance = distance * math.log(1/valueAtCenter) / oneAtDistance
    return valueAtCenter * math.exp(scaledDistance)
end

function density(point)
    local distance = point.Magnitude
    return falloffFunction(distance, CENTER_DENSITY, DENSITY_ONE_DISTANCE) 
end
2 Likes