Sign in to follow this  

Pixel Perfect Collision

Recommended Posts

unreason    139
I'm trying to do pixel-perfect collision detection for a 2D game, and I'm running into some problems. I'm not sure how to deal with detecting whether objects collided with each other between iterations. If, for example, the two objects are lines one pixel wide, and they are both moving at 4 pixels per move, they might move through each other without ever seeming to touch. How should I deal with this? Also, if possible I'd like to allow this collision detection to work with rotation as well. Is this feasible? If so, how? Thanks!

Share this post

Link to post
Share on other sites
Jiia    592
Aye [smile]

The problem you're having is not specific to pixel perfect collision. It's a problem with absolutely all bounding collision which doesn't provide a safeguard against it. Rectangle, sphere, triangle, they all can pass through each other if only the bounds are checked after each pass of time.

You have a first-check bounding collision before your pixel collision, correct? You make sure the bounding rectangles overlap before checking pixels? I would recommend allowing so much amount of movement per iteration as you loop through an object's path each frame. If movement distance is difficult to determine, use the time passed each frame. Allow the objects to use so much time per iteration through a movement loop.

Another alternative would be to just cap off time itself. If more than so much time passes in one frame, reduce it to that limit. This will cause your game to slow down on slower computers though, rather than skip frames.

Your goal to not allow a single pixel to pass through another is a very tight time frame, unless those pixels are moving pretty slow.

Also, you should know there is an unforeseen situation with pixel perfect collision. Animation itself requires collision response. For example, switching from frame 0 -> 1 will require you to check for collision, and usually (depending on the game) provide response.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this