Tôi đang cố gắng tạo một tập hợp các điểm (được biểu diễn bằng cấu trúc Vector
) gần như mô hình một thiên hà xoắn ốc.Thuật toán thế hệ Galaxy
Mã C# tôi đã chơi với bên dưới; nhưng tôi chỉ có thể làm cho nó tạo ra một 'cánh tay' duy nhất của thiên hà.
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;
}
Hình như bạn cần một vòng lặp thứ hai dựa trên numOfArms và offseting góc cánh tay bởi khoảng cách góc giữa cánh tay. Sau đó thay đổi vòng lặp lặp bên trong của bạn thành numOfStars/numOfArms. –
Điều này không thuộc về gamedev? –
Ngoài ra: http://stackoverflow.com/questions/348321/mathematical-question-procedural-generation-of-a-galaxy –