2

下面的函数 FindEmptySpace() 在一个区域中找到一个随机的空白空间。它通过在该区域内选择随机坐标来执行此操作,检查那里是否已经存在任何东西,然后循环该过程,直到它成功找到一个没有任何东西的位置,然后返回它。

它工作得很好,但我刚刚意识到如果没有可用空间,这可能会以一个永无止境的循环结束。但是该函数如何知道何时没有可用空间并返回错误?

static function FindEmptySpace ()
{
    var sphereRadius = 2.0;
    while ( true )
    {
        var spawnPos = RandomPoint();   // Get random position within level bounds

        if ( !Physics.CheckSphere( spawnPos, sphereRadius ) )   // Check if area is empty
        break;  
    }
    return spawnPos;    // Return empty location
}
4

2 回答 2

4

我按照 Renan 的建议重写了它,现在循环到边界的边缘,如果我们再次到达起点,我们没有找到任何东西并返回 null。

未经测试,但我认为这是一个很好的解决方案。

static function CheckForEmptySpace ()
{
    var bounds = GameController.levelAttributes.bounds;
    var sphereRadius = 2.0;
    var startingPos = Vector3( Random.Range(bounds.xMin, bounds.xMax), 0, Random.Range(bounds.yMin, bounds.yMax) );
    // Loop, until empty adjacent space is found
    var spawnPos = startingPos; 
    while ( true )
    {
        if ( !Physics.CheckSphere( spawnPos, sphereRadius ) )   // Check if area is empty
            return spawnPos;    // Return location
        else
        {
            // Not empty, so gradually move position down. If we hit the boundary edge, move and start again from the opposite edge.
            var shiftAmount = 2;
            spawnPos.z -= shiftAmount;
            if ( spawnPos.z < bounds.yMin )
            {
                spawnPos.z = bounds.yMax;
                spawnPos.x += shiftAmount;
                if ( spawnPos.x > bounds.xMax )
                    spawnPos.x = bounds.xMin;
            }
            // If we reach back to a close radius of the starting point, then we didn't find any empty spots
            var proximity = (spawnPos - startingPos).sqrMagnitude;
            var range = shiftAmount-0.1;    // Slight 0.1 buffer so it ignores our initial proximity to the start point
            if ( proximity < range*range )  // Square the range
            {
                Debug.Log( "An empty location could not be found" );
                return null;
            }
        }
    }
}
于 2013-09-18T18:11:55.107 回答
2

您不应该继续寻找随机点 - 不能保证它会停止。即使这不太可能,例如,算法有可能在切换到其他点之前持续检查完全相同的点数天。

相反,选择一个点(可能是随机的),检查它是否为空......然后检查下一个相邻的点。执行此操作,直到您全部检查完为止。您需要一种方法来避免两次检查同一点。将每个点都视为图中的一个节点,可以让您有效地搜索空点。然后,当功能完成时,您可以 100% 确定是否有空白空间,并相应地引发错误。

于 2013-09-18T14:33:19.883 回答