Spiral Galaxy Generator - Any improvements?

Get help using Construct 2

» Sat Nov 01, 2014 9:46 pm

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.

Code: Select all
`    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;`

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.

HTML5 Demo

And the capx. You'll need the Canvas Plugin to have a play around.

spiralgalaxytry2.capx

I wasn't really planning to use it for anything (or am 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.
You do not have the required permissions to view the files attached to this post.
B
46
S
23
G
7
Posts: 459
Reputation: 6,763

» Sat Nov 01, 2014 10:04 pm

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?
B
45
S
10
G
11
Posts: 1,106
Reputation: 9,732

» Sat Nov 01, 2014 10:23 pm

TiAm wrote: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.
B
46
S
23
G
7
Posts: 459
Reputation: 6,763