edub13 Posted March 25, 2012 at 06:17 PM Report #445599 Posted March 25, 2012 at 06:17 PM Boas pessoal, não estou a conseguir criar colisao entre dois rectangulos? Como e que faço? Ate agora tenho isto : #include <SDL/SDL.h> #include <stdbool.h> #define S_WIDTH 500 #define S_HEIGHT 500 #define S_BPP 32 SDL_Surface * screen = NULL; SDL_Event event; int Run = 1; int main(int argc, char ** argv) { SDL_Init(SDL_INIT_VIDEO); screen = SDL_SetVideoMode(S_WIDTH,S_HEIGHT,S_BPP,SDL_HWSURFACE); SDL_Rect a; a.x = 10; a.y = 10; a.w = 20; a.h = 20; SDL_Rect b; b.x = 300; b.y = 300; b.w = 40; b.h = 40; int keys [323] = {false}; while(Run){ if(SDL_PollEvent(&event)){ if(event.type == SDL_QUIT){ Run = 0; } if(event.type == SDL_KEYDOWN) { keys[event.key.keysym.sym] = true; } if(event.type == SDL_KEYUP) { keys[event.key.keysym.sym] = false; } } if(keys[sDLK_RIGHT]) { a.x += 1; } if(keys[sDLK_LEFT]) { a.x -= 1; } if(keys[sDLK_DOWN]) { a.y += 1; } if(keys[sDLK_UP]) { a.y -= 1; } //Colisao janela if(a.x < 0 ){ a.x = 0; } if(a.y < 0 ){ a.y = 0; } if(a.x + a.w > 500 ){ a.x = 500 - a.w; } if(a.y + a.h > 500){ a.y = 500 - a.h ; } SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0)); SDL_FillRect(screen,&a,SDL_MapRGB(screen->format, 200,0,0)); SDL_FillRect(screen,&b,SDL_MapRGB(screen->format, 0,200,0)); SDL_Flip(screen); } SDL_Quit(); return(EXIT_SUCCESS); } Learning: C++
bsccara Posted March 25, 2012 at 11:38 PM Report #445655 Posted March 25, 2012 at 11:38 PM Detectar a intersecção de dois rectangulos ? Vê aqui: http://www.tekpool.com/node/2686
seuqram Posted March 26, 2012 at 12:16 AM Report #445666 Posted March 26, 2012 at 12:16 AM tens de ter o x e y dos 2 retangutos: yretangulo1 yretangulo2 xretangulo1 xretangulo2 depois tens de meter assim: if(xretangulo1+(lado1:2)>xretangulo2-(lado2:2)&&xretangulo1-(lado1:2)<xretangulo2+(lado2:2)&&yretangulo1-(lado1:2)<yretangulo2+(lado1:2)&&yretangulo1+(lado1:2)>yretangulo2-(lado1:2)) { //colidiu! } if(!xretangulo1+(lado1:2)>xretangulo2-(lado2:2)&&!xretangulo1-(lado1:2)<xretangulo2+(lado2:2)&&!yretangulo1-(lado1:2)<yretangulo2+(lado1:2)&&!yretangulo1+(lado1:2)>yretangulo2-(lado1:2)) { //não colidiu! } (tens de considerar o y e o x dos lados e depois tens de fazer se o lado dele da mair que esse e o outro ta menos que esse...) não conssigo explicar melhor! mas podes ver aqui como fazer melhor aquilo que tu queres ou aquilo que será melhor para tu fazeres! (http://www.vsoftgames.com/site/files/tecnicas_deteccao_colisao_para_jogos.pdf)
HappyHippyHippo Posted March 26, 2012 at 07:32 AM Report #445672 Posted March 26, 2012 at 07:32 AM Detectar a intersecção de dois rectangulos ? Vê aqui: http://www.tekpool.com/node/2686 😄 tens de ter o x e y dos 2 retangutos: yretangulo1 yretangulo2 xretangulo1 xretangulo2 depois tens de meter assim: if(xretangulo1+(lado1:2)>xretangulo2-(lado2:2)&&xretangulo1-(lado1:2)<xretangulo2+(lado2:2)&&yretangulo1-(lado1:2)<yretangulo2+(lado1:2)&&yretangulo1+(lado1:2)>yretangulo2-(lado1:2)) { //colidiu! } if(!xretangulo1+(lado1:2)>xretangulo2-(lado2:2)&&!xretangulo1-(lado1:2)<xretangulo2+(lado2:2)&&!yretangulo1-(lado1:2)<yretangulo2+(lado1:2)&&!yretangulo1+(lado1:2)>yretangulo2-(lado1:2)) { //não colidiu! } (tens de considerar o y e o x dos lados e depois tens de fazer se o lado dele da mair que esse e o outro ta menos que esse...) não conssigo explicar melhor! mas podes ver aqui como fazer melhor aquilo que tu queres ou aquilo que será melhor para tu fazeres! (http://www.vsoftgames.com/site/files/tecnicas_deteccao_colisao_para_jogos.pdf) ? IRC : sim, é algo que ainda existe >> #p@p Portugol Plus
pikax Posted March 26, 2012 at 01:43 PM Report #445716 Posted March 26, 2012 at 01:43 PM ? LOL X2 Por muito mais que que estude só aprendo uma coisa, que ainda tenho muita coisa para aprender. A beleza de um código está em decompor problemas complexos em pequenos blocos simples. "learn how to do it manually first, then use the wizzy tool to save time." "Kill the baby, don't be afraid of starting all over again. Fail soon, learn fast."
edub13 Posted March 26, 2012 at 09:04 PM Author Report #445789 Posted March 26, 2012 at 09:04 PM Já estive a ver os links, que mandaram, mas não tou a perceber como faço :b Learning: C++
pikax Posted March 27, 2012 at 01:50 AM Report #445831 Posted March 27, 2012 at 01:50 AM Já estive a ver os links, que mandaram, mas não tou a perceber como faço :b http://lazyfoo.net/SDL_tutorials/lesson17/index.php fica algo assim: #define S_WIDTH 500 #define S_HEIGHT 500 #define S_BPP 32 SDL_Surface * screen = NULL; SDL_Event event; int Run = 1; int TestCollision(SDL_Rect *r1, SDL_Rect *r2) { int leftA, leftB; int rightA,rightB; int topA,topB; int bottomA,bottomB; leftA=r1->x; rightA=r1->x+r1->w; topA=r1->y; bottomA=r1->y+r1->h; leftB=r2->x; rightB=r2->x+r2->w; topB=r2->y; bottomB=r2->y+r2->h; if(bottomA<=topB || topA>=bottomB || rightA<=leftB || leftA>=rightB ) return 1; return 0; } int main(int argc, char ** argv) { SDL_Init(SDL_INIT_VIDEO); screen = SDL_SetVideoMode(S_WIDTH,S_HEIGHT,S_BPP,SDL_HWSURFACE); SDL_Rect a; a.x = 10; a.y = 10; a.w = 20; a.h = 20; SDL_Rect b; b.x = 300; b.y = 300; b.w = 40; b.h = 40; int keys [323] = {false}; while(Run){ if(SDL_PollEvent(&event)){ if(event.type == SDL_QUIT){ Run = 0; } if(event.type == SDL_KEYDOWN) { keys[event.key.keysym.sym] = true; } if(event.type == SDL_KEYUP) { keys[event.key.keysym.sym] = false; } } if(keys[sDLK_RIGHT]) { a.x += 1; } if(keys[sDLK_LEFT]) { a.x -= 1; } if(keys[sDLK_DOWN]) { a.y += 1; } if(keys[sDLK_UP]) { a.y -= 1; } //Colisao janela if(a.x < 0 ){ a.x = 0; } if(a.y < 0 ){ a.y = 0; } if(a.x + a.w > 500 ){ a.x = 500 - a.w; } if(a.y + a.h > 500){ a.y = 500 - a.h ; } SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0)); if(TestCollision(&a,&b)) { SDL_FillRect(screen,&b,SDL_MapRGB(screen->format, 0,0,200)); } else SDL_FillRect(screen,&b,SDL_MapRGB(screen->format, 0,200,0)); SDL_FillRect(screen,&a,SDL_MapRGB(screen->format, 200,0,0)); SDL_Flip(screen); } SDL_Quit(); return(EXIT_SUCCESS); } Por muito mais que que estude só aprendo uma coisa, que ainda tenho muita coisa para aprender. A beleza de um código está em decompor problemas complexos em pequenos blocos simples. "learn how to do it manually first, then use the wizzy tool to save time." "Kill the baby, don't be afraid of starting all over again. Fail soon, learn fast."
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now