Guest User

Collision Test Code

a guest
Apr 28th, 2011
405
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.28 KB | None | 0 0
  1. #include "SDL/SDL.h"
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4.  
  5. //This is actually C code, so I'm setting up a bool
  6. //type, like the one in C++.
  7. #define true 1
  8. #define false 0
  9. typedef int bool;
  10.  
  11. //Sprite data structure.
  12. typedef struct sprite_t {
  13.   int x, y; //Horizontal  and vertical position, respectively. I assume these refer to the upper left corner of the sprite.
  14.   int w, h; //Width and height of sprite, respectively.
  15.   SDL_Surface *image;
  16. }sprite_t;
  17.  
  18. //One of the few good reasons to use a global variable:
  19. //the screen surface
  20. SDL_Surface *screen;
  21.  
  22. //set sprite width and height from image.
  23. void setup_sprite(sprite_t *sprite);
  24.  
  25. //collision test function. Returns true if sprites are touching.
  26. bool collision(sprite_t sprite_1, sprite_t sprite_2);
  27.  
  28. //render put the sprite on the screen
  29. void render(sprite_t sprite);
  30.  
  31. int main(){
  32.     //sprites
  33.     sprite_t s1,s2;
  34.   //load images
  35.     s1.image = SDL_LoadBMP("sprite-1.bmp");
  36.     s2.image = SDL_LoadBMP("sprite-2.bmp");
  37.     //check if image loaded correctly:
  38.     if(s1.image == NULL || s2.image == NULL){
  39.         printf("Error: image load failed: %s\n", SDL_GetError());
  40.         return -1;
  41.     }
  42.     //set sprite width from surface
  43.     setup_sprite(&s1);
  44.     setup_sprite(&s2);
  45.     //Init SDL
  46.     if (SDL_Init(SDL_INIT_VIDEO)){
  47.     printf("Error: Unable to init SDL: %s\n", SDL_GetError());
  48.     return -1;
  49.   }
  50.   //Create window
  51.     screen = SDL_SetVideoMode(640, 480, 24, SDL_SWSURFACE);
  52.     if(screen == NULL){
  53.         printf("Error: Unable to create window: %s\n", SDL_GetError());
  54.         return -1;
  55.     }
  56.     //set sprite positions.
  57.     s1.x = 200;
  58.     s1.y = 200;
  59.     s2.x = 150;
  60.     s2.y = 150;
  61.     //main loop
  62.     int done = 0;
  63.     SDL_Event event;
  64.     while(!done){
  65.         //use the arrow keys to move s2 around.
  66.         while(SDL_PollEvent(&event)){
  67.             switch(event.type){
  68.                 case SDL_KEYDOWN:
  69.                     switch(event.key.keysym.sym){
  70.                         case SDLK_UP: s2.y--; break;
  71.                         case SDLK_DOWN: s2.y++; break;
  72.                         case SDLK_RIGHT: s2.x++; break;
  73.                         case SDLK_LEFT: s2.x--; break;
  74.                         case SDLK_ESCAPE: done = 1; break;
  75.                     }
  76.                     //check if moving resulted in a collision.
  77.                     if(collision(s1,s2)){
  78.                         printf("collision!\n");
  79.                     }
  80.                     break;
  81.                 case SDL_QUIT:
  82.                     done = 1;
  83.                     break;
  84.             }
  85.         }
  86.         //clear screen
  87.         SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
  88.         //render sprites
  89.         render(s1);
  90.         render(s2);
  91.         SDL_Flip(screen);
  92.     }
  93.     return 0;
  94. }
  95.  
  96. //collision test function. Returns true if sprites are touching.
  97. bool collision(sprite_t sprite_1, sprite_t sprite_2){
  98.   //Let's examine the x axis first:
  99.   //If the leftmost or rightmost point of the first sprite lies somewhere inside the second, continue.
  100.   if( (sprite_1.x >= sprite_2.x && sprite_1.x <= (sprite_2.x + sprite_2.w)) ||
  101.       ((sprite_1.x + sprite_1.w) >= sprite_2.x && (sprite_1.x + sprite_1.w) <= (sprite_2.x + sprite_2.w)) ){
  102.     //Now we look at the y axis:
  103.     if( (sprite_1.y >= sprite_2.y && sprite_1.y <= (sprite_2.y + sprite_2.h)) ||
  104.         ((sprite_1.y + sprite_1.h) >= sprite_2.y && (sprite_1.y + sprite_1.h) <= (sprite_2.y + sprite_2.h)) ){
  105.       //The sprites appear to overlap.
  106.       return true;
  107.     }
  108.   }
  109.   //This algorithm will not detect collisions if sprite_2 is smaller than sprite_1.
  110.     //To solve this problem, we'll check again, using sprite_2 in place of sprite_1.
  111.     if( (sprite_2.x >= sprite_1.x && sprite_2.x <= (sprite_1.x + sprite_1.w)) ||
  112.       ((sprite_2.x + sprite_2.w) >= sprite_1.x && (sprite_2.x + sprite_2.w) <= (sprite_1.x + sprite_1.w)) ){
  113.     //Now we look at the y axis:
  114.     if( (sprite_2.y >= sprite_1.y && sprite_2.y <= (sprite_1.y + sprite_1.h)) ||
  115.         ((sprite_2.y + sprite_2.h) >= sprite_1.y && (sprite_2.y + sprite_2.h) <= (sprite_1.y + sprite_1.h)) ){
  116.       //The sprites appear to overlap.
  117.       return true;
  118.     }
  119.   }
  120.   //The sprites do not overlap:
  121.   return false;
  122. }
  123.  
  124. //set sprite width and height from image.
  125. void setup_sprite(sprite_t *sprite){
  126.     //safety first:
  127.     if(sprite == NULL)return;
  128.     if(sprite->image == NULL)return;
  129.    
  130.     sprite->w = sprite->image->w;
  131.     sprite->h = sprite->image->h;
  132. }
  133.  
  134. //render put the sprite on the screen
  135. void render(sprite_t sprite){
  136.     if(sprite.image == NULL)return;
  137.     SDL_Rect drect; //used to store location image to blit
  138.     drect.x = sprite.x;
  139.     drect.y = sprite.y;
  140.     SDL_BlitSurface(sprite.image, NULL, screen, &drect);
  141. }
Advertisement
Add Comment
Please, Sign In to add comment