我試圖生成一組點(由Vector
結構表示),粗略地模擬一個螺旋星系。星系生成算法
我一直在玩的C#代碼如下;但我似乎只能讓它產生一個單一的「手臂」的星系。
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;
}
看起來你需要根據numOfArms第二回路和offseting由臂之間的角距離的臂角度。然後將你的內部迭代循環改爲numOfStars/numOfArms。 –
這不屬於gamedev嗎? –
另外:http://stackoverflow.com/questions/348321/mathematical-question-procedural-generation-of-a-galaxy –