How to do pixel-perfect collision detection of two partially transparent images

前端 未结 3 1626
被撕碎了的回忆
被撕碎了的回忆 2020-12-20 07:44

So basically I to make so that when the two characters on the screen touch and someone presses a button it will take away their health. The only thing I don\'t know how to d

3条回答
  •  我在风中等你
    2020-12-20 08:19

    You could do this in two steps.

    1. Check the bounding circles overlapping.

    2. If the circles overlap, check the (more precise) bounding rectangles overlapping.

    If you have two images, then, the center of each of them equals (x + w/2), (y + h/2). And the radius of each of them equals sqrt((w/2)^2 + (h/2)^2).

    And two circles overlap when the distance between them (sqrt((x1-x2)^2 + (y1-y2)^2)) less than max(radius1, radius2).

    The bounding rectangles collision detection is intuitive, but computationally may be harder (may influence on a large number of objects).

提交回复
热议问题