bool wheel::checkCollide(terrain* world) { tritest::Sphere s; glm::vec4 loc = getLoc(); s.c = glm::vec3(loc.x, loc.y, loc.z); s.r = radius; int gridX, gridY; world->getGridPos(loc.x, loc.y, gridX, gridY); for(int x = gridX-1; x<=gridX+1; x++) { if(x<0)continue; if(x>=world->width-1)break; for(int y = gridY-1; y<=gridY+1; y++) { if(y<0)continue; if(y>=world->height-1)break; Vertex* a = &world->model->vertices[x+y*world->width]; Vertex* b = &world->model->vertices[x+(y+1)*world->width]; Vertex* c = &world->model->vertices[x+1+y*world->width]; Vertex* d = &world->model->vertices[x+1+(y+1)*world->width]; /* glm::vec3 a = glm::vec3(x*world->gridSize, y*world->gridSize, world->getHeight(x,y)); glm::vec3 b = glm::vec3(x*world->gridSize, (y+1)*world->gridSize, world->getHeight(x,y+1)); glm::vec3 c = glm::vec3((x+1)*world->gridSize, y*world->gridSize, world->getHeight(x+1,y)); glm::vec3 d = glm::vec3((x+1)*world->gridSize, (y+1)*world->gridSize, world->getHeight(x+1,y+1));*/ if(checkCollide(world, s, a, b, d)) { return true; } if(checkCollide(world, s, a, c, d)) { return true; } } } return false; }
static int Collides(struct SPRITE *s, struct SPRITE *other) { /* basic collision detect, return TRUE if collides with other sprite */ return checkCollide(s->box, other->box); }
void handle_events(object_list * L){ SDL_Event event; SDL_Surface *superman = NULL; SDL_Rect supermanCoord, backgroundCoord, pipeCoord; SDL_Surface *background = NULL; int continuer = 1; int collisionSupermanEdge = 0; int collisionSupermanPipe = 0; int collision = 0; background = SDL_LoadBMP("background.bmp"); backgroundCoord = initBackground(backgroundCoord); superman = SDL_LoadBMP("superman.bmp"); SDL_SetColorKey(superman, SDL_SRCCOLORKEY, SDL_MapRGB(superman->format,0 ,255 ,0)); supermanCoord = initSuperman(supermanCoord); while(continuer){ SDL_PollEvent(&event); switch(event.type){ case SDL_QUIT: continuer = 0; break; case SDL_KEYDOWN: switch(event.key.keysym.sym){ case SDLK_SPACE: supermanCoord.y -= 3; break; default: break; } } supermanCoord.y += 1; collisionSupermanPipe = printAllPipe(L, supermanCoord); //superman tape un tuyau collisionSupermanEdge = !checkCollide(supermanCoord, backgroundCoord); //superman sort de l'écran collision = collisionSupermanPipe || collisionSupermanEdge; //OU logique entre les 2 collisions possibles if(collision == 0){ //pas de collision moveAllPipe(L); SDL_BlitSurface(background, NULL, screen, &backgroundCoord); SDL_BlitSurface(superman, NULL, screen, &supermanCoord); printAllPipe(L, supermanCoord); } else{ freeList(L); init(); } SDL_Flip(screen); } SDL_FreeSurface(background); SDL_FreeSurface(superman); }
void moveSpriteCutscene(struct SPRITE *s, unsigned int animIndex, unsigned int loopAnim, float x, float y, float speed) { /* move sprite in a cutscene, to x and y */ struct HITBOX h; newHitbox(&h, x, y, 1, 1); startCutscene(); do { /* update only this sprite and draw */ if (refreshCutscene()) { SetVelocityBetweenHitboxes(s, h, speed); updateSprite(s); s->SetAnim(s, animIndex, loopAnim); } } while (!checkCollide(s->box, h)); endCutscene(); }
bool LineBlockMap::checkSpriteNextPointValid(CCSprite *nowNode,const CCPoint &dest) { if (!nowNode) return false; CCPoint nowPoint = nowNode->getPosition(); float dx = dest.x; float dy = dest.y; float oldx = nowPoint.x - (nowNode->getContentSize().width/2); float oldy = nowPoint.y - (nowNode->getContentSize().height/2); float oldmaxx = oldx + nowNode->getContentSize().width; float oldmaxy = oldy + nowNode->getContentSize().height; float x = dx - (nowNode->getContentSize().width/2); float y = dy - (nowNode->getContentSize().height/2); float maxx = x + nowNode->getContentSize().width; float maxy = y + nowNode->getContentSize().height; x = oldx < x ? oldx : x; y = oldy < y ? oldy : y; maxx = oldmaxx > maxx ? oldmaxx:maxx; maxy = oldmaxy > maxy ? oldmaxy : maxy; float width = maxx - x ; float height = maxy - y; CCRect rect = CCRectMake(x ,y ,width,height); return checkCollide(rect); }