二维碰撞位图问题

时间:2012-11-06 18:47:34

标签: java android collision-detection

位图A的位置是X / Y位置是静态的,位图B是移动位图,它从屏幕的底部移动到用户在屏幕上触摸的位置。当移动的位图到达静态位图但是它不被识别为碰撞时,移动的位图使用浮点数,我将其四舍五入,静态位图使用int。下面是一段代码,我想弄清楚我的位图交叉路径需要改变什么才算是碰撞

                  canvas.drawBitmap(rock2, 70, 32, null);
    //this is where it checks to see if the moving bitmaps y coordinate is matched to the Y coordinate of the static bitmap
   if(canvas.getHeight() - Math.round(animY*-1) == (canvas.getHeight() - (canvas.getHeight()-32))){

        Log.d("ALERT", "COLLIDE");

      }
    //the Y value is *-1 because the moving bitmap is starting from the bottom of the screen so this converts the value to positive value

我想知道我的计算是否已关闭,或者我是以错误的方式进行此碰撞。以下是我的动作代码段

               //UP Y ANIMATION
    if(animY*-1 < canvas.getHeight() && !bumpY){

        animY += speedY;

        //IF ROCK IS NOT AT EXTREME LEFT OR EXTREME RIGHT KEEP ON TRACK
        if(animX < (canvas.getWidth()/2) || animX*-1 < (canvas.getWidth()/2) && !bumpX){
            animX += speedX;
            //IF ROCK HITS EDGE LEFT/RIGHT RETURN TO SENDER (INDICATE BUMP)
            if(animX*-1 > (canvas.getWidth()/2) - rock.getWidth()/2 || animX > (canvas.getWidth()/2) - rock.getWidth()/2){
                bumpX = true;
                bumpY = true;
            }
        }

        //IF Y HITS TOP OF SCREEN
        if(animY*-1 > canvas.getHeight()){
            bumpY = true;
        }
    }

    //DOWN Y ANIMATION
    if(animY < 0 && bumpY){ 

        //REVERSE DIRECTION OF Y
         animY  -= speedY;

         //IF ROCK HITS TOP OR SIDE REVERSE X DIRECTION
         if(bumpX || bumpY)
                animX -= speedX;

         //IF AT STARTING POINT
         if(animY > 0){
                bumpY = false;
                bumpX = false;
            }
    }

             //in an ontouch method where the X and Y values are calculated
             case MotionEvent.ACTION_UP:
            finalX = event.getX() - (cross.getWidth() / 2);
            finalY = event.getY() - (cross.getHeight() / 2);
            moveToX = finalX - startX;
            moveToY = finalY - startY;
            speedX = moveToX / 50;
            speedY = moveToY / 50;
            break;

任何提示都将不胜感激,谢谢。

1 个答案:

答案 0 :(得分:0)

抱歉,如果这是一个愚蠢的答案,但你应该检查:

if(canvas.getHeight() - Math.round(animY*-1) >= (canvas.getHeight() - (canvas.getHeight()-32))){

代替? (将“==”更改为“&gt; =”)否则您只检查岩石何时完全落在(canvas.getheight -32)上,而不是检查过去