我目前正在編寫一款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的要求很高。你相信我能申請更好的解決方案嗎?
你爲什麼迭代速度?我得到了大部分代碼,只是沒有這一部分 – Denzil
嘿Denzil,我這樣做,以便我可以檢查激光是否真的擊中了敵人。激光很小,有時直接飛過敵人。我之前使用的方法是每次激光加入距離時檢查是否有碰撞。然而,現在我讓它移動1,然後一次又一次檢查碰撞,直到它達到8(這是它的速度),然後我渲染它。這有任何意義嗎? –
您可以隨時使用您的物體的幾何邊界框/形狀,並計算髮生碰撞的時間/位置。爲了更詳細地瞭解,可能需要更多關於對象形狀的信息。 – Darthfett