1

現在、私は次のことを行っていますが、それは遅い方法だと確信しています。基本的に、私はグリッドを調べて、a) 星が存在するかどうか、b) 星がある場合はどのようなタイプかを判断します。私がすべきだと思うことは、既知の値 (たとえば current_star_density * 0.25) を使用し、それを使用して一連の点をプロットすることです。私が不確かな部分は、その量をプロットするときに重複を防ぐ方法だと思います.

どんな助けでもありがたく受け取られます。これは、コードの関連部分です。

for (var z = 0; z < gridZ; z++) 
{
    for (var x= 0; x < gridX; x++) 
    {       
        star_chance = Random.value * 1000;   // * 1000      
        if (star_chance <= current_star_density)
        {
            star_class = Random.value * 100;
            if (star_class <=1) // add in the other star types etc
            {
                new_star = Instantiate(prefab_o, Vector3(x * 10 + transform.position.x - 500, 0, z * 10 + transform.position.z - 500), Quaternion.identity);
                new_star.transform.parent = this.transform;
                new_star.name = String.Format("O:{0}.{1}:{2}.{3}",galaxy_X,x,galaxy_Y,z);
            } 
            else if (star_class <=2) 
            {
                new_star = Instantiate(prefab_b, Vector3(x * 10 + transform.position.x - 500, 0, z * 10 + transform.position.z - 500), Quaternion.identity);
                new_star.transform.parent = this.transform;
                new_star.name = String.Format("B:{0}.{1}:{2}.{3}",galaxy_X,x,galaxy_Y,z);
            }
            else if (star_class <=5) 
            {
                new_star = Instantiate(prefab_a, Vector3(x * 10 + transform.position.x - 500, 0, z * 10 + transform.position.z - 500), Quaternion.identity);
                new_star.transform.parent = this.transform;
                new_star.name = String.Format("A:{0}.{1}:{2}.{3}",galaxy_X,x,galaxy_Y,z);
            } 
            else if (star_class <=10) 
            {
                new_star = Instantiate(prefab_f, Vector3(x * 10 + transform.position.x - 500, 0, z * 10 + transform.position.z - 500), Quaternion.identity);
                new_star.transform.parent = this.transform;
                new_star.name = String.Format("F:{0}.{1}:{2}.{3}",galaxy_X,x,galaxy_Y,z);
            } 
            else if (star_class <=20) 
            {
                new_star = Instantiate(prefab_g, Vector3(x * 10 + transform.position.x - 500, 0, z * 10 + transform.position.z - 500), Quaternion.identity);
                new_star.transform.parent = this.transform;
                new_star.name = String.Format("G:{0}.{1}:{2}.{3}",galaxy_X,x,galaxy_Y,z);
            } 
            else if (star_class <=40) 
            {
                new_star = Instantiate(prefab_k, Vector3(x * 10 + transform.position.x - 500, 0, z * 10 + transform.position.z - 500), Quaternion.identity);
                new_star.transform.parent = this.transform;
                new_star.name = String.Format("K:{0}.{1}:{2}.{3}",galaxy_X,x,galaxy_Y,z);
            }
            else if (star_class <=80) 
            {
                new_star = Instantiate(prefab_m, Vector3(x * 10 + transform.position.x - 500, 0, z * 10 + transform.position.z - 500), Quaternion.identity);
                new_star.transform.parent = this.transform;
                new_star.name = String.Format("M:{0}.{1}:{2}.{3}",galaxy_X,x,galaxy_Y,z);
            }
        }   
    }
}
4

1 に答える 1

0

あなたが達成しようとしている最終結果の種類はよくわかりませんが、おそらくこのようなもの(疑似コード)を意味していますか?

star_densityここでは 0 から 1 までの数値で、1 は完全な密度を意味し、0 は何も意味しません。

// We have a fixed amount of stars based on the grid size and density
// You could add a small random multiplier if you wish
for (var i = 0; i < gridX * gridZ * star_density; ++i) {
    // Pick a random position until we find an empty one
    do {
        var x = floor(Random.value * gridX);
        var z = floor(Random.value * gridY);
    } while (grid[z][x].containsStar());
    var new_star = /* Do all the star initialization stuff and randomization */
    grid[z][x] = new_star;
}

これは、グリッドを使用して星の存在を検索できることを前提としています。星を保存する場所はわかりませんでしたが、現在実際のグリッドがなくても、配置アルゴリズムに一時的な配列を使用できます。

これは、星の密度が低い場合 (たとえば、30% 未満 - いくつかの実験が必要な完全に構成された数) でより効率的であるはずですが、衝突が発生する可能性が低い場合、ある時点で現在のものよりも悪化する可能性があります。空きスロットと、1 つではなく 2 つのランダム値を使用します。もう 1 つの欠点は、RNG が安っぽい場合、理論的には大きなグリッドと非常に高密度の無限ループに入る可能性があることです。密度に基づいて使用するアルゴリズムを選択するだけで、最悪の場合のパフォーマンスに対抗できます。

于 2013-02-22T10:40:27.850 に答える