Thread: IntersectRect()

  1. #1

    Question IntersectRect()

    I want to use the WinAPI function IntersectRect() for collision detection in my game so can anyone tell me how to go about using this function.

    i have to imaes loaded and two rects set to the exact position and size as the images and i want to be able to tell if they collide and if they do i want to havethe xpos of image1 be reduced by 100.

    Code:
    #include "cozSprite.h"
    #include <windows.h>
    cozSprite spaceship; //sets up object of class cozSprite
    SDL_Surface *screen;
     class image1
    {
     public:
      int width;
      int height;
      int xpos;
      int ypos;
    
      }image1;
    
    class image2
    {
     public:
      int width;
      int height;
      int xpos;
      int ypos;
    }image2;
    
    int main(int argc, char *argv[])
    {
      Uint8* keys;
      /*-----------------------------------------------------------------*/
      // image1
      image1.width  = 201;
      image1.height = 199;
      image1.xpos   = 0;
      image1.ypos   = 0;
    
      // image2
      image2.width  = 201;
      image2.height = 201;
      image2.xpos  = 600;
      image2.ypos  = 0;
    
     /*-----------------------------------------------------------------*/
      if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 ) {
        printf("Unable to init SDL: %s\n", SDL_GetError());
        exit(1); }
      atexit(SDL_Quit);
    
      screen=SDL_SetVideoMode(640,480,32,SDL_HWSURFACE|SDL_DOUBLEBUF);
      if ( screen == NULL ) {
        printf("Unable to set 640x480 video: %s\n", SDL_GetError());
        exit(1); }
    
    
        spaceship.Init(screen,202,201,2); //initializes the sprite
        spaceship.Load("space.bmp", 0,0,0); //loads the sprite
        spaceship.Load("space.bmp", 1,0,199);
    
      int done=0;
    
      while(done == 0)
      {
        SDL_Event event;
    
        while ( SDL_PollEvent(&event) )
        {
          if ( event.type == SDL_QUIT )  {  done = 1;  }
    
          if ( event.type == SDL_KEYDOWN )
          {
            if ( event.key.keysym.sym == SDLK_ESCAPE ) { done = 1; }
          }
        }
          RECT rect1;
          rect1.left   = image1.xpos;
          rect1.top    = image1.xpos + image1.width;
          rect1.right  = image1.xpos - image1.width;
          rect1.bottom = image1.ypos - image1.height;
    
          RECT rect2;
          rect2.left   = image2.xpos;
          rect2.top    = image2.xpos + image2.width;
          rect2.right  = image2.xpos - image2.width;
          rect2.bottom = image2.ypos - image2.height;
    
    
          keys = SDL_GetKeyState(NULL);
        if ( keys[SDLK_UP] ) { image1.ypos -= 1; }
        if ( keys[SDLK_DOWN] ) { image1.ypos += 1; }
        if ( keys[SDLK_LEFT] ) { image1.xpos -= 1; }
        if ( keys[SDLK_RIGHT] ) { image1.xpos += 1; }
    
         spaceship.Draw(0,image1.xpos,image1.ypos); //draws frame in specified postion on the screen
         spaceship.Draw(1,image2.xpos,image2.ypos);
    
         SDL_Flip(screen);
    
    
    
    
        }
    
    
    
      return 0;
    }
    please don't make any comments on the code i jsut threw this tegether but don't know how to use the function o am asking about. Do i need a third RECT?

  2. #2
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    Yes, you'll need a 3rd rect. The 3rd rect will represent the result of an intersection, if it returns TRUE.

    Here's a basic example.
    Code:
    RECT rcOne, rcTwo, rcTemp;
    SetRect(&rcOne,0,0,32,32);
    SetRect(&rcTwo,10,10,42,42);
    if(IntersectRect(&rcTemp,&rcOne,&rcTwo))
    {
       MessageBox(NULL,"They intersect","...",NULL);
    }
    else
    {
       MessageBox(NULL,"They don't intersect","...",NULL);
    }
    Depending on your particular needs the temp RECT could just be used in the check or if you need pixel-perfect collision detection then you could use the size and location of the temp RECT to get pixel data from that part of the two sprites.

  3. #3

    Talking RECT

    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. IntersectRect() not working as expected?
    By dxfoo in forum Windows Programming
    Replies: 1
    Last Post: 09-05-2006, 04:52 PM