Search Unity

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

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

  1. zenden_1

    zenden_1

    Joined:
    May 20, 2017
    Posts:
    53
    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
  2. zenden_1

    zenden_1

    Joined:
    May 20, 2017
    Posts:
    53
  3. neoshaman

    neoshaman

    Joined:
    Feb 11, 2011
    Posts:
    6,493
    just use falloff mask that modulate density with distance from the center
     
    zenden_1 likes this.
  4. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    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

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,780
    Yep I would go with radius from the center.
    Further you go, less particles you generate.
     
    zenden_1 likes this.
  6. zenden_1

    zenden_1

    Joined:
    May 20, 2017
    Posts:
    53
    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

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,780
    folloff mask in similar concept to this

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

    zenden_1

    Joined:
    May 20, 2017
    Posts:
    53
    @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.