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

01: /* teste une collision entre 2 sprites
02:     struct sprite * sprite1 : pointeur sur la structure du sprite numero 1
03:     struct sprite * sprite2 : pointeur sur la structure du sprite numero 2
04:     int type        : type de collision
05:                     0:collision simple
06:                     1:renvois la zone entre 1 et 9 ou le sprite 2 touche le sprite 1
07:                         1 2 3
08:                         4 5 6      0
09:                         7 8 9
10: */
11: 
12: short test_collision(struct sprite * sprite1, struct sprite * sprite2, int type)
13: {   // colision par zone simple
14:     int collision = 0;
15:     if((sprite1->pos_y + sprite1->size_y) >= sprite2->pos_y)
16:         if(sprite1->pos_y <= (sprite2->pos_y + sprite2->size_y))
17:             if((sprite1->pos_x + sprite1->size_x) >= sprite2->pos_x)
18:                 if(sprite1->pos_x <= (sprite2->pos_x + sprite2->size_x))
19:                     collision = 1; // les deux sprites s'entrecroisent
20:     if(!type || !collision) return collision;
21: 
22:     // colision par zone, renvoi le mur de 1 � 9
23: 
24:     int x1 = sprite1->pos_x;
25:     int y1 = sprite1->pos_y;
26:     int x2 = x1 + sprite1->size_x;
27:     int y2 = y1 + sprite1->size_y;
28:     int xx1 = sprite2->pos_x;
29:     int yy1 = sprite2->pos_y;
30:     int xx2 = xx1 + sprite2->size_x;
31:     int yy2 = yy1 + sprite2->size_y;
32: 
33:     int x=0,y=0;
34: 
35:     // calcul de la position y du sprite 1 par rapport au 2 en collision
36:     if(y1 < yy1) y=0;
37:     else
38:     {   if(y2 > yy2) y=2 ; else y=1;
39:     }
40: 
41:     // calcul de la position x du sprite 1 par rapport au 2 en collision
42:     if(x1 < xx1) x=0;
43:     else
44:     {   if(x2 > xx2) x=2 ; else x=1;
45:     }
46: 
47:     return((y*3)+(x+1));
48: }