2013-04-22 85 views
1

過去幾天我一直在這個問題上絞盡腦汁,對於我的生活似乎可以找到問題代碼。本質上,這段代碼生成一個隨機數量的對象,其中包含一個x和y座標和一個半徑,然後代碼檢查新對象是否與任何其他對象相沖突,如果沒有,那麼它會將它添加到主數組中然後返回調用函數。我的問題是,當我加載頁面時,所有的對象都在那裏,但一些仍然相互碰撞,我不明白爲什麼。任何人都可以看到問題嗎?PHP代碼未能給出corrent結果最有可能的邏輯錯誤

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 
} 

]);

+0

你能與所產生的$這個 - > ObjectLocations一個更新的var_dump你的問題? – 2013-04-22 12:50:48

+0

當然不適合註冊日期 – Ardenexal 2013-04-23 00:08:40

回答

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; 
} 
+0

葉我試過,當我測試仍然有相同的問題 – Ardenexal 2013-04-23 00:07:58

+0

當然比較應該是「<=」不是「<」。 (當然在我看來:))我已經忽略了這個編輯 – Soyale 2013-04-23 11:43:05

+0

@Soyale很好的^運營商。我忽略了完全哈哈:) – 2013-04-23 12:38:30

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; 
    } 

正如我看到你把它推到一個數組中是否碰撞?

+0

這種情況只是防止碰撞檢查,如果數組是空的。他可以以相同的效果放棄它,但這只是一種優化。編輯:但我同意它可以寫得更好。 – 2013-04-22 12:28:57

+0

好的,對不起,但是你在哪裏檢查它是否碰撞 - 不創建那個對象?在我看來,你永遠不會停止在你的Generate方法中創建對象。我的意思是,數組在第一次迭代之後不是空的,然後在第二次迭代時,無論座標是什麼都不會停止創建對象 – 2013-04-22 12:31:13

+0

他正在使用isColliding方法檢查它。 – 2013-04-22 12:33:42