I am writing an Android game right now and I would need some help in the collision of the Pawns on screen. I figured I could run a for loop on the Player class with all Pawn objects on the screen checking whether or not Width*Height intersects with each other, but is there a more efficient way to do this?

And if you do it this way, many of the transparent pixel inside the rectangular area will also be considered as collision as well. Is there a way to check for collision between Bitmap on a Canvas that disregard transparent pixels? The class for player is below and the Pawn class uses the same method of display.

Class Player {
  private Resources res; // Used for referencing Bitmap from predefined location
  private Bounds bounds; // Class that holds the boundary of the screen
  private Bitmap image;
  private float x, y;
  private Matrix position;
  private int width, height;
  private float velocity_x, velocity_y;

  public Player (Resources resources, Bounds boundary) {
    res = resources;
    bounds = boundary;
    image = BitmapFactory.decodeResource(res, R.drawable.player);
    width = image.getWidth();
    height = image.getHeight();
    position = new Matrix();
    x = bounds.xMax / 2; // Initially puts the Player in the middle of screen
    y = bounds.yMax / 2;
  public void draw(Canvas canvas) {
    canvas.drawBitmap(image, position, null);
  • \$\begingroup\$ possible duplicate of per pixel based collision detection \$\endgroup\$
    – Anko
    Jul 29, 2013 at 9:47
  • 1
    \$\begingroup\$ Why the code? It doesn't seem to do any collision detection... \$\endgroup\$
    – Anko
    Jul 29, 2013 at 9:50

1 Answer 1


More efficient: you could do some sort of spatial partitioning to early-discard some of the collisions, maybe compare the squared distance of their centers and the sum of their squared radii? But if you don't have that many pieces, it's probably not worth it.

More precise: checking every pixel against every other pixel is precise but cumbersome. I've done this before using quadtrees, which you can generate at load- or even compile-time, are a lot faster, and partially solves your problem above. You may find a resolution of 2, 3 or even 5 pixels is precise enough.

  • \$\begingroup\$ Thanks, I decide to use checking of coordinates and just intersections of width and height first, then if the intersection exists, check four corners of this rectangle pixel by pixel since the Pawns in the game all has connected singular piece Bitmaps. \$\endgroup\$ Jul 30, 2013 at 20:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .