9 Bounding Box Collision Detection


Table of Contents

  1. Introduction
  2. Initializing Allegro
  3. Keyboard Input
  4. Drawing Graphics
  5. Playing Sound
  6. Drawing Text
  7. Regulating FPS
  8. Enumerations and Random Numbers
  9. Bounding Box Collision Detection
  10. Planning the Game
  11. Super Basic Game Structure
  12. Wrapping up


Download entire guide as ODT


Part 9 – Bounding Box Collision Detection

Bounding-box collision detection is just one of many ways to detect collision between objects. It consists of having each collidable object have a region that is “solid”. This can either be the entire picture, but is probably better if it’s a smaller region of the image.

A, B, and C

A, B, and C

The Ayne sprite is 32×32, but the region should probably be from (7, 1) to (37, 47) (image B) or (11, 8) to (34, 47) (image C) if you don’t want things like her hair and the ends of her arms causing collisions.

Bounding Boxes

Bounding Boxes

Left edges = blue,     Right edges = pink,

Top edges = red,       Bottom edges = green


The basic idea for this collision is as such:

Left1 < Right 2      && Right1 > Left2 &&

Top1 < Bottom2    && Bottom1 > Top2

You should probably write a function somewhere in main.cpp or it’s own header file to return a bool for IsCollision, rather than having the function be part of a class. The parameters should be two Rectangles.

The Code

First you need to write a Rectangle struct to hold the collisions, like this:

struct Rectangle
	int x, y, w, h;

This struct will encapsulate your coordinates and dimensions for the character.

Before you write the collision code, you’ll need to make sure your characters and objects in the game have a collision region Rectangle. This way, you only pass the rectangle to the IsCollision function so it can be used with all different types of objects.

Also, you’ll have to write some functions, because if you just pass the collision region coordinates, it will be based at 0,0 and you won’t get the right results. You need to add the collision region to the object’s current coordinates.

In Allegro, you will have to write your own Rectangle struct, but in SDL you can use SDL_Rect. For the following code, it’s assuming you’re using Allegro, but just replace Rectangle with SDL_Rect if you’re using SDL.

#include "Rectangle.h"

class Character		// Incomplete Character class
		Rectangle colRegion;
		Rectangle coordinates;
		Rectangle RegionCoordinates();

Rectangle Character::RegionCoordinates()
	// create a rectangle to pass to the IsCollision function.
	Rectangle temp;
	temp.x = colRegion.x + coordinates.x;
	temp.w = colRegion.w;
	temp.y = colRegion.y + coordinates.y;
	temp.h = colRegion.h;

	return temp;

Here’s how you would implement and call the IsCollision function (again, just replace Rectangle with SDL_Rect for SDL):

// ...includes go here...

bool IsCollision( Rectangle A, Rectangle B );

int main()
	// ...stuff... 
	if (IsCollision(player.RegionCoordinates(), stick.RegionCoordinates()))
		player.AddToScore( 1 );
	// ...more stuff...

bool IsCollision( Rectangle A, Rectangle B )
	if ( 	A.x 		<	B.x + B.w 	&&
		A.x + A.w 	> 	B.x 		&&
		A.y		<	B.y + B.h 	&&
		A.y + A.h	>	B.y )
		return true;
	return false;

© Rachel J. Morris, 2009

Print Friendly