Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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;
    position.preTranslate(x,y);
  }
  public void draw(Canvas canvas) {
    canvas.drawBitmap(image, position, null);
  }
}
share|improve this question
    
possible duplicate of per pixel based collision detection –  Anko Jul 29 '13 at 9:47
    
Why the code? It doesn't seem to do any collision detection... –  Anko Jul 29 '13 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.

share|improve this answer
    
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. –  Aekasitt Guruvanich Jul 30 '13 at 20:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.