Bitmap collision detection on SurfaceView canvas in Android

On Android, I use SurfaceView to display a simple 2D game. Bitmaps (.png) with alpha (representing game objects) are drawn on canvas.

Now I would like to make a simple but accurate collision detection. Checking if these bitmaps overlap is pretty simple.

But how can I check for conflicts when these bitmaps have transparent areas? My task is to determine whether two balls collide or not. They fill the entire raster map in width and height, but, as in all four edges, there are transparent areas, of course, since this is a circle in a square.

What is the easiest way to detect collisions there, only if the balls really collide, and not their surrounding raster frame?

Should I store the coordinates of as many points as possible on the contour of the ball? Or can Android “ignore” the alpha channel when checking for conflicts?

+2
source share
3 answers

If this is a spherical collision, you can perform analytic collision detection - it will be much faster than pixel detection. You only need to have two ball centers (x1, y1) and (x2, y2) and a radius r1 for the first ball and r2 for the second. If the distance between the centers of the ball is less than or equal to the sum of the radius, then the balls collide:

    colide = sqrt((x1-x2)^2+(y1-y2)^2)<=r1+r2

but a slightly faster way is to compare the square of this value:

   colide =  (x1-x2)^2+(y1-y2)^2<=(r1+r2)^2
+7

, , , Path s.

, , :

Path path1 = new Path();
path1.addCircle(10, 10, 4, Path.Direction.CW);
Path path2 = new Path();
path2.addCircle(15, 15, 8, Path.Direction.CW);

Region region1 = new Region();
region1.setPath(path1, clip);
Region region2 = new Region();
region2.setPath(path2, clip);

if (!region1.quickReject(region2) && region1.op(region2, Region.Op.INTERSECT)) {
    // Collision!
}

, , drawPath(). transform() .

+10

, AndEngine, . , SurfaceView. : Pixel AndEngine.

+2

All Articles