Spiral galaxy math

I am creating a spinning galaxy made of blocks(for now, just so I can see where the ‘systems’ would be.)

I have been fiddling with this for a few days. Honestly don’t know what I will do with it, just thought it would be fun to do. Learning a bit about rotations and such.

I have this so far:

public int numberArms = 6;
	public int numberStars = 1000;
	public float galaxyRadius = 500f;
	public int spread = 100;
		
	float fHatRandom (float fRange)
	{
		float fArea = 4 * Mathf.Atan (6.0f);
		float fP = fArea * Random.value;
		return Mathf.Tan (fP / 4) * fRange / 6.0f;
	}
	
	float fLineRandom (float fRange)
	{
		float fArea = fRange * fRange / 2;
		float fP = fArea * Random.value;
		return fRange - Mathf.Sqrt (fRange * fRange - 2 * fP);
	}
	// Use this for initialization
	void Start ()
	{
		Random.seed = 100;
		
		int starsPerArm = numberStars / numberArms;
		float fAngularSpread = spread / numberArms;
		float fArmAngle = (360 / numberArms);
		for (int arm = 0; arm < numberArms; arm++)
		{
			for (int i = 0; i < starsPerArm; i++)
			{
				float fR = fHatRandom (galaxyRadius);			
				float fQ = fLineRandom (fAngularSpread);
				float fK = 1;

				float fA = numberArms * (fArmAngle);

				float fX = fR * Mathf.Cos (Mathf.Deg2Rad * (fA + fR * fK + fQ));
				float fY = fR * Mathf.Sin (Mathf.Deg2Rad * (fA + fR * fK + fQ));
			
				Vector3 starPos = new Vector3 (fX, fY, arm*4);
				Collider[] colliders = Physics.OverlapSphere (starPos, 1);
			
				if (colliders.Length == 0)
				{
					GameObject star = GameObject.CreatePrimitive (PrimitiveType.Cube);
					star.transform.position = starPos;
					star.transform.parent = transform;
				} else
				{ 
					i--;//because they overlapped, we try again.
				}
			}	
		}
	}
	// Update is called once per frame
	void Update ()
	{
		transform.Rotate(0,0,-0.1f);
	}

I have a picture of what it produces but unity is being odd about letting me post it.

As it works right now, it creates the spiral arm of the galaxy just fine. But as you can see, I just set the position of the arm to be stacked on the other arms because I cannot for the life of me figure out how to get them to rotate around the center, for that matter my center seems to be off.

I admittedly have the math skills of a gnat and have been fumbling my way through this, can someone help correct the math and get the arms/center where they belong?

If you want to test the code, just make a new class and drop it on an object, nothing else is required.

All the math you can eat:

http://www.kof.zcu.cz/st/dis/schwarzmeier/galaxy_models.html

You might find this helpful: http://wiki.unity3d.com/index.php/Particle_Spiral_Effect

You might want to create arm under one game object, then instantiate that arm (will copy the child stars), but use a different angle for each arm.

But to alter this code, try something like this:

float fA = numberArms * (fArmAngle * arm);

maybe that is what you want…anyway, tnks for the code.

public int numberArms = 6;

public int numberStars = 1000;

public float galaxyRadius = 500f;

public int spread = 100;

float fHatRandom(float fRange)
{
    float fArea = 4 * Mathf.Atan(6.0f);
    float fP = fArea * Random.value;
    return Mathf.Tan(fP / 4) * fRange / 6.0f;
}

float fLineRandom(float fRange)
{
    float fArea = fRange * fRange / 2;
    float fP = fArea * Random.value;
    return fRange - Mathf.Sqrt(fRange * fRange - 2 * fP);
}
// Use this for initialization
void Start()
{
    Random.seed = 100;

    int starsPerArm = numberStars / numberArms;
    float fAngularSpread = spread / numberArms;
    float fArmAngle = (360 / numberArms);
    for (int arm = 0; arm < numberArms; arm++)
    {
        for (int i = 0; i < starsPerArm; i++)
        {
            float fR = fHatRandom(galaxyRadius);
            float fQ = fLineRandom(fAngularSpread);
            float fK = 1;

            //float fA = numberArms * (fArmAngle);
            float fA = arm * fArmAngle;

            float fX = fR * Mathf.Cos(Mathf.Deg2Rad * (fA + fR * fK + fQ));
            float fY = fR * Mathf.Sin(Mathf.Deg2Rad * (fA + fR * fK + fQ));

            Vector3 starPos = new Vector3(fX, fY, arm * 4);
            Collider[] colliders = Physics.OverlapSphere(starPos, 15);

            if (colliders.Length == 0)
            {
                GameObject star = GameObject.CreatePrimitive(PrimitiveType.Cube);
                star.transform.position = starPos;
                star.transform.parent = transform;
                Debug.Log(starPos);
            }
            else
            {
                i--;//because they overlapped, we try again.
            }
        }
    }
}