1

私はここ数日間、この問題について頭を悩ませてきました。私の人生では、問題コードを見つけることができるようです. 基本的に、このコードは 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
}

]);

4

2 に答える 2

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;
}
于 2013-04-22T12:55:19.030 に答える