Graphics‎ > ‎2D‎ > ‎

Sprite collision

So you can pickup that mushroom

To check for collision between two sprites A & B.

Bounding Box
Consider each sprite as a box and check if they are one the side of each other in any axis. If all of these checks are false then the bounding boxes overlap.

bool canCollide(A, B)
{
    if(A.min.y > B.max.y)    return false;    // A Above B
    if(B.min.y > A.max.y)    return false;    // B above A

    if(A.min.x > B.max.x)    return false;    // A right of B
    if(B.min.x > A.max.x)    return false;    // B right of A

    return true;
}

Pixel Collision
Pixel collision is done by getting the areas of the two sprites that overlap. Then check each pixel in each sprite until two are found that are not transparent. If so return a collision. Smaller faster objects, like bullets can be treated as a point when tested against larger sprites (a player ship).

Reference



Comments