Search code examples
androidbuttonbitmapcollisionrect

What should i use to do a collision test it?


I am developing a game. I would probably need to do some math in it. I have a character made out of about 5-6 bitmaps together. I want to check its collisions. For example if he hits a bullet, hit platform and all of this stuff. I also want to check if a bitmap is being pressed(like a button). I heard that a rect is good for this but I'm not sure how to use it. can anyone explain how to use the rect for it or if you have a better or easier idea. Thanks!


Solution

  • Never mind, i found out that the rect class has contains option. for example:

        Rect r = new Rect();
        r.set(left, top, right, bottom);
        if (r.contains(x, y)){
            // this is where it will happen if you touch it
        }
        if (r.contains(r2)){
            //what happens if it collides with rect number 2
        }
    //or check intersect        
    
            if(r2.intersect(r)){
    //what happens if it collides with rect number 2
    }