Php代码无法给出最可能的逻辑错误

时间:2013-04-22 12:21:16

标签: php arrays logic collision-detection

在过去的几天里,我一直在努力解决这个问题,因为我的生活似乎找不到问题代码。本质上这个代码生成一个随机数量的具有x和y坐标和半径的对象,然后代码检查以查看新对象是否与任何其他对象发生碰撞,如果它没有那么它会将它添加到主数组中然后返回调用函数。我的问题是,当我加载页面时,所有对象都存在,但有些仍然相互碰撞,我无法弄清楚原因。有谁能看到这个问题?

public function Generate($chunkX, $chunkY) {
    if (!(isset($this->ChunkX) && isset($this->ChunkY) )) {
        $this->ChunkX = $chunkX;
        $this->ChunkY = $chunkY;
    }
    $counter = 0;
    $this->ObjectLocations = array();
    $totalAstroids = $this->GetAstroidNo();


    while ($counter < $totalAstroids) {
        $tempObjectLocations = array();
        //X and Y Chunk Coordinates
        $tempObjectLocations['chunkX'] = $chunkX;
        $tempObjectLocations['chunkY'] = $chunkY;
        //X and Y coordinates for the object.
        $tempObjectLocations['coordX'] = rand(4, 60);
        $tempObjectLocations['coordY'] = rand(4, 60);
        $tempObjectLocations['radius'] = rand(4, 12);
        //Checks if objects already exist in array
        if (count($this->ObjectLocations) > 0) {

            //if the object does not collide with any other object 
            //the location will be added into the database
            if ($this->isColliding($tempObjectLocations) == false) {
                array_push($this->ObjectLocations, $tempObjectLocations);
                $counter += 1;
            }
            // if object is the first created insert into table.
        } else {
            array_push($this->ObjectLocations, $tempObjectLocations);
            $counter += 1;
        }
    }

    return $this->ObjectLocations;
}
public function isColliding($obj1) {
    //Checks if object conflicts with nearby objects
    $a = count($this->ObjectLocations);
    for ($i = 0; $i < $a; $i++) {
        $obj2 = $this->ObjectLocations[$i];

        //Calculates the distance between two points
        $distance = sqrt(($obj1['coordX'] - $obj2['coordX']) ^ 2 + ($obj1['coordY'] - $obj2['coordY']) ^ 2);

        //Checks if the distance between the two objects is 
        //more than the radius of both objects added together
        if ($distance < ($obj1['radius'] + $obj2['radius'] )) {
            return true;
        }
    }
    return false;
}

Json结果

parseResponse([
{
    "chunkX": "1",
    "chunkY": "1",
    "coordX": 54,
    "coordY": 17,
    "radius": 8
},
{
    "chunkX": "1",
    "chunkY": "1",
    "coordX": 41,
    "coordY": 57,
    "radius": 12
},
{
    "chunkX": "1",
    "chunkY": "1",
    "coordX": 42,
    "coordY": 36,
    "radius": 8
},
{
    "chunkX": "1",
    "chunkY": "1",
    "coordX": 40,
    "coordY": 58,
    "radius": 8
},
{
    "chunkX": "1",
    "chunkY": "1",
    "coordX": 25,
    "coordY": 58,
    "radius": 12
},
{
    "chunkX": "1",
    "chunkY": "1",
    "coordX": 57,
    "coordY": 8,
    "radius": 10
},
{
    "chunkX": "1",
    "chunkY": "1",
    "coordX": 46,
    "coordY": 17,
    "radius": 11
},
{
    "chunkX": "1",
    "chunkY": "1",
    "coordX": 42,
    "coordY": 29,
    "radius": 8
},
{
    "chunkX": "1",
    "chunkY": "1",
    "coordX": 18,
    "coordY": 58,
    "radius": 11
},
{
    "chunkX": "1",
    "chunkY": "1",
    "coordX": 59,
    "coordY": 5,
    "radius": 11
},
{
    "chunkX": "1",
    "chunkY": "1",
    "coordX": 15,
    "coordY": 56,
    "radius": 12
}

]);

2 个答案:

答案 0 :(得分:2)

我有一些主张。在isColliding

public function isColliding($obj1) {
    //Checks if object conflicts with nearby objects
    $a = count($this->ObjectLocations);
    for ($i = 0; $i < $a; $i++) {
        $obj2 = $this->ObjectLocations[$i];

        //Calculates the distance between two points
        $distance = sqrt(($obj1['coordX'] - $obj2['coordX']) ^ 2 + ($obj1['coordY'] - $obj2['coordY']) ^ 2);

        //Checks if the distance between the two objects is 
        //more than the radius of both objects added together
        if ($distance < ($obj1['radius'] + $obj2['radius'] )) { // -> Bad idea !
            return true;
        }
    }
    return false;
}

我标记了不好的地方。为什么?因为你把小行星看成是一个质点,但实际上并没有。如果它们的半径之和等于它们之间的距离,它们仍然会相互碰撞。所以这个条件应该是这样的:

if ($distance <= ($obj1['radius'] + $obj2['radius'] )) { // -> Should work :)
                return true;
            }

每个人都看起来但没有看到。有一些基本的错误(我也没有看到这个问题:))。在PHP ^ 运算符是一个XOR运算符而不是幂运算符:) 所以你的脚本的正确表示法是:

public function isColliding($obj1) {
    //Checks if object conflicts with nearby objects
    $a = count($this->ObjectLocations);
    for ($i = 0; $i < $a; $i++) {
        $obj2 = $this->ObjectLocations[$i];

        //Calculates the distance between two points
//correct ^2 to pow function
        $distance = sqrt(pow($obj1['coordX'] - $obj2['coordX'], 2) + pow($obj1['coordY'] - $obj2['coordY'], 2));

        //Checks if the distance between the two objects is 
        //more than the radius of both objects added together
        if ($distance < ($obj1['radius'] + $obj2['radius'] )) { // -> Bad idea !
            return true;
        }
    }
    return false;
}

答案 1 :(得分:0)

也许没有帮助的答案,但是......我认为你的IF / ELSE陈述应该导致两种不同的状态?

        if ($this->isColliding($tempObjectLocations) == false) {
            array_push($this->ObjectLocations, $tempObjectLocations);
            $counter += 1;
        }
        // if object is the first created insert into table.
    } else {
        array_push($this->ObjectLocations, $tempObjectLocations);
        $counter += 1;
    }

我可以看到你将它推入数组是否发生碰撞?