Spiral Galaxy Generator - Any improvements?

0 favourites
  • 3 posts
From the Asset Store
Vector illustration of 5 spiral characters with transparency in 3 different colors.
  • So after playing around with this for a few hours I think I've gone as far as I can due to my math & lack of real programming language knowledge.

    Here is the code I think I sort of implemented or not. More likely not.

        public Vector3[] GenerateArm(int numOfStars, int numOfArms, float rotation)
        {
            Vector3[] result = new Vector3[numOfStars];
            Random r = new Random();
    
            float fArmAngle = (float)((360 / numOfArms) % 360);
            float fAngularSpread = 180 / (numOfArms * 2);
    
            for (int i = 0; i < numOfStars; i++)
            {
    
                float fR = (float)r.NextDouble() * 64.0f;
                float fQ = ((float)r.NextDouble() * fAngularSpread) * 1;
                float fK = 1;
    
                float fA = ((float)r.NextDouble() % numOfArms) * fArmAngle;
    
                float fX = fR * (float)Math.Cos((MathHelper.DegreesToRadians(fA + fR * fK + fQ)));
                float fY = fR * (float)Math.Sin((MathHelper.DegreesToRadians(fA + fR * fK + fQ)));
    
                float resultX = (float)(fX * Math.Cos(rotation) - fY * Math.Sin(rotation));
                float resultY = (float)(fY * Math.Cos(rotation) - fX * Math.Sin(rotation));
    
                result[i] = new Vector3(resultX, resultY, 1.0f);
            }
    
            return result;[/code:1i0h2gyi]
    
    I understand some but not all.  I've disabled my implementation of the lines:
    
    float fArmAngle = (float)((360 / numOfArms) % 360);
    float fAngularSpread = 180 / (numOfArms * 2);
    
    When using these lines there isn't a spiral just a nice circle.  Also the code converts degrees to radians so maybe that's where my version trips up.
    
    This is what generates using my sort of hack of the code.  Which doesn't look too bad but when changing some values it either destroys the spiral or makes a nice circle.
    
    [img="https://dl.dropboxusercontent.com/u/1646976/Game%20Media/Misc/galaxy.png"]
    
    [url=https://dl.dropboxusercontent.com/u/1646976/ConstructTutorials/Construct2/Not%20Very%20Good%20Galaxy%20Gen%20V1/index.html]HTML5 Demo[/url]
    
    And the capx.  You'll need the [url=https://www.scirra.com/forum/plugin-canvas_t64239?&hilit=canvas][h2]Canvas Plugin[/h2][/url] to have a play around.
    
    [attachment=0:1i0h2gyi][/attachment:1i0h2gyi]
    I wasn't really planning to use it for anything [i][h2](or am I?)[/h2][/i] other than a learning exercise and I love stuff like this.
    
    If anyone does have a play and improves it just post it here and if possible explain my stupidity in the implementation of code or maths.
  • Wow, that's really neat! I was looking around for something like this recently; my space based shooter would look great with an animated nebula in the background...especially since the working title is 'Nebular Drift'

    Any way to get it moving?

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Wow, that's really neat! I was looking around for something like this recently; my space based shooter would look great with an animated nebula in the background...especially since the working title is 'Nebular Drift'

    Any way to get it moving?

    I have no idea

    If you are mathematically minded/genius you could implement

    Density Wave Theory

    .

    Or you could try to cheat with extra canvases. Create a random number and plot the point/star on a different canvas depending on this number and then rotate the canvases at slightly different speeds. Not sure how it would look but would probably be better than rotating the 1 canvas.

    **EDIT

    Scratch that.

    Link to gif -

    (7mb & recorded at 10fps)

    Multiple canvases destroys the galaxy.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)