Android Studio:可靠的碰撞检测

时间:2017-09-09 13:31:48

标签: java android collision-detection

如何可靠地检测android上两个矩形之间的碰撞?

我尝试了两种方法,第一次基本碰撞:

// using android.graphics.Rect
//worldRects is an array of Rects containing only one Rect based off mapX and mapY
//playerRect is a stationary Rect in the middle of the screen slightly bigger than player
//sectorArrayX & Y and sector have to do with joystick and moving map accordingly

for (int ir = 0; ir < worldRects.length; ir++) {
  if (!Rect.intersects(playerRect, worldRects[ir])) {
     mapX = mapX + ((speed * (-1 * sectorArrayX[sector])) / fps);
     mapY = mapY + ((speed * (-1 * sectorArrayY[sector])) / fps);
   } 
}

这会导致玩家在碰撞时锁定位置

我的第二次尝试是试图在碰撞时向相反的方向移动玩家,这种方法运作得相当好,但无论我多少次增加&#34;反弹&#34;量。

for (int ir = 0; ir < worldRects.length; ir++) {
  if (!Rect.intersects(playerRect, worldRects[ir])) {
    mapX = mapX + ((speed * (-1 * sectorArrayX[sector])) / fps);
    mapY = mapY + ((speed * (-1 * sectorArrayY[sector])) / fps);
  } else {
    mapX = mapX + (25<-"bounce back amount" * (1 * sectorArrayX[sector])); 
    mapY = mapY + (25 * (1 * sectorArrayY[sector])); 
  }
}

我试图以一种非常有效的方式做到这一点,在一种可靠的方法之后,真的不在乎准确性。提前谢谢!

我还包括两个问题的视频:

问题1: https://youtu.be/iB7hLZpS3hQ

问题2: https://youtu.be/k9oEA2s7e3E

0 个答案:

没有答案