how to make a simple collision detection of bitmaps in Android

Posted by Dritan Berna on Stack Overflow See other posts from Stack Overflow or by Dritan Berna
Published on 2013-06-30T16:19:10Z Indexed on 2013/06/30 16:20 UTC
Read the original article Hit count: 229

I already have a code with collision but it has check for winners and ontouch method that I don't really need because my bitmaps are moving itself and I just want them to collide if they overlap.

private boolean checkCollision(Grafika first, Grafika second) {
        boolean retValue = false;

        int width = first.getBitmap().getWidth();
        int height = first.getBitmap().getHeight();

        int x1start = first.getCoordinates().getX();

        int x1end = x1start + width;

        int y1start = first.getCoordinates().getY();

        int y1end = y1start + height;

        int x2start = second.getCoordinates().getX();

        int x2end = x2start + width;

        int y2start = second.getCoordinates().getY();

        int y2end = y2start + height;

        if ((x2start >= x1start && x2start <= x1end) || (x2end >= x1start && x2end <= x1end)) {


            if ((y2start >= y1start && y2start <= y1end) || (y2end >= y1start && y2end <= y1end)) {
                retValue = true;
            }
        }
        return retValue;
    }

© Stack Overflow or respective owner

Related posts about android

Related posts about collision-detection