Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //http://www.youtube.com/user/thecplusplusguy
- //you need cirlce.bmp 20x20 bmp image containing a 10 pixel radius (20 diameter) circle
- //circle collision example
- #include <SDL/SDL.h>
- #include <iostream>
- struct Circle
- {
- SDL_Rect position;
- SDL_Surface* image;
- int radius;
- Circle(SDL_Surface* img, int x,int y,int radius)
- {
- image=img;
- position.x=x;
- position.y=y;
- this->radius=radius;
- }
- void render()
- {
- SDL_BlitSurface(image,NULL,SDL_GetVideoSurface(),&position);
- }
- bool intersects(Circle* c)
- {
- int x=c->position.x - position.x;
- int y=c->position.y - position.y;
- int r=radius + c->radius;
- return (x*x + y*y < r*r); //a^2+b^2=c^2
- }
- bool intersects(SDL_Rect* rec)
- {
- //source: http://stackoverflow.com/questions/401847/circle-rectangle-collision-detection-intersection
- int circleDistanceX = abs(position.x - rec->x);
- int circleDistanceY = abs(position.y - rec->y);
- if (circleDistanceX > (rec->w/2 + radius)) { return false; }
- if (circleDistanceY > (rec->h/2 + radius)) { return false; }
- if (circleDistanceX <= (rec->w/2)) { return true; }
- if (circleDistanceY <= (rec->h/2)) { return true; }
- int cornerDistance_sq = ((float)circleDistanceX - rec->w/2)*(circleDistanceX - rec->w/2) + (circleDistanceY - rec->h/2)*(circleDistanceY - rec->h/2);
- return (cornerDistance_sq <= (radius*radius));
- }
- };
- int main(int argc, char** argv)
- {
- SDL_Init(SDL_INIT_EVERYTHING);
- SDL_Surface* screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
- bool running = true;
- const int FPS = 30;
- Uint32 start;
- bool b[4] = {0,0,0,0};
- SDL_Rect rect;
- rect.x = 10;
- rect.y = 10;
- rect.w = 20;
- rect.h = 20;
- SDL_Surface* circle=SDL_LoadBMP("circle.bmp");
- if(!circle)
- {
- std::cout << "You need circle.bmp" << std::endl;
- return 1;
- }
- Circle circle1(circle,30,30,10);
- Circle circle2(circle,50,50,10);
- SDL_SetColorKey(circle,SDL_SRCCOLORKEY,SDL_MapRGB(screen->format,255,0,255));
- while(running) {
- start = SDL_GetTicks();
- SDL_Event event;
- while(SDL_PollEvent(&event)) {
- switch(event.type) {
- case SDL_QUIT:
- running = false;
- break;
- case SDL_KEYDOWN:
- switch(event.key.keysym.sym) {
- case SDLK_UP:
- b[0] = 1;
- break;
- case SDLK_LEFT:
- b[1] = 1;
- break;
- case SDLK_DOWN:
- b[2] = 1;
- break;
- case SDLK_RIGHT:
- b[3] = 1;
- break;
- }
- break;
- case SDL_KEYUP:
- switch(event.key.keysym.sym) {
- case SDLK_UP:
- b[0] = 0;
- break;
- case SDLK_LEFT:
- b[1] = 0;
- break;
- case SDLK_DOWN:
- b[2] = 0;
- break;
- case SDLK_RIGHT:
- b[3] = 0;
- break;
- }
- break;
- }
- }
- //logic
- if(b[0])
- rect.y--;
- if(b[1])
- rect.x--;
- if(b[2])
- rect.y++;
- if(b[3])
- rect.x++;
- unsigned char* downKeys=SDL_GetKeyState(0);
- if(downKeys[SDLK_w])
- circle1.position.y--;
- if(downKeys[SDLK_s])
- circle1.position.y++;
- if(downKeys[SDLK_a])
- circle1.position.x--;
- if(downKeys[SDLK_d])
- circle1.position.x++;
- if(downKeys[SDLK_u])
- circle2.position.y--;
- if(downKeys[SDLK_j])
- circle2.position.y++;
- if(downKeys[SDLK_h])
- circle2.position.x--;
- if(downKeys[SDLK_k])
- circle2.position.x++;
- SDL_FillRect(screen,0,0); //clear screen
- SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format,255,0,0)); // fill the screen white (maybe better outside while loop)
- circle1.render();
- circle2.render();
- if(circle1.intersects(&circle2))
- std::cout << "Two cirlces intersecting" << std::endl;
- if(circle1.intersects(&rect))
- std::cout << "Circle1 intersects rect" << std::endl;
- if(circle2.intersects(&rect))
- std::cout << "Circle2 intersects rect" << std::endl;
- SDL_Flip(screen);
- if(1000/FPS > SDL_GetTicks()-start) {
- SDL_Delay(1000/FPS-(SDL_GetTicks()-start));
- }
- }
- SDL_Quit();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement