Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Procedurally Generated Galaxy Spirals

Discussion in 'Scripting' started by Noxbuds, Jul 10, 2015.

  1. Noxbuds

    Noxbuds

    Joined:
    Mar 17, 2015
    Posts:
    57
    So I am making a space game. It will be kinda like Elite: Dangerous or No Man's Sky. So far, it generates a "galaxy" - it chooses arm count, how much it spirals and the length of the arms with my pseudo-random number generator (seeded with an array). At this level, it just places cloud billboards across the arms.

    It does all of that EXCEPT spiralling. Here is the code for making the spirals and placing them in an array:
    Code (CSharp):
    1. for (int i = 0; i < galaxy.armcount; i++) {
    2.            
    3.             c_x = 0;
    4.             c_z = 0;
    5.            
    6.             c_rotation += galaxy.armcount/swirl_value;
    7.            
    8.             for (int j = 0; j < c_armlength; j++) {
    9.                
    10.                 c_x += (int)(Mathd.Cos (c_rotation) * 1.1);
    11.                 c_z += (int)(Mathd.Sin (c_rotation) * 1.1);
    12.                
    13.                 galaxy.arm_waypoints[i,j] = new Vector3d (c_x+x_offset,y_offset,c_z+z_offset);
    14.                
    15.                 c_rotation += (galaxy.armcount/c_armlength)*swirl_value;
    16.                
    17.             }
    18.            
    19.         }
    This is what it produces:
    upload_2015-7-10_21-23-4.png

    By the way, I did this earlier as a prototype in Scratch: https://scratch.mit.edu/projects/70031266/

    I practically copied the code from my Scratch project.
    Is there something I'm not doing right?
     
  2. eisenpony

    eisenpony

    Joined:
    May 8, 2015
    Posts:
    971
    There could be a few similar problems but this one stands out:
    Code (CSharp):
    1. c_x += (int)(Mathd.Cos (c_rotation) * 1.1);
    2. c_z += (int)(Mathd.Sin (c_rotation) * 1.1);
    Not 100% sure on the implementation of Mathd.Cos / Sin but I will assume they have similar outputs to Math.Cos / Sin.

    Mathd.Cos(x) is a continuous function with a co-domain between 0 and 1
    Mathd.Cos(x) * 1.1 is a continuous function with co-domain between 0 and 1.1
    (int)(Mathd.Cos(x) * 1.1) is a discrete function with possible results 0 or 1

    This is because (int) casts the result of the expression into an integer, effectively truncating the fractional components.
    (int)x = 0 for 0 < x < 1
    (int)x = 1 for 1 < x < 2
     
  3. William_Lee_Sims

    William_Lee_Sims

    Joined:
    Oct 11, 2014
    Posts:
    40
    There's two things going on (I think).

    I think Scratch's sine and cosine take degrees. Unity takes radians. You'll only need to change your lines to something like "Mathf.Cos( c_rotation * Mathf.Deg2Rad )".

    The other thing is the "c_x" and "c_z" should probably be kept as floating point values and not integers. Especially since you are accumulating in them. Don't cast the result of the sines and cosines to integers, either.

    Let me know if that works!
     
  4. Noxbuds

    Noxbuds

    Joined:
    Mar 17, 2015
    Posts:
    57
    OK. Will do, thanks for the help ;)

    Mathd is exactly the same as Mathf, but with doubles.
     
  5. Noxbuds

    Noxbuds

    Joined:
    Mar 17, 2015
    Posts:
    57
    I tried making them floats, removing the *1.1 and nothing much different is happening (apart from it being more compact, hooray):
    upload_2015-7-11_12-33-18.png
     
    Last edited: Jul 11, 2015
  6. Noxbuds

    Noxbuds

    Joined:
    Mar 17, 2015
    Posts:
    57
    So for the Sin/Cos inputs I tried multiplying swirl_value by (j/10) and here's how it turned out: upload_2015-7-13_16-24-58.png
     
  7. Noxbuds

    Noxbuds

    Joined:
    Mar 17, 2015
    Posts:
    57
    HOORAY! IT WORKS!
    The answer was so simple I want to kick myself. I just had to change the rotation line to this:
    Code (CSharp):
    1. c_rotation += swirl_value;