Search code examples
javaandroidcollision-detection

Issue with 2D collision bitmaps


The bitmap A has a position which is its X/Y position is static, bitmap B is moving bitmap which moves from the bottom of the screen to where ever the user touches on screen. When the moving bitmap reaches the static bitmap however it is not recognized as a collision, the moving bitmap uses float which i round off and the static bitmap uses int. Below is a snippet of code, I want to figure out what is needed to be changed for my bitmaps crossing paths to be considered a collision

                  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

I wonder if my calculations are off, or I am going about this collision the wrong way. Below is my movement code snippet

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

Any tips would be appreciated, thank you.


Solution

  • sorry if this is a dumb answer, but should your check be:

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

    instead? (change the "==" to ">=" ) otherwise you are only checking for when the rock lands EXACTLY on (canvas.getheight -32), rather than checking past it