Android Bitmap: Collision Detecting
        Posted  
        
            by 
                Aekasitt Guruvanich
            
        on Game Development
        
        See other posts from Game Development
        
            or by Aekasitt Guruvanich
        
        
        
        Published on 2013-07-29T08:24:15Z
        Indexed on 
            2013/10/28
            10:02 UTC
        
        
        Read the original article
        Hit count: 250
        
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);
  }
}
© Game Development or respective owner