# Formula to make the ends of the arms of a spiral galaxy less dense

Discussion in 'Scripting' started by Zenden1, May 22, 2019.

1. ### Zenden1

Joined:
May 20, 2017
Posts:
50
i wan't to make a spiral galaxy in Unity 3D using C# ( i use a derived logarithmic spiral ) but i wan't to set the end of the arms less dens than the middle.

I want that :

In my game i have this result actually :

Code (CSharp):
1. public int numArms = 5;
2. public float armOffsetMax = 0.5f;
3. public float rotationFactor = 5;
4. public float randomOffsetXY = 0.02f;
5. public float percentStarInCentre = 5;
6.
7.
8.
9. for (int i = 0; i < PixelMath.instanceStarCount; i++)
10.         {
11.             if(type == GalaxyTypes.Spiral)
12.             {
13.                 float distance = Random.value;
14.                 distance = Mathf.Pow (distance, PixelMath.percentStarInCentre / 100) ;
15.
16.
17.                 // Choose an angle between 0 and 2 * PI.
18.                 float angle = Random.value * 2 * Mathf.PI;
19.                 float armOffset = Random.value * PixelMath.armsOffetSetMax;
20.
21.                 armOffset = armOffset - PixelMath.armsOffetSetMax / 2;
22.                 armOffset = armOffset * (1 / distance);
23.
24.                 float squaredArmOffset = Mathf.Pow (armOffset, 2);
25.
26.                 if (armOffset < 0)
27.                     squaredArmOffset = squaredArmOffset * -1;
28.                 armOffset = squaredArmOffset;
29.
30.                 float rotation = distance * PixelMath.rotationFactor;
31.
32.                 // Compute the angle of the arms.
33.                 angle = (int) (angle / armSeparationDistance ) * armSeparationDistance + armOffset + rotation;
34.
35.
36.                 // Convert polar coordinates to 2D cartesian coordinates.
37.                 float starX =  Mathf.Sin (angle) * distance;
38.                 float starZ =  Mathf.Cos (angle) * distance;
39.                 float starY = 0;

Last edited: May 24, 2019

Joined:
May 20, 2017
Posts:
50
up

3. ### neoshaman

Joined:
Feb 11, 2011
Posts:
4,102
just use falloff mask that modulate density with distance from the center

Zenden1 likes this.
4. ### palex-nx

Joined:
Jul 23, 2018
Posts:
539
Right now you're adding one random star in every iteration of your loop. Create a probability function as prob = 1 - (distance / radius) or whatever falloff you want (square, log, etc). Generate a random number from 0 to 1. If number is more than probabilty then skip to next iteration.

5. ### Antypodish

Joined:
Apr 29, 2014
Posts:
4,580
Yep I would go with radius from the center.
Further you go, less particles you generate.

Zenden1 likes this.
6. ### Zenden1

Joined:
May 20, 2017
Posts:
50
Hi, do you have any link or exemple of that ? I never heard about that and i found nothing possible to be that .

I just have the French College level, So....

7. ### Antypodish

Joined:
Apr 29, 2014
Posts:
4,580
folloff mask in similar concept to this

Is just like filter.
But you can do it programatically.

8. ### Zenden1

Joined:
May 20, 2017
Posts:
50
@Antypodish @neoshaman Thank you !
I found what i want after your help and someone other.

My Fallofff function is actually :
Code (CSharp):
1. distance = (Mathf.Pow(distance, 1f / 3f) - 1f) / (1-slopeMod);
Use that and you will get this :

neoshaman likes this.