Thread: Collisions does not take affect on the corners

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    151

    Collisions does not take affect on the corners

    When the block contacts a corner it goes through the side (not the top or bottom) but everywhere else it works just fine, any ideas?

    here is my code
    Code:
    #include <SDL/SDL.h>
    #include <SDL/SDL_image.h>
    #include <string>
    
    using namespace std;
    
    SDL_Surface *screen = NULL;
    SDL_Surface *block = NULL;
    SDL_Surface *wall = NULL;
    
    bool quit = false;
    
    SDL_Event event;
    //block coordinates
    int x = 0;
    int y = 0;
    int w = x + 32;
    int h = y +32;
    
    int right;
    int left;
    int up;
    int down;
    
    int collision(){
        SDL_Rect blockColl;
        blockColl.x = x;
        blockColl.y = y;
        blockColl.w = w;
        blockColl.h = h;
    
        if(blockColl.y == 0){
            return 1;
        }
        if(blockColl.h == 480){
            return 2;
        }
        if(blockColl.w == 320){
            return 3;
        }
        if(blockColl.x == 0){
            return 4;
        }
    
        return 0;
    }
    
    void handle_events(){
        if(SDL_PollEvent(&event)){
                if(event.type == SDL_QUIT){
                    quit = true;
                }
                if(event.type == SDL_KEYDOWN){
                    switch(event.key.keysym.sym){
                        case SDLK_UP: //
                            up = -1;
                             break;
                        case SDLK_DOWN://
                            down = 1;
                            break;
                        case SDLK_RIGHT://
                             right = 1;
                             break;
                        case SDLK_LEFT://
                             left = -1;
                             break;
                        case SDLK_ESCAPE: quit = true; break;
                        default: break;
                    }
            }
            if(event.type == SDL_KEYUP){
                switch(event.key.keysym.sym){
                    case SDLK_UP: up = 0; break;
                    case SDLK_DOWN: down = 0; break;
                    case SDLK_RIGHT: right = 0; break;
                    case SDLK_LEFT: left = 0; break;
                    default: break;
                }
            }
    
        }
    }
    
    
    void apply_surface(int X, int Y, SDL_Surface *source, SDL_Surface *destination){
        SDL_Rect pos;
        pos.x = X;
        pos.y = Y;
    
        SDL_BlitSurface(source, NULL, destination, &pos);
    }
    
    SDL_Surface *load_image(string filename){
        SDL_Surface *image;
    
        image = IMG_Load(filename.c_str());
    
        return image;
    }
    
    bool init(){
        if(SDL_Init(SDL_INIT_EVERYTHING) == -1){
            return false;
        }
    
        screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
    
        if(screen == NULL){
            return false;
        }
    
        SDL_WM_SetCaption("Test", NULL);
    
        return true;
    }
    
    int main( int argc, char* args[] )
    {
        if(init() == false){
            return 1;
        }
    
        block = load_image("block.png");
            wall = load_image("wall.png");
    
        if(block == NULL){
            return 1;
        }
        if(wall == NULL){
            return 1;
        }
    
        while(quit == false){
    
            handle_events();
            w = x + 32;
            h = y + 32;
    
            if(collision() != 1){
                y += up;
            }
            if(collision() != 2){
                y += down;
            }
            if(collision() != 3){
                x += right;
            }
            if(collision() != 4){
                x += left;
            }
    
            //make screen color
            SDL_FillRect(screen, &screen->clip_rect, SDL_MapRGB(screen->format, 0x12, 0x24, 0x48));
    
            apply_surface(x, y, block, screen);
            apply_surface(320, 0, wall, screen);
    
            SDL_Delay(1000/100);
    
            SDL_Flip(screen);
        }
    
        //Quit SDL
        SDL_Quit();
    
        return 0;
    }
    thank you.

  2. #2
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    Code:
        if(blockColl.y == 0){
            return 1;
        }
        if(blockColl.h == 480){
            return 2;
        }
        if(blockColl.w == 320){
            return 3;
        }
        if(blockColl.x == 0){
            return 4;
        }
    Code:
            if(collision() != 1){
                y += up;
            }
            if(collision() != 2){
                y += down;
            }
            if(collision() != 3){
                x += right;
            }
            if(collision() != 4){
                x += left;
            }
    What happens when both x and y are negative (which would be the case when attempting to leave via a corner)? Or when x+32 and y+32 are bigger than their limits?

    [SPOILER]
    The function will only report one of the cases - in this case it will return 1 for (y==0) but it won't get to the check for (x==0) since it already returned, and so your program goes merrily on its way along in the negative x direction.
    [/SPOILER]
    Consider this post signed

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    151
    Thanks!

    and I like your post signature

  4. #4
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Testing for collision based on position at time T will always be imprecise and prone to failure. Instead you should test if a collision happened between time T and T2.

  5. #5
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    and I like your post signature
    Aw, shucks, I'm flattered.
    Consider this post signed

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 11-20-2007, 09:35 AM
  2. EnableMenuItem has no affect
    By Gerread in forum Windows Programming
    Replies: 3
    Last Post: 04-28-2007, 02:47 AM
  3. Hwang Woo-Suk is a douchebag - how does this affect stem cell research?
    By Govtcheez in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 01-12-2006, 08:09 AM
  4. Help with collisions.
    By Unregistered in forum Game Programming
    Replies: 2
    Last Post: 06-25-2002, 01:00 PM
  5. Collisions help(PLEASE!!! IT'S NOT HARD!!!)
    By SyntaxBubble in forum Windows Programming
    Replies: 5
    Last Post: 11-28-2001, 06:19 PM