Je suis en train de coder un petit jeu de plateforme 2D avec la SDL et afin de gérer les collisions, j'ai essayé la méthode du quad tree. Seulement, cela ne marche pas, et le personnage ne peut plus bouger, aucune touche configurée n'est utisable.
Merci d'avance de votre aide.
Désolé d'avance si il n'y a pas assez de commentaire. J'ai d'abord mis le code des fonctions, puis le main.

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <SDL/SDL.h>
#include <stdlib.h>
#include <stdio.h>
 
typedef struct dimensions dimensions;
struct dimensions
{
	int L;
	int l;
	int x;
	int y;
};
 
int collision(SDL_Rect* positionA, SDL_Rect positionB, int D)  //fonction de collision basique
{
	if(D == 0)
	{
		if (((*positionA).y <= (positionB.y + positionB.h)) && ((*positionA).x < (positionB.x + positionB.w)) && (((*positionA).x + (*positionA).w) > positionB.x) && ((*positionA).y > positionB.y))
		{
			(*positionA).y = (positionB.y + positionB.h);
		}
		}
 
	if(D == 1)
	{
		if ((((*positionA).y + (*positionA).h) >= positionB.y) && (((*positionA).y + (*positionA).h) < (positionB.y + positionB.h)) && ((*positionA).x < (positionB.x + positionB.w)) && (((*positionA).x + (*positionA).w) > positionB.x))
		{
			(*positionA).y = (positionB.y - (*positionA).h);
		}
	}
 
	if(D == 2)
	{
		if ((((*positionA).x + (*positionA).w) < (positionB.x + positionB.w)) && (((*positionA).x + (*positionA).w) >= positionB.x) && (((*positionA).y + (*positionA).h > positionB.y)) && ((*positionA).y < (positionB.y + positionB.h)))
		{
			(*positionA).x = (positionB.x - (*positionA).w);
		}
	}
 
	if(D == 3)
	{
		if (((*positionA).x > positionB.x + 1/2*positionB.w) && ((*positionA).x < positionB.x + positionB.w) && ((*positionA).y + (*positionA).h > positionB.y) && ((*positionA).y < positionB.y + positionB.h))
		{
			(*positionA).x = (positionB.x + positionB.w);
		}
	}
}
 
void QuadTreeCollisions(SDL_Rect *positionA, dimensions dimensions2Ecran, int *choix2, int dude)//fonction qui définit les zones du quadrilla
{												//ge dans lesquelles se trouve le personnage
        int i = 0;								
	for( i = 0;i < dude;i ++)
	{
        	if(((*positionA).x > dimensions2Ecran[i].x) && ((*positionA).x <= ((1/2)*dimensions2Ecran[i].l)) && ((*positionA).y > dimensions2Ecran[i].y) &&((*positionA).y <= ((1/2)*dimensions2Ecran[i].L)))
        	{
                	choix2[i] = i;
                	i ++;
        	}
	}
}
 
void QuadTreeFaireCollisions(SDL_Rect (*tableau1[])[],int *tableauNombreTerme,SDL_Rect *positionA, int choix)//fonction qui appelle les diffé
{													//ntes collisions à tester
        int i = 0, f = 0;
        for(i = 0; i < tableauNombreTerme[choix]; i++)
        {
                for(f = 0 ; f < 4; f++)
                {
                        collision(positionA, (*tableau1[choix])[i], f);
                } 
        }
}
 
 
void QuadTree( SDL_Rect (*tableau1[])[], int tableauNombreTerme[], SDL_Rect *positionA, dimensions dimensionsEcran)	//fonction principale
{													//de la gestion de collision
	int choix[4] = {42,42,42,42};
	int i = 0, e = 0, f = 0;
	int choix2[16] = {42};
	dimensions dimensions2Ecrans[16] = {42};
	dimensions dimension1Ecrans[4] = {dimensionsEcran, dimensionsEcran, dimensionsEcran, dimensionsEcran};
	pause();
	QuadTreeCollisions(positionA, dimension1Ecrans, choix, 4);
	pause();
	for(i = 0; i < 4; i+4)
	{
		switch (choix[i])
		{
			case 0:
			dimensions2Ecrans[i].L = (1/2)*dimensionsEcran.L;
			dimensions2Ecrans[i].l = (1/2)*dimensionsEcran.l;
			dimensions2Ecrans[i].x = 0;
			dimensions2Ecrans[i].y = 0;
			break;
			case 1:
			dimensions2Ecrans[i+1].l = (1/2)*dimensionsEcran.l;
			dimensions2Ecrans[i+1].x = 0;
			dimensions2Ecrans[i+1].y = (1/2)*dimensionsEcran.L;
			break;
			case 2:
			dimensions2Ecrans[i+2].x = (1/2)*dimensionsEcran.l;
			dimensions2Ecrans[i+2].y = (1/2)*dimensionsEcran.L;
			case 3:
			dimensions2Ecrans[i+3].l = (1/2)*dimensionsEcran.l;
			dimensions2Ecrans[i+3].x = 0;
			dimensions2Ecrans[i+3].y = (1/2)*dimensionsEcran.L;
			break;
		}
	}	
	QuadTreeCollisions(positionA, dimensions2Ecrans, choix2, 16);
	for(e = 0; e < 4 ; e++)
	{
		for(f = i; f < i+4 ; f++)
		{
			if (choix2[f] != 42)
			{
				choix[i] = choix[i] + choix2[f];
				i = i+4;
			}
		}
	}
	for (e = 0; e < 4; e++);
	{
		QuadTreeFaireCollisions((tableau1), tableauNombreTerme, positionA, choix[e]);
	}
}

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#include<stdlib.h>
#include<stdio.h>
#include<SDL/SDL.h>
#include<math.h>
#include"collisions.h"
 
int main()
{
	dimensions dimensionsEcran;	//dimensions de l'écran
	dimensionsEcran.L = 660;
	dimensionsEcran.l = 900;
	dimensionsEcran.x = 0;
	dimensionsEcran.y = 0;
 
	int positionSpeciale = 30;   
	int Bool = 0;
	int D[4] = {0,1,2,3};
	int direction = 0;
	int saut = 0;
	int continuer = 1;
	int toilettes = 1;
	SDL_Event event;	
 
	SDL_Surface *baker = NULL;
	SDL_Surface *sol = NULL;	//Sol du niveau
	SDL_Surface *Baguette = NULL;	//baguette
	SDL_Surface *pain2 = NULL;	//pain au melon
	SDL_Surface *mur = NULL;	//Mur vertical
	SDL_Surface *ecran = NULL;
 
	SDL_Rect bordEcran00;		//surfaces qui permet de gérer la sortie de l'écran
	bordEcran00.x = 0;
	bordEcran00.y = 0;
	bordEcran00.w = 0;
	bordEcran00.h = 660;
 
	SDL_Rect bordEcran01;
	bordEcran01.x = 0;
	bordEcran01.y = 0;
	bordEcran01.w = 900;
	bordEcran01.h = 0;
 
	SDL_Rect positionBaguette;
	positionBaguette.x = 0;
	positionBaguette.y = 0;
	positionBaguette.w = 45;
	positionBaguette.h = 20;
 
	SDL_Rect positionSol1;		//Position du sol du niveau
	positionSol1.x = 0;
	positionSol1.y = 630;
	positionSol1.w = 900;
	positionSol1.h = 30;
 
	SDL_Rect positionSol2;		//Position grande plate-forme
	positionSol2.x = 120;
	positionSol2.y = 450;
	positionSol2.w = 630;
	positionSol2.h = 30;
 
	SDL_Rect positionMur1;
	positionMur1.x = 870;
	positionMur1.y = 128;
	positionMur1.w = 30;
	positionMur1.h = 520;
 
	SDL_Rect positionMur2;
	positionMur2.x = 0;
	positionMur2.y = 0;
	positionMur2.w = 20;
	positionMur2.h = 180;
 
	SDL_Rect positionpltefrme[5];
 
	positionpltefrme[0].x = 340;
	positionpltefrme[0].y = 390;
	positionpltefrme[0].w = 100;
	positionpltefrme[0].h = 20;
 
	positionpltefrme[1].x = 140;
	positionpltefrme[1].y = 320;
	positionpltefrme[1].w = 200;
	positionpltefrme[1].h = 30;
 
	positionpltefrme[2].x = 125;
	positionpltefrme[2].y = 100;
	positionpltefrme[2].w = 200;
	positionpltefrme[2].h = 30;	
 
	positionpltefrme[3].x = 360;
	positionpltefrme[3].y = 140;
	positionpltefrme[3].w = 200;
	positionpltefrme[3].h = 30;
 
	positionpltefrme[4].x = 600;
	positionpltefrme[4].y = 128;
	positionpltefrme[4].w = 300;
	positionpltefrme[4].h = 30;
 
	SDL_Rect clip[3];
 
	clip[0].x = 0;				//parties du sol à charger pour faire les plate-formes horizontales.
	clip[0].y = 0;
	clip[0].w = 630;
	clip[0].h = 30;
 
	clip[1].x = 0;
	clip[1].y = 0;
	clip[1].w = 200;
	clip[1].h = 30;
 
	clip[2].x = 0;
	clip[2].y = 0;
	clip[2].w = 300;
	clip[2].h = 30;
 
	SDL_Rect positionBaker;			//position du personnage
	positionBaker.x = 10;
	positionBaker.y = 565;
	positionBaker.w = 45;
	positionBaker.h = 65;
 
	SDL_Rect* positionA = &positionBaker;
 
 
	SDL_Rect tab1[2];			//création des différents tableaux qui contiennent les coordonnées des objets du niveau
        tab1[0] = positionMur2;
        tab1[1] = positionpltefrme[2];
 
	SDL_Rect tab2[3];
	tab2[0] = bordEcran01;
        tab2[1] = positionpltefrme[2];
        tab2[2] = positionpltefrme[3];
 
 	SDL_Rect tab3[1];
        tab3[0] = positionpltefrme[1];
 
	SDL_Rect tab4[2];
	tab4[0] = positionpltefrme[1];
        tab4[1] = positionMur2;
 
	SDL_Rect tab5[2];
        tab5[0] = positionpltefrme[4];
        tab5[1] = positionpltefrme[3];
 
	SDL_Rect tab6[2];
        tab6[0] = positionpltefrme[4];
        tab6[1] = positionMur1;
 
	SDL_Rect tab7[2];
        tab7[0] = positionpltefrme[4];
        tab7[1] = positionMur1;
 
	SDL_Rect tab8[2];
        tab8[0] = positionpltefrme[3];
        tab8[1] = positionpltefrme[4];
 
	SDL_Rect tab9[2];
        tab9[0] = positionpltefrme[0];
        tab9[1] = positionSol2;
 
	SDL_Rect tab10[2];
        tab10[0] = positionSol2;
        tab10[1] = positionMur1;
 
	SDL_Rect tab11[3];
        tab11[0] = positionSol1;
        tab11[1] = positionSol2;
        tab11[2] = positionMur1;
 
	SDL_Rect tab12[2];
        tab12[0] = positionSol2;
        tab12[1] = positionSol1;
 
	SDL_Rect tab13[3];
        tab13[0] = positionMur2;
        tab13[1] = positionpltefrme[1];
        tab13[2] = positionSol2;
 
	SDL_Rect tab14[3];
        tab14[0] = positionSol2;
        tab14[1] = positionpltefrme[0];
        tab14[2] = positionpltefrme[1];
 
	SDL_Rect tab15[2];
        tab15[0] = positionSol2;
        tab15[1] = positionSol1;
 
	SDL_Rect tab16[4];
        tab16[0] = positionSol1;
        tab16[1] = positionSol2;
        tab16[2] = positionMur2;
        tab16[3] = bordEcran00;
 
 
	SDL_Rect (*tableauObjetsCollisions[16])[] = {NULL}; 			//création du tableau de pointeurs sur des tableaux, qui perm
	(tableauObjetsCollisions[0]) = &tab1;					//de transmettre les coordonnées des objets du niveaux aux 
	(tableauObjetsCollisions[1]) = &tab2;					//fonctions
	(tableauObjetsCollisions[2]) = &tab3;
	(tableauObjetsCollisions[3]) = &tab4;
	(tableauObjetsCollisions[4]) = &tab5;
	(tableauObjetsCollisions[5]) = &tab6;
	(tableauObjetsCollisions[6]) = &tab7;
	(tableauObjetsCollisions[7]) = &tab8;
	(tableauObjetsCollisions[8]) = &tab9;
	(tableauObjetsCollisions[9]) = &tab10;
	(tableauObjetsCollisions[10]) = &tab11;
	(tableauObjetsCollisions[11]) = &tab12;
	(tableauObjetsCollisions[12]) = &tab13;
	(tableauObjetsCollisions[13]) = &tab14;;
	(tableauObjetsCollisions[14]) = &tab15;
	(tableauObjetsCollisions[15]) = &tab16;
 
	int tableau2[16] = {2,3,1,2,2,2,2,2,2,2,3,2,3,3,2,4};			//tableau contenant la taille de chaque tableau du grand tabl
 
	SDL_Rect positionBakerRel;						//eau de pointeurs
	positionBakerRel.x = 0;
	positionBakerRel.y = 0;
 
	ecran = SDL_SetVideoMode(900, 660, 32, SDL_HWSURFACE);			//création des surfaces
	sol = SDL_CreateRGBSurface(SDL_HWSURFACE, 900, 30, 32, 0, 0, 0, 0);
	mur = SDL_CreateRGBSurface(SDL_HWSURFACE, 30, 520, 32, 0, 0, 0, 0);
	baker = SDL_CreateRGBSurface(SDL_HWSURFACE, 45, 65, 32, 0, 0, 0, 0);
	SDL_FillRect(baker, NULL,SDL_MapRGB(ecran->format, 0, 0, 255));
	SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, 0, 0, 0));
	SDL_FillRect(sol, NULL, SDL_MapRGB(ecran->format, 255, 255, 255));
	SDL_FillRect(mur, NULL, SDL_MapRGB(ecran->format, 255, 255, 255));
 
	SDL_BlitSurface(sol, NULL, ecran, &positionSol1);			
	SDL_BlitSurface(sol, &clip[0], ecran, &positionSol2);
	SDL_BlitSurface(sol, &clip[1], ecran, &positionpltefrme[0]);
	SDL_BlitSurface(sol, &clip[1], ecran, &positionpltefrme[1]);
	SDL_BlitSurface(sol, &clip[1], ecran, &positionpltefrme[2]);
	SDL_BlitSurface(sol, &clip[1], ecran, &positionpltefrme[3]);
	SDL_BlitSurface(sol, &clip[2], ecran, &positionpltefrme[4]);
	SDL_BlitSurface(mur, NULL, ecran, &positionMur1);
	SDL_BlitSurface(mur, NULL, ecran, &positionMur2);
	SDL_BlitSurface(baker, NULL, ecran, &positionBaker);
 
	SDL_EnableKeyRepeat(10, 10);
 
	SDL_Flip(ecran);
 
	while(continuer)						//boucle principale
	{
 
		if(toilettes == 1)
		{
			SDL_WaitEvent(&event);				//gestion des déplacements
			switch(event.type)
			{
				case SDL_KEYDOWN:
				switch(event.key.keysym.sym)
				{
					case SDLK_RIGHT:
					positionBaker.x++;
					direction = 1;
					break;
					case SDLK_LEFT:
					positionBaker.x--;
					direction = 2;
					break;
					case SDLK_ESCAPE:
					continuer = 0;
					break;
					case SDLK_DOWN:
					toilettes = 0;
					break;
					case SDLK_SPACE:
					positionBaker.y--;
					break;
				}
				break;
			}
		}
		if (toilettes == 0)					//gestion des sauts
		{
        		if (saut <= 30)
			{
				positionBaker.y = positionBaker.y-2;
			}
			if (saut >= 30 && saut <= 60)
			{
				positionBaker.y = positionBaker.y+2;
			}
			if (direction == 1)
			{
				positionBaker.x++;
				Bool = 0;
			}
			if (direction == 2)
			{
				positionBaker.x--;
			}
			saut++;
			if (saut > 60)
			{
				toilettes = 1;
				saut = 0;
				direction = 0; 
			} 	
		}
 
 
		QuadTree((tableauObjetsCollisions), tableau2, positionA, dimensionsEcran);
 
		SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, 0, 0, 0));
		SDL_BlitSurface(sol, NULL, ecran, &positionSol1);
		SDL_BlitSurface(sol, &clip[0], ecran, &positionSol2);
		SDL_BlitSurface(sol, &clip[1], ecran, &positionpltefrme[0]);
		SDL_BlitSurface(sol, &clip[1], ecran, &positionpltefrme[1]);
		SDL_BlitSurface(sol, &clip[1], ecran, &positionpltefrme[2]);
		SDL_BlitSurface(sol, &clip[1], ecran, &positionpltefrme[3]);
		SDL_BlitSurface(sol, &clip[2], ecran, &positionpltefrme[4]);
		SDL_BlitSurface(mur, NULL, ecran, &positionMur1);
		SDL_BlitSurface(mur, NULL, ecran, &positionMur2);
		SDL_BlitSurface(baker, NULL, ecran, &positionBaker);
		SDL_Flip(ecran);
	}
 
	SDL_FreeSurface(sol);
	SDL_Quit();	
	return EXIT_SUCCESS;
}