创建螺旋星系需要数学指导

时间:2012-10-22 20:36:15

标签: c# math

我正在创造一个由星星/系统块组成的旋转星系。

我已经花了几天时间摆弄这个并且到目前为止:

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.
      }
     }    
   }
}

}

现在它正常工作,它创造了银河系的螺旋臂。但正如你所看到的,我只是将手臂的位置设置在其他手臂上,因为我不能为我的生活弄清楚如何让它们围绕中心旋转,因为我的中心似乎已关闭

我当然拥有一个gnat的数学技能,并且一直在摸索我的方式,有人可以帮助纠正数学并获得他们所属的武器/中心吗?

1 个答案:

答案 0 :(得分:3)

不应该:

float fA = numberArms * (fArmAngle);

float fA = arm * (fArmAngle);

只是说......