Android Bitmap: Collision Detecting
- by Aekasitt Guruvanich
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);
}
}