C++ & SDLfirst game, Hit detection problem

Hello, I recently started to learn C++ and SDL and I am working on a game called "Gun Fight" at the moment. The game is similar to pong in the sense that there are two objects on either side of the screen but in this case they are guns, they can only move vertical. The bullets move horizontal across the screen when shooting at the other gun. Each player has 5 bars of health, one bullet depletes one bar, at the moment when you reach 0 bars nothing happens because I have not programmed it in yet. My trouble is that the hit detection only registers a hit if the gun is also in motion, I can not seem to figure out why (Also there is a bug where the 5 bars of health will sometimes instantly drop after 1 hit, but that is less of a big deal for me)

Hit detection
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
 // Hit Detection

   
	if ( bullet2startX + widthb2 > dstX && dstY < bullet2startY && dstY + heightb2 > bullet2startY && shoot2 == true )
	{
		
		if (hwidth3 == 28)
		{
		hwidth3 = 0;

		}
		
		if (hwidth3 == 56)
		{
		hwidth3 = 28;
		}
		
		if (hwidth3 == 84)
		{
		hwidth3 = 56;
		}
		
		if (hwidth3 == 112)
		{
		hwidth3 = 84;
		}

		if (hwidth3 == 140)
		{
		hwidth3 = 112;
		}
		bullet2startX = 1000;
		}

	if ( bullet1startX + widthb1 > dstX2 && dstY2 < bullet1startY && dstY2 + heightb1 > bullet1startY && shoot1 == true )
	{
			
		if (hwidth2 == 28)
		{
		hwidth2 = 0;

		}
		
    	if (hwidth2 == 56)
		{
		hwidth2 = 28;
		}
		
		if (hwidth2 == 84)
		{
		hwidth2 = 56;
		}
		
		if (hwidth2 == 112)
		{
		hwidth2 = 84;
		}

		if (hwidth2 == 140)
		{
		hwidth2 = 112;
		}
		bullet1startX = -1000;
	}


All the variables
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
//Integers

// Health Bar
int hwidth = 140;
int hheight = 30;

int hwidth2 = 140;
int hheight2 = 30;
int hwidth3= 140;

int healthx1 = 400;
int healthx2 = 100;
int healthy = 400;


// Gun right
int width = 200;
int height = 200;

int srcX = 0;
int srcY = 0;

int dstX = 530;
int dstY = 150;

// Bullet 1
int widthb1 = 20;
int heightb1 = 20;

int bullet1startX;
int bullet1startY;

// Gun Left
int width2 = 200;
int height2 = 200;

int srcX2 = 0;
int srcY2 = 0;

int dstX2 = -80;
int dstY2 = 150;

// Bullet 2

int widthb2 = 20;
int heightb2 = 20;

int bullet2startX;
int bullet2startY;

int bulletspeed = 1;


Also this is the entire code if you want to take a look at it
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#include <sdl.h>

SDL_Surface *screen = NULL;
SDL_Surface *background = NULL;
SDL_Surface *sprite = NULL;
SDL_Surface *sprite2 = NULL;
SDL_Surface *sprite3 = NULL;
SDL_Surface *sprite4 = NULL;
SDL_Surface *sprite5 = NULL;
SDL_Surface *sprite6 = NULL;
//Integers

// Health Bar
int hwidth = 140;
int hheight = 30;

int hwidth2 = 140;
int hheight2 = 30;
int hwidth3= 140;

int healthx1 = 400;
int healthx2 = 100;
int healthy = 400;


// Gun right
int width = 200;
int height = 200;

int srcX = 0;
int srcY = 0;

int dstX = 530;
int dstY = 150;

// Bullet 1
int widthb1 = 20;
int heightb1 = 20;

int bullet1startX;
int bullet1startY;

// Gun Left
int width2 = 200;
int height2 = 200;

int srcX2 = 0;
int srcY2 = 0;

int dstX2 = -80;
int dstY2 = 150;

// Bullet 2

int widthb2 = 20;
int heightb2 = 20;

int bullet2startX;
int bullet2startY;

int bulletspeed = 1;
// Making an Image for the background
bool run = true;
SDL_Event event;

void surface(int x, int y, SDL_Surface* source, SDL_Surface* destination)
{
SDL_Rect rect;
rect.x = x;
rect.y = y;

SDL_BlitSurface (source, NULL, destination, &rect);

}

void draw_sprite(int srcX,int srcY,
     int dstX,int dstY,
     int width,int height,
     SDL_Surface *source, SDL_Surface *destination)
{
 SDL_Rect src;
 src.x = srcX;
 src.y = srcY;
 src.w = width;
 src.h = height;

 SDL_Rect dst;
 dst.x = dstX;
 dst.y = dstY;
 dst.w = width;
 dst.h = height;

SDL_BlitSurface(source, &src, destination, &dst);
}


int main ( int argc, char* argv[])

{

 SDL_Init(SDL_INIT_EVERYTHING);

 SDL_WM_SetCaption ("Gun Fight",NULL); // Sets caption
 //Load
 screen = SDL_SetVideoMode (640,440,32,SDL_SWSURFACE); //Establishes Screen
 background = SDL_LoadBMP("background.bmp"); // Loads the bmp
 sprite = SDL_LoadBMP("sprite.bmp"); // Loads the bmp
 sprite2 = SDL_LoadBMP("sprite2.bmp"); // Loads the bmp
 sprite3 = SDL_LoadBMP("sprite3.bmp"); // Loads the bmp
 sprite4 = SDL_LoadBMP("sprite4.bmp"); // Loads the bmp
 sprite5 = SDL_LoadBMP("sprite5.bmp"); // Loads the bmp
 sprite6 = SDL_LoadBMP("sprite6.bmp"); // Loads the bmp
 SDL_SetColorKey (sprite,SDL_SRCCOLORKEY,SDL_MapRGB (sprite ->format, 255, 255, 255)); //takes away any of set color
 SDL_SetColorKey (sprite2,SDL_SRCCOLORKEY,SDL_MapRGB (sprite2 ->format, 255, 255, 255)); //takes away any of set color
 SDL_SetColorKey (sprite6,SDL_SRCCOLORKEY,SDL_MapRGB (sprite6 ->format, 255, 255, 255));

 bool keys[323] = {false};
 bool shoot1 = {false};
 bool shoot2 = {false};
 
 while (run)

 {
	if (SDL_PollEvent(&event))
	 {	
		 if (event.type == SDL_QUIT)  
				  {
						  run = false;
				  }
		
		 
		 if(event.type == SDL_KEYDOWN) //If any key is pressed down
			{
			 
			keys[event.key.keysym.sym]	= true;
			
			}
		 if(event.type == SDL_KEYUP)
		 {
			keys[event.key.keysym.sym]	= false;
		 }
	}


// Key presses

// Right Gun
	    if (keys[SDLK_UP])
		{
			dstY -= 1;
		}
		if (keys[SDLK_DOWN])
		{
		dstY += 1;
		}
		
		if ( keys[SDLK_LEFT] & shoot1 == false )
		{
		
		shoot1 = true;
		bullet1startX = dstX;
		bullet1startY = dstY;
		}


// Left Gun
		if ( keys[SDLK_w] )
		{
			dstY2 -= 1;
		}

		if ( keys[SDLK_s] )
		{
			dstY2 += 1;
		}

		if ( keys[SDLK_d] & shoot2 == false )
		{
		
		shoot2 = true;
		bullet2startX = dstX2;
		bullet2startY = dstY2;
		}


// Screen Edges
		if (dstY < 0)
		{
			dstY = 0;
		}

		if (dstY > 360)
		{
			dstY = 360;
		}

		if (dstY2 < 0)
		{
			dstY2 = 0;
		}

		if (dstY2 > 360)
		{
			dstY2 = 360;
		}
	

   surface(0,0,background,screen);
   
    if ( shoot1 == true )
   {
	   if ( bullet1startX > 0 - widthb1 )
	   {
		    draw_sprite(srcX,srcY,
			bullet1startX,bullet1startY,
			widthb1,heightb1, 
			sprite4, screen);

			bullet1startX -= bulletspeed;
	   }
	   else
	   {
		shoot1 = false;
		
	   }
   }

	 if ( shoot2 == true )
   {
	   if ( bullet2startX < 640 - widthb1 )
	   {
		    draw_sprite(srcX2,srcY2,
			bullet2startX,bullet2startY,
			widthb2,heightb2, 
			sprite3, screen);

			bullet2startX += bulletspeed;
	   }
	   else
	   {
		shoot2 = false;
		
	   }
   }


   draw_sprite(srcX,srcY,
      healthx1,healthy,
      hwidth3,hheight, 
      sprite5, screen);
   
   draw_sprite(srcX,srcY,
      healthx1,healthy,
      hwidth,hheight, 
      sprite6, screen);

    draw_sprite(srcX,srcY,
      healthx2,healthy,
      hwidth2,hheight2, 
      sprite5, screen);

	 draw_sprite(srcX,srcY,
      healthx2,healthy,
      hwidth,hheight, 
      sprite6, screen);

   draw_sprite(srcX,srcY,
      dstX,dstY,
      width,height, 
      sprite, screen);
  
   draw_sprite(srcX2,srcY2,
      dstX2,dstY2,
      width2,height2, 
      sprite2, screen);
  
  
   // Hit Detection

   
	if ( bullet2startX + widthb2 > dstX && dstY < bullet2startY && dstY + heightb2 > bullet2startY && shoot2 == true )
	{
		
		if (hwidth3 == 28)
		{
		hwidth3 = 0;

		}
		
		if (hwidth3 == 56)
		{
		hwidth3 = 28;
		}
		
		if (hwidth3 == 84)
		{
		hwidth3 = 56;
		}
		
		if (hwidth3 == 112)
		{
		hwidth3 = 84;
		}

		if (hwidth3 == 140)
		{
		hwidth3 = 112;
		}
		bullet2startX = 1000;
		}

	if ( bullet1startX + widthb1 > dstX2 && dstY2 < bullet1startY && dstY2 + heightb1 > bullet1startY && shoot1 == true )
	{
			
		if (hwidth2 == 28)
		{
		hwidth2 = 0;

		}
		
    	if (hwidth2 == 56)
		{
		hwidth2 = 28;
		}
		
		if (hwidth2 == 84)
		{
		hwidth2 = 56;
		}
		
		if (hwidth2 == 112)
		{
		hwidth2 = 84;
		}

		if (hwidth2 == 140)
		{
		hwidth2 = 112;
		}
		bullet1startX = -1000;
	}
	   SDL_Flip (screen);

 }
 SDL_Quit();
 return 0;

}



Thank you guys for any help you can provide!
(sorry for the messy code)

Also -- Screen shot
http://gyazo.com/3d46592f3e526cd9f8aed0e51eb3dd36
-----

-Kyle
Last edited on
You ought to separate the interface. It will make your code clearer and more maintainable.
I meant, if you get hit the health will decrease. But how many hits you can take, or how will you represent it, it is another issue.

dstY < bullet2startY && dstY + heightb2 > bullet2startY That looks wrong.
I don't see in the code that the gun movement will make any difference.
ne555 Thank you for your assistance, I am having trouble understanding what you mean by "separating the interface"

Also, representing the hits that you take is done by re-adjusting the size of a bmp picture of a green bar, which you can see under the if statement for hit detection. The green bar lies underneath a black bar with 5 squares cut out, if you look at the screen shot you will see what I mean.

What looks wrong about the hit detection? could you be more specific

And yes, I do not see any code that the gun movement would make a difference, that is why I do not understand it.
Last edited on
I think it should be dstY < bullet2startY && dstY + height > bullet2startY //The bullet is 'inside' the gun You are just checking the bottom (¿or is it the top?) but that simplification don't hurt so much.

About the health bar. At first I thought that that was part of the detection algorithm.
The thing is, ¿how much code you need to change and how hard is it to locate it, if you want to modify the game?
By instance give them 3 life instead of 5. Or using colors to represent the health.
Or no health at all (the one with more hits in a period wins).

For all that the detection shouldn't be updated. But that is the place where you control everything.
Yes I realize the way I am doing it is not even close to efficient, and I can actually locate everything in the game right now, I am sure in a few months I will have trouble remembering.

I tried your theory for hit detection and it caused massive bugs, it caused the bullets to randomly detect being hit through out the entire screen

I reverted the changes and stumbled upon another discovery if the left gun is at the top of the screen, than it can be hit without movement, but not the same for the right gun. This makes me think that it is my hit detection which is off and that the moving gun just causes the bullet to cover the entire gun and can hit the miniscule hit detection block
Bump, need to finish this game before tuesday for my computer prog class.
I have decided that the Hit Detection box was exactly the size of the bullet, making it nearly impossible to hit the gun unless the gun was in motion or it was a perfect shot. Also, ne555 was right when he was telling me that the hit detection was wrong, but his fixes caused it to bug out because the < > signs were also wrong for the left gun / bullet comming from the right gun. I was woundering where the x and y coords are based from on the picture I.E is it the upper right, left, bottom? etc. Thanks!
Topic archived. No new replies allowed.