File: colision.c - Tab length: 1 2 4 8 - Lines: on off - No wrap: on off

/* teste une collision entre 2 sprites
  struct sprite * sprite1 : pointeur sur la structure du sprite numero 1
  struct sprite * sprite2 : pointeur sur la structure du sprite numero 2
  int type    : type de collision
          0:collision simple
          1:renvois la zone entre 1 et 9 ou le sprite 2 touche le sprite 1
            1 2 3
            4 5 6      0
            7 8 9
*/


short test_collision(struct sprite * sprite1, struct sprite * sprite2, int type)
{ // colision par zone simple
  int collision = 0;
  if((sprite1->pos_y + sprite1->size_y) >= sprite2->pos_y)
    if(sprite1->pos_y <= (sprite2->pos_y + sprite2->size_y))
      if((sprite1->pos_x + sprite1->size_x) >= sprite2->pos_x)
        if(sprite1->pos_x <= (sprite2->pos_x + sprite2->size_x))
          collision = 1; // les deux sprites s'entrecroisent
  if(!type || !collision) return collision;

  // colision par zone, renvoi le mur de 1 � 9

  int x1 = sprite1->pos_x;
  int y1 = sprite1->pos_y;
  int x2 = x1 + sprite1->size_x;
  int y2 = y1 + sprite1->size_y;
  int xx1 = sprite2->pos_x;
  int yy1 = sprite2->pos_y;
  int xx2 = xx1 + sprite2->size_x;
  int yy2 = yy1 + sprite2->size_y;

  int x=0,y=0;

  // calcul de la position y du sprite 1 par rapport au 2 en collision
  if(y1 < yy1) y=0;
  else
  { if(y2 > yy2) y=2 ; else y=1;
  }

  // calcul de la position x du sprite 1 par rapport au 2 en collision
  if(x1 < xx1) x=0;
  else
  { if(x2 > xx2) x=2 ; else x=1;
  }

  return((y*3)+(x+1));
}