O'Reilly logo

SDL Game Development by Shaun Mitchell

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Dealing with collisions

With so many bullets flying around and having the Enemy objects to check collisions against, it is important that there be a separate class that does this collision checking for us. This way we know where to look if we decide we want to implement a new way of checking for collisions or optimize the current code. The Collision.h file contains a static method that checks for collisions between two SDL_Rect objects:

const static int s_buffer = 4; static bool RectRect(SDL_Rect* A, SDL_Rect* B) { int aHBuf = A->h / s_buffer; int aWBuf = A->w / s_buffer; int bHBuf = B->h / s_buffer; int bWBuf = B->w / s_buffer; // if the bottom of A is less than the top of B - no collision if((A->y + A->h) - aHBuf <= B->y + bHBuf) { return false; ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required