Per Pixel Collision - Code explanation

前端 未结 3 847
死守一世寂寞
死守一世寂寞 2021-01-14 19:37

I\'m currently trying to understand per pixel collision detection.

This is the code I don\'t understand:

static bool IntersectPixels(Rectangle rectan         


        
3条回答
  •  甜味超标
    2021-01-14 20:36

    it's not that hard (in this case) - you give the algorithm the two bounding-boxes of your objects (so the hole object is inside this box), and a array with color-information for them. Tha algorithm assumes that a point belongs to the object IFF it is not transparent - this is important.

    The first step is to calculate the intersecting rectangle - if you intersect two rectangles that have sides parallel to the axes like in this case - you will get a rectangle again or an empty set.

    The next step is to iterate in this intersecting rectangle for all (x,y) -coordinates insiede - first y, then x -so you get your normal first x, then y inside, but this is minor point and not important.

    Then finally the algorithm gets the color for object A and B at the current pixel (x,y) - if both colors are NOT transparent then the pixel is in both objects and the objects have to intersect at this point - so the algorithm terminates with "YES they intersect"

    If all pixels in the intersection of the bounding boxes where checked and no common (e.g. not transparent) pixel was found the object don't intersect and so the algorithm terminates with "NO they don't intersect"

    I hope this helps.

提交回复
热议问题