02:05
03 Sep 2010

News

Latest Updates
Recent Comments
Most Popular Downloads

Projects

Blob Wars : Blob And Conquer
Blob Wars : Metal Blob Solid
JGameLaunch
The Legend of Edgar
Project: Starfighter
Random Name Generator
Random Shooter
TANX Squadron
Virus Killer
XMAME GUI

Older Files

List of Older Downloads

Medals

Main & Signup
Recently Awarded
Player List

Games
Blob Wars : Metal Blob Solid
Legend of Edgar
Virus Killer

Online Manuals

Blob Wars : Blob And Conquer
Blob Wars : Metal Blob Solid
Project: Starfighter
The Legend of Edgar
Virus Killer

LBP Beta Code!

Beta Code Giveaway!

Game Tutorials

Overview and Comments

Basic Series
1.01 - Opening a Window
1.02 - Graphics
1.03 - Sound
1.04 - Input and Movement
1.05 - Simple Shooter Pt. 1
1.06 - Simple Shooter Pt. 2
1.07 - Simple Shooter Pt. 3
1.08 - Sprites and Animation
1.09 - Starfields
1.10 - A Basic Game

Intermediate Series
2.01 - Displaying a Tile Based Map
2.02 - Scrolling a Tile Based Map
2.03 - A Tile Based Map Editor
2.04 - Tile Based Map Collision Detection
2.05 - Advanced Animation

Articles

Making of TANX Squadron
Making of Starfighter
Making of Metal Blob Solid
Making of Blob And Conquer

Blob Wars Review #1
Blob Wars Review #2
Blob Wars Review #3

Blob And Conquer Review #1

Interview with Stephen J Sweeney

Gallery

3D Renders

Help and FAQs

Installation and Licensing Help

About

Contact Information

THE HONOUR OF THE KNIGHTS
The new Creative Commons Licensed
novel from Stephen J Sweeney

Visit the official website,
www.battleforthesolarsystem.com
to start reading online!

Buy Now! from Amazon.co.uk

Buy Now! from Book Depository

Basic Tutorials
Basic Game Tutorial #6 - Collision Detection

Collision Detection

 Introduction

Collision detection is a vital part of any game. In this tutorial we will look at 2D collision detection so that you can fire a bullet and make it hit an enemy UFO.

Compile and run tutorial06. You can control the ship by using the arrow keys (not the ones on the numeric pad). Pressing the space key will fire a bullet, which will move to the right from the ship's current position. If the bullet hits one of the UFOs then both it and the UFO will be removed. The bullet will also be removed if it goes past the right edge of the screen.

 An indepth look

Most of the code has not changed very much, so we will only look at the functions that have changed or that are new. We will start will structs.h

We update the Entity structure by adding another variable called type:

typedef struct Entity
{
    int active, type;
    int x, y, thinkTime;
    SDL_Surface *sprite;
    void (*action)(void);
    void (*draw)(void);
} Entity;
The type variable will be used in the collision detection routine, since we don't want bullets to collide against each other.

In main.c, we call two new functions addUFO and doCollisions. These two functions will be looked at later on.

We have made a minor change to bullet.c:

entity[i].x = x;
entity[i].y = y;
entity[i].action = &moveStandardBullet;
entity[i].draw = &drawStandardEntity;
entity[i].sprite = getSprite(BULLET_SPRITE);
entity[i].type = TYPE_BULLET;
We set the Entity type to TYPE_BULLET. This helps us identify the Entity as being a bullet so that we don't try and collide bullets against each other.

The new file ufo.c, contains all the code for dealing with the UFOs. There are only two functions in this file and it is essentially the same as bullet.c:

int i = getFreeEntity();

if (i == -1)
{
	printf("Couldn't get a free slot for a UFO!\n");
	
	return;
}

entity[i].x = x;
entity[i].y = y;
entity[i].action = &moveUFO;
entity[i].draw = &drawStandardEntity;
entity[i].sprite = getSprite(UFO_SPRITE);
entity[i].type = TYPE_ENEMY;
As in the bullet.c, we get search for a free Entity to assign to our UFO and set its functions. The moveUFO function is below, but it is an empty function, essentially meaning that the UFO will do nothing. Finally, we set the Entity type to TYPE_ENEMY.

Now we will look at the collision detection. This function is located in collisions.c. We will start by looking at the actual detection routine itself:

int collision(int x0, int y0, int w0, int h0, int x2, int y2, int w1, int h1)
{
    int x1 = x0 + w0;
    int y1 = y0 + h0;

    int x3 = x2 + w1;
    int y3 = y2 + h1;

    return !(x1<x2 || x3<x0 || y1<y2 || y3<y0);
}
The 2D collision detection routine is box to box, which is not accurate, but it is very fast. There are routines for pixel perfect collision detection, but we will not be looking at these. This function takes 8 parameters, the x, y, width and height of the first box and the x, y, width and height of the second box. Using these parameters we then construct the coordinates of two rectangles and test if they overlap. The function will return 1 if they do and 0 if they don't. We will now look at its use:
void doCollisions()
{
    int i, j;

    /* Check each entity against the rest, skipping over inactive ones */

    for (i=0;i<MAX_ENTITIES;i++)
    {
        if (entity[i].active == 0)
        {
            continue;
        }

        for (j=0;j<MAX_ENTITIES;j++)
        {
            /* Don't collide with yourself, inactive entities or entities of the same type */
            
            if (i == j || entity[j].active == 0 || entity[j].type == entity[i].type)
            {
                continue;
            }
            
            /* Test the collision */

            if (collision(entity[i].x, entity[i].y, entity[i].sprite->w, entity[i].sprite->h,
            entity[j].x, entity[j].y, entity[j].sprite->w, entity[j].sprite->h) == 1)
            {
                /* If a collision occured, remove both Entities */
                
                entity[j].active = 0;
                
                entity[i].active = 0;
                
                break;
            }
        }
    }
}
In doCollisions we run two loops, one inside the other, since we need to check each Entity against all the others. In our outer loop, we first check that the Entity we are indexing is actually active, otherwise we simply pass over it. Then, in our inner loop, we skip over any Entity that is either the same type as the current Entity, is no active or is Entity that we are currently checking. We then run our collision test by passing in the x, y, width and height of both Entities and checking the response. If we get back a value of 1 then we set both the Entities to inactive and break out of the inner loop.

 Conclusion

So now you know how to move a sprite, fire a bullet and make it hit something. In later tutorials we will look at a health system so that when something is hit it doesn't die straight away, but you could always try this yourself. The theory is very simple: add a additional variable to the Entity structure to store the health. When a collison occurs you decrease the health and if it is less than or equal to 0 you remove the Entity.

In the next tutorial we will look at SDL_TTF so that we can display information such as the player's score.

 Downloads

Source Code - tutorial06.tar.gz

 Comments (latest 4)

Jeff
March 7th, 2009 at 10:05

I've got all of the previous tutorials to work properly. This one however gives me a runtime error as soon as it is started. No sprites are loaded, only the window is created. Still haven't been able to figure out why.

 
Steve
March 7th, 2009 at 18:28

Are you using Windows? You should see a file called stdout.txt and/or stderr.txt. There should be some debugging information in there. Could you check for the file and let me know what it says?

 
Jeff
March 7th, 2009 at 22:24

Yes, I am using windows (Visual Studio 2005) but couldn't find either file when I searched my project folder. I have to go to work right now so I won't be able to tinker with it for another 6 hours. I have it setup as a console application. I'm not sure if it will make a difference to change it to a win32 application instead. I'll let you know in a bit. Any further help would be much appreciated.

 
Jeff
March 8th, 2009 at 07:54

Well, I went ahead and compiled tutorial 7 and that worked for some reason without a problem. I guess if any other windows users are having problems with tutorial 6, try number 7. GREAT WEBSITE!!! These tutorials have taught me A LOT!!!

 

View All Comments
(You can also subscribe to the comment RSS feed by clicking here)

 
Name
Homepage
Email
Comments
-- Homepage must start with http://
-- Javascript and Cookies must be enabled to use this form
 

1,991,111 pages served

Copyright © 1996 - 2010 Parallel Realities :: About :: License