Advertisement
thecplusplusguy

circle collision example

Apr 19th, 2013
347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.95 KB | None | 0 0
  1. //http://www.youtube.com/user/thecplusplusguy
  2. //you need cirlce.bmp 20x20 bmp image containing a 10 pixel radius (20 diameter) circle
  3. //circle collision example
  4. #include <SDL/SDL.h>
  5. #include <iostream>
  6.  
  7. struct Circle
  8. {
  9.     SDL_Rect position;
  10.     SDL_Surface* image;
  11.     int radius;
  12.     Circle(SDL_Surface* img, int x,int y,int radius)
  13.     {
  14.         image=img;
  15.         position.x=x;
  16.         position.y=y;
  17.         this->radius=radius;
  18.     }
  19.    
  20.     void render()
  21.     {
  22.         SDL_BlitSurface(image,NULL,SDL_GetVideoSurface(),&position);
  23.     }
  24.    
  25.     bool intersects(Circle* c)
  26.     {
  27.         int x=c->position.x - position.x;
  28.         int y=c->position.y - position.y;
  29.         int r=radius + c->radius;
  30.         return (x*x + y*y < r*r);   //a^2+b^2=c^2
  31.     }
  32.  
  33.     bool intersects(SDL_Rect* rec)
  34.     {
  35.         //source: http://stackoverflow.com/questions/401847/circle-rectangle-collision-detection-intersection
  36.     int circleDistanceX = abs(position.x - rec->x);
  37.     int circleDistanceY = abs(position.y - rec->y);
  38.  
  39.     if (circleDistanceX > (rec->w/2 + radius)) { return false; }
  40.     if (circleDistanceY > (rec->h/2 + radius)) { return false; }
  41.  
  42.     if (circleDistanceX <= (rec->w/2)) { return true; }
  43.     if (circleDistanceY <= (rec->h/2)) { return true; }
  44.  
  45.     int cornerDistance_sq = ((float)circleDistanceX - rec->w/2)*(circleDistanceX - rec->w/2) + (circleDistanceY - rec->h/2)*(circleDistanceY - rec->h/2);
  46.  
  47.     return (cornerDistance_sq <= (radius*radius));
  48.     }
  49. };
  50.  
  51. int main(int argc, char** argv)
  52. {
  53.     SDL_Init(SDL_INIT_EVERYTHING);
  54.     SDL_Surface* screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
  55.     bool running = true;
  56.     const int FPS = 30;
  57.     Uint32 start;
  58.     bool b[4] = {0,0,0,0};
  59.     SDL_Rect rect;
  60.     rect.x = 10;
  61.     rect.y = 10;
  62.     rect.w = 20;
  63.     rect.h = 20;
  64.     SDL_Surface* circle=SDL_LoadBMP("circle.bmp");
  65.     if(!circle)
  66.     {
  67.         std::cout << "You need circle.bmp" << std::endl;
  68.         return 1;
  69.     }
  70.     Circle circle1(circle,30,30,10);
  71.     Circle circle2(circle,50,50,10);
  72.     SDL_SetColorKey(circle,SDL_SRCCOLORKEY,SDL_MapRGB(screen->format,255,0,255));
  73.     while(running) {
  74.         start = SDL_GetTicks();
  75.         SDL_Event event;
  76.         while(SDL_PollEvent(&event)) {
  77.             switch(event.type) {
  78.                 case SDL_QUIT:
  79.                     running = false;
  80.                     break;
  81.                 case SDL_KEYDOWN:
  82.                     switch(event.key.keysym.sym) {
  83.                         case SDLK_UP:
  84.                             b[0] = 1;
  85.                             break;
  86.                         case SDLK_LEFT:
  87.                             b[1] = 1;
  88.                             break;
  89.                         case SDLK_DOWN:
  90.                             b[2] = 1;
  91.                             break;
  92.                         case SDLK_RIGHT:
  93.                             b[3] = 1;
  94.                             break;
  95.                    
  96.                     }
  97.                     break;
  98.                 case SDL_KEYUP:
  99.                     switch(event.key.keysym.sym) {
  100.                         case SDLK_UP:
  101.                             b[0] = 0;
  102.                             break;
  103.                         case SDLK_LEFT:
  104.                             b[1] = 0;
  105.                             break;
  106.                         case SDLK_DOWN:
  107.                             b[2] = 0;
  108.                             break;
  109.                         case SDLK_RIGHT:
  110.                             b[3] = 0;
  111.                             break;
  112.                    
  113.                     }
  114.                     break;
  115.             }
  116.         }
  117.  
  118.         //logic
  119.         if(b[0])
  120.             rect.y--;
  121.         if(b[1])
  122.             rect.x--;
  123.         if(b[2])
  124.             rect.y++;
  125.         if(b[3])
  126.             rect.x++;
  127.         unsigned char* downKeys=SDL_GetKeyState(0);
  128.         if(downKeys[SDLK_w])
  129.             circle1.position.y--;
  130.         if(downKeys[SDLK_s])
  131.             circle1.position.y++;
  132.         if(downKeys[SDLK_a])
  133.             circle1.position.x--;
  134.         if(downKeys[SDLK_d])
  135.             circle1.position.x++;
  136.            
  137.         if(downKeys[SDLK_u])
  138.             circle2.position.y--;
  139.         if(downKeys[SDLK_j])
  140.             circle2.position.y++;
  141.         if(downKeys[SDLK_h])
  142.             circle2.position.x--;
  143.         if(downKeys[SDLK_k])
  144.             circle2.position.x++;
  145.        
  146.        
  147.         SDL_FillRect(screen,0,0);   //clear screen
  148.         SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format,255,0,0));    // fill the screen white (maybe better outside while loop)
  149.        
  150.         circle1.render();
  151.         circle2.render();
  152.        
  153.         if(circle1.intersects(&circle2))
  154.             std::cout << "Two cirlces intersecting" << std::endl;
  155.         if(circle1.intersects(&rect))
  156.             std::cout << "Circle1 intersects rect" << std::endl;
  157.         if(circle2.intersects(&rect))
  158.             std::cout << "Circle2 intersects rect" << std::endl;
  159.        
  160.         SDL_Flip(screen);
  161.  
  162.         if(1000/FPS > SDL_GetTicks()-start) {
  163.             SDL_Delay(1000/FPS-(SDL_GetTicks()-start));
  164.         }
  165.     }
  166.     SDL_Quit();
  167.     return 0;
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement