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; ...

Get SDL Game Development now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.