2012-05-16 76 views
0

我目前正在編寫一款android遊戲,並且正在處理快速碰撞檢測。 我已經想出了一個解決方案,但我想知道最好的方法來做到這一點。快速碰撞檢測

我的解決方案:如果我們有一個移動30個單位的遊戲對象,我們可以直接通過另一個遊戲對象。所以當我更新時,我將遊戲對象迭代1個單位並運行碰撞檢測,直到達到我想要的速度,然後渲染。

這是一個遊戲對象,它檢查玩家的激光或玩家本身是否與其發生碰撞。

public void update(PlayerDroid[] holderPlayerDroid) { 
      // Update the location 
      //y = y + velocity; 
      //stupidBadDroidPositionShape.setLocation(this.x, this.y); 

      // Updates regarding interactions with the enemy out of the StupidBadDroids perspective, which is the PlayeDroid 
      for(int numberOfPlayerDroid = 0; numberOfPlayerDroid < holderPlayerDroid.length; numberOfPlayerDroid++) { 
       // Check if the StupidBadDroid got hit 
       for(int iterations = 0; iterations < velocity; iterations++) { 
        y = y + 1; 
        stupidBadDroidPositionShape.setLocation(this.x, this.y); 
        // Check if StupidBadDroid collides with the enemy (which is the player) 
        if(Physics.shapeInShape(holderPlayerDroid[numberOfPlayerDroid].getPlayerPositionShape(), getPlayerPositionShape())) { 
         isDead = true; 
        } 
        for(int i = 0; i < holderPlayerDroid[numberOfPlayerDroid].amountOfVisibleLasers; i++) { 
         if(holderPlayerDroid[numberOfPlayerDroid].holderLaser[i].isDisposed() == false) { 
          if(Physics.shapeInShape(holderPlayerDroid[numberOfPlayerDroid].holderLaser[i].getLaserPositionShape(), getPlayerPositionShape())) { 
           isDead = true; 
           holderPlayerDroid[numberOfPlayerDroid].holderLaser[i].dispose(); 
          } 
         } 

        } 
       } 



      } 

    } 

這種方式對CPU的要求很高。你相信我能申請更好的解決方案嗎?

+0

你爲什麼迭代速度?我得到了大部分代碼,只是沒有這一部分 – Denzil

+0

嘿Denzil,我這樣做,以便我可以檢查激光是否真的擊中了敵人。激光很小,有時直接飛過敵人。我之前使用的方法是每次激光加入距離時檢查是否有碰撞。然而,現在我讓它移動1,然後一次又一次檢查碰撞,直到它達到8(這是它的速度),然後我渲染它。這有任何意義嗎? –

+1

您可以隨時使用您的物體的幾何邊界框/形狀,並計算髮生碰撞的時間/位置。爲了更詳細地瞭解,可能需要更多關於對象形狀的信息。 – Darthfett

回答

0

您正在描述隧道,並且正在嘗試執行連續的碰撞檢測。你的方法是CPU密集型的,因爲你試圖暴力破解解決方案。

您要的保真度越高,解決方案的技術就越多。如果你不需要太多的保真度,你可以假設物體在每一幀中的路徑是線性的,並且「擴大」你的點擊框來覆蓋物體在幀中移動的整個距離。因此,例如,您可以不將每個點一次移動一個離散的距離,而只需將這些點擴展到一個線段並查看它們是否相交。儘管如此,你的點擊框不會是點,所以只需按照路徑長度「拉伸」即可。這是一個非常低廉的解決方案 - 如果發生同一對象上的多個碰撞,您不會總是選擇發生「第一次」的碰撞。

對於非常複雜的解決方案,嘗試 -

http://www.bulletphysics.org/mediawiki-1.5.8/index.php/Collision_Detection_and_Physics_FAQ