Resolving Pixel Perfect Collisions

I have a pixel-perfect collision detection system that uses a bitmask for loaded sprite images and checks for overlap in the binary digits using a kind of bitwise analysis algorithm.

It works quite nicely, and it only has to run when the cheap bounding box collision code returns a valid collision area.

The bounding box collision code computes the collision area between the two entities’ bounds and returns null if there is no collision.

The problem I have now is how to resolve these collisions. If I’m updating a moving ball 20 units per tick, I don’t want it moving 10-15 units into another entity and being rendered before bouncing or stopping (obviously).

What’s the best method with this kind of pixel-perfect collision to resolve collisions? The best idea I’ve come up with so far is a simple loop to guess and check until you get as close as needed to the collision location, but this seems kind of risky and would most likely scale quite poorly. Any other ideas?

Thanks in advance for your help!

You could try putting your collision detection before rendering in your game loop. That should catch the ball before it’s rendered in the middle of an object. After that all you’d need to do is set the ball’s position to the point of collision and your good. :slight_smile:

The collision checking actually comes after the update, before the rendering. The problem is, the event will fire, but nothing will be done until the next tick. The order really doesn’t matter though. Either it will show collision too early or too late.

The problem is computing that collision point when you aren’t dealing with just boxes or even polygons but bitmaps of loaded sprite images. Remember this is pixel-perfect collision detection. So again, I’m stuck on how to actually GET the point of collision and resolve the position of the colliding entity to that point.

For both entities, find the distance in that entitie’s direction of movement from the point of collision in the bit mask to where the entity ends in the bit mask. Then move entities back in the direction they came by half of the larger amount, or if one is static, move the dynamic one by the full amount.

Check this.