Image collide

Tagged: ,

This topic contains 3 replies, has 3 voices, and was last updated by  wiebow 2 years, 11 months ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
  • #1419


    Is there anyone out there who has tackled image-image collisions (pixel perfect) and want to share the approach to me?  Or any example code? Thanks!



    There are a few approaches, but most efficient ones take advantage of some first-pass culling which you’re likely already familiar with.


    Bounding box collision, separated into buckets (or organized into quadtrees;  any kind of spacial partitioning will do as long as it’s appropriate for the amount of sprites you have to test) are usually the first culling step.

    Once you’ve confirmed a collision on the bounding box, you do a bitmask comparison on portions of the bounding boxes which overlap.  There are multiple ways to approach this — I believe using the stencil mask is one way you can perform this test “in hardware”, though I don’t know if it would be any quicker than blitting to memory.   You can generate the masks using a shader, or process them manually (I’m not sure how to do this in mojo2, but in mojo1 you’d pull the data using gles11.LoadImageData() and do a simple threshold on the alpha bits to shove it into a bool matrix).

    After you get to this point, you have a couple options:  1.  Test every pixel one at a time, in the standard left-to-right, top-to-bottom order until you hit a “pass” (A And B = True), or 2.  Spot test at a certain pixel resolution (for example, a 1:8 resolution primary pass).  If you take the first option this may be fast enough for your game, but if it isn’t, the second option is better, you can increase the resolution every time you get overlapping masks until it’s as accurate as you want it.


    Simon Armstrong

    I was pretty proud of my BlitzMax collision implementation.  It uses a pretty simple scan converter and allows use of layers that can be used to optimise collision performance further.



    Thanks for the pointers, nobuyuki!

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.