Advertisement Jump to content
Sign in to follow this  

Collision detection

This topic is 4881 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all! As in my previous post, I have this little game with two people moving... But how can I make so one cant go into the other and the other cant go into the first? I tried letting the first move only if its height(i think) doesnt equal the other's height.. but that also restricted the first from going under or over the other! Here's my code:
#include <stdio.h>
#include <stdlib.h>
#include "SDL.h"
#include "SDL_image.h"

SDL_Surface *screen;

void Draw(int x, int y, SDL_Surface *sprite);

int main(int nArgs, char **args)
    Uint8* keys;
    SDL_Surface *bg;
    bool isRunning;
    int height = 0, width = 0;
    int height2 = 0, width2 = 780;


    screen = SDL_SetVideoMode(800, 600, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
    if (screen == NULL)
        return 0;
    bg = IMG_Load("bg.bmp");
    if (bg == NULL)
        return 0;
    SDL_Surface *mario1;
    mario1 = IMG_Load("mario1.bmp");
    if (mario1 == NULL)
        return 0;
    SDL_Surface *mario2;
    mario2 = IMG_Load("mario2.bmp");
    if (mario2 == NULL)
        return 0;
    SDL_Surface *luigi1;
    luigi1 = IMG_Load("luigi1.bmp");
    if (luigi1 == NULL)
        return 0;
    SDL_Surface *luigi2;
    luigi2 = IMG_Load("luigi2.bmp");
    if (luigi2 == NULL)
        return 0;
    bool MARIO1 = true, LUIGI1 = true;

    isRunning = true;
    SDL_SetColorKey(mario1, SDL_SRCCOLORKEY, SDL_MapRGB(mario1->format, 0, 255, 255));
    SDL_SetColorKey(luigi1, SDL_SRCCOLORKEY, SDL_MapRGB(luigi1->format, 0, 255, 255));
    SDL_SetColorKey(mario2, SDL_SRCCOLORKEY, SDL_MapRGB(mario2->format, 0, 255, 255));
    SDL_SetColorKey(luigi2, SDL_SRCCOLORKEY, SDL_MapRGB(luigi2->format, 0, 255, 255));

        SDL_Event event;

        switch (event.type)
            case SDL_QUIT:
                isRunning = false;
            case SDL_KEYDOWN:
                 switch (event.key.keysym.sym)
                     case SDLK_ESCAPE:
                         isRunning = false;
        keys = SDL_GetKeyState(NULL);
        if (keys[SDLK_UP]) 
            if (height > 0)
                height -= 5;
        if (keys[SDLK_DOWN]) 
            if (height < 380)
                height += 5;
        if (keys[SDLK_LEFT]) 
            if (width > 0)
                width -= 5;
            MARIO1 = false;
        if (keys[SDLK_RIGHT]) 
            if (width < 780)
                width += 5;
            MARIO1 = true;
        if (keys[SDLK_KP8]) 
            if (height2 > 0)
                height2 -= 4;
        if (keys[SDLK_KP5]) 
            if (height2 < 380)
                height2 += 4;
        if (keys[SDLK_KP4]) 
            if (width2 > 0)
                width2 -= 4;
            LUIGI1 = false;
        if (keys[SDLK_KP6]) 
            if (width2 < 780)
                width2 += 4;
            LUIGI1 = true;
        SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
        Draw(0, 0, bg);
        if (MARIO1)
            Draw(width, height, mario1);
            Draw(width, height, mario2);
        if (LUIGI1)
            Draw(width2, height2, luigi1);
            Draw(width2, height2, luigi2);

    return 0;

void Draw(int x, int y, SDL_Surface *sprite)
    SDL_Rect source;
    SDL_Rect dest;

    source.x = 0;
    source.y = 0;
    source.w = sprite->w;
    source.h = sprite->h;

    dest.x = x;
    dest.y = y;
    dest.w = source.w;
    dest.h = source.h;

    SDL_BlitSurface(sprite, &source, screen, &dest);


Any help and examples are apreciated. I'm using Dev-CPP.

Share this post

Link to post
Share on other sites
I didn't look at the code but if you only check the height that's for sure that it won't allow you to go under/over. Also check if it's at the same width.

I don't know how you code is made, but you might want to make a struct for your collision detection. Which contain the minimum width/max of your first char and min/max height aslo, and do it for the second one too.

Then check if the height and width is the same than respectivly min/max height and min/max width.

If yes don't allow him to move. That might not be perfect at the begening but I'm sure you will be able to work on it a bit more, else post again, and if I'm not clear tell me so.

[Edited by - deathwearer on September 5, 2005 3:50:41 AM]

Share this post

Link to post
Share on other sites
If you look at the source code to evoInvaders 0.1, I've implemented some basic collision testing for my entities. I can't remember where it is, I believe it's in ent_entitiy.c - it's a simple rect-collision test and should help you along.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!