Du bist nicht angemeldet.

Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!

Werbeanzeige

multipilz5

Frischling

  • »multipilz5« ist der Autor dieses Themas

Beiträge: 4

Beruf: Keiner (Wunschberuf Programmierer bei einer Spielesoftwarefirma)

  • Private Nachricht senden

1

20.01.2015, 18:56

Kollision bei "Bricks" klappt nicht zwischen Ball und Brick (SDL, OpenGL)

Hallo erst mal,
ich habe mir erst neulich ein paar Videos zur Programmierung mit SDL und OpenGL auf C++ Basis angesehen um ein kleines Spiel (Bricks) zu programmieren. Ich habe ein Fenster, etwa 8 Bricks, die Platte und einen "Ball". Bei dem ersten Brick (im Code brick c) funktionieren alle Abläufe bei einer Kollision zwischen Ball und Brick. Sprich je nach dem von welcher Richtung aus der Ball den Brick trifft, prallt er dem entsprechend ab, der Punktestand wird um eins erhöht und der Brick verschwindet. Bei allen anderen Bricks funktioniert das abprallen nur horizontal.

Ich finde den Fehler nicht :dash: :dash: :dash: und hoffe, dass mir jemand helfen kann.
(Ich bin noch Anfänger in der C++ Programmierung und in der Spieleprogrammierung. Daher ist der Code jetzt wahrscheinlich sehr ineffizient und nicht sehr schön.)

Ich bedanke mich schon im voraus bei jedem der sich diesen Code ansieht, ihn versteht und mir sagen kann wo der Fehler ist, bzw. bei dem der es versucht. :D


C-/C++-Quelltext

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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
//Code zum Kompilieren:g++ Main.cpp -o Main -lSDLmain -lSDL -lGL
/*
Compile command:
g++ -o test main.cpp -lSDLmain -lSDL -lGL
 */
 
// specific headers
#include "SDL/SDL.h"
#include "SDL/SDL_opengl.h"
#include <iostream>
 
bool checkCollision(float Ax, float Ay, float Aw, float Ah, float Bx, float By, float Bw, float Bh){
    if(Ay > By+Bh) return false;
    else if(Ax > Bx+Bw) return false;
    else if(Ax+Aw < Bx) return false;
    else if(Ay+Ah < By) return false;
    else return true;
}
 
 
//start of the program
int main(int argc, char* args[]){
    //initialize SDL
    SDL_Init(SDL_INIT_EVERYTHING);
 
    //Set OpenGL memory usage
    SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
 
 
    //Caption of the window
    SDL_WM_SetCaption("Our first game", NULL);
 
    //Size of the window
    SDL_SetVideoMode(1280,720,32, SDL_OPENGL);
 
    //Specific the clear color
    glClearColor(1,1,1,1); //RED,GREEN,BLUE,ALPHA
 
    //What portion of the screen we will display
    glViewport(0,0,1280,720);
 
    //Shader model - Use this
    glShadeModel(GL_SMOOTH);
 
    //2D rendering
    glMatrixMode(GL_PROJECTION);
 
    //"Save" it
    glLoadIdentity();
 
    //Disable depth checking
    glDisable(GL_DEPTH_TEST);
 
    std::cout << "OpenGL is running\n";
    std::cout << "Main loop has started\n";
 
    //Handles the main loop
    bool isRunning = true;
 
    //For handling with event
    SDL_Event event;
    
    //Struktur Brick
    struct brick {
        float myX;
        float myY;
        float width;
        float hight;
        bool exists;
    };
 
    //Struktur Platte
    struct platte {
        float myX;
        float myY;
        float width;
        float hight;
    };
    
    //Struktur Ball
    struct ball {
        float myX;
        float myY;
        float durchmesser;
        float xbew;
        float ybew;
    };
    
    //Punktestand
    int punkte = 0;
    
    //deklaration Brick
    //Reihe 1
    brick c;
    c.myX = 5;
    c.myY = 5;
    c.width = 60;
    c.hight = 30;
    c.exists = true;
    
    brick d;
    d.myX = c.myX + c.width + 5;
    d.myY = c.myY;
    d.width = 60;
    d.hight = 30;
    d.exists = true;
    
    brick e;
    e.myX = d.myX + d.width + 5;
    e.myY = c.myY;
    e.width = 60;
    e.hight = 30;
    e.exists = true;

    brick f;
    f.myX = e.myX + e.width + 5;
    f.myY = c.myY;
    f.width = 60;
    f.hight = 30;
    f.exists = true;
    
    brick g;
    g.myX = f.myX + f.width + 5;
    g.myY = c.myY;
    g.width = 60;
    g.hight = 30;
    g.exists = true;
    
    brick h;
    h.myX = g.myX + g.width + 5;
    h.myY = c.myY;
    h.width = 60;
    h.hight = 30;
    h.exists = true;
    
    brick i;
    i.myX = h.myX + h.width + 5;
    i.myY = c.myY;
    i.width = 60;
    i.hight = 30;
    i.exists = true;
    
    //deklaration Ball
    ball b;
    b.myX = 50;
    b.myY = 200;
    b.durchmesser = 20;
    b.xbew = 2.0;
    b.ybew = 2.0;
    
    //deklaration Platte
    platte p;
    p.myX = 300;
    p.myY = 666;
    p.width = 80;
    p.hight = 20;
    
    bool left, right = false;
    bool zahl = true;
    
 
    //Main game loop
    while (isRunning){
        
        //EVENTS
        while (SDL_PollEvent(&event)){
            
            //if the window was closed
            if ( event.type == SDL_QUIT ){
                isRunning = false;
            }
 
            //If a button was released and the button is escape
            if ( event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_ESCAPE ){
                isRunning = false;
            }
            
            //rot färben des Bildschirms mit Taste "r"
            if ( event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_r ){
                glClearColor(1,0,0,1);
            }
            
            //Steuerung Platte
            if (event.type == SDL_KEYDOWN){
                if (event.key.keysym.sym == SDLK_LEFT){
                    left = true;
                }
                
                else if (event.key.keysym.sym == SDLK_RIGHT){
                    right = true;
                }
            }
            else if (event.type == SDL_KEYUP){
                if(event.key.keysym.sym == SDLK_LEFT){
                    left = false;
                }
                
                else if(event.key.keysym.sym == SDLK_RIGHT){
                    right = false;
                }
            }
            
            //bugfix (Platte hat von Anfang an den Wert true)
            if(zahl == true){
                left = false;
                zahl = false;
            }
            
            //logic that should happen for a certain event
        }
 
        //LOGIC
        
        //Steuerung Platte
        if(left == true){
            p.myX -= 4.0;
        }
        if(right == true){
            p.myX += 4.0;
        }
        
        //kollision Ball - Brick c
        if(checkCollision(c.myX, c.myY, c.width, c.hight, b.myX, b.myY, b.durchmesser, b.durchmesser) == true){
            if(b.myX > c.myX+c.width-3 && c.exists == true) b.xbew = -b.xbew;
            if(b.myX+b.durchmesser < c.myX+3 && c.exists == true) b.xbew = -b.xbew;
            if(b.myY > c.myX+c.hight-3 && c.exists == true) b.ybew = -b.ybew;
            if(b.myY+b.durchmesser < c.myY+3 && c.exists == true) b.ybew = -b.ybew;
            if(c.exists == true) punkte ++;
            c.exists = false;
        }
        
        //kollision Ball - Brick d
        if(checkCollision(d.myX, d.myY, d.width, d.hight, b.myX, b.myY, b.durchmesser, b.durchmesser) == true){
            if(b.myX > d.myX+d.width-3 && d.exists == true) b.xbew = -b.xbew;
            if(b.myX+b.durchmesser < d.myX+3 && d.exists == true) b.xbew = -b.xbew;
            if(b.myY > d.myX+d.hight-3 && d.exists == true) b.ybew = -b.ybew;
            if(b.myY+b.durchmesser < d.myY+3 && d.exists == true) b.ybew = -b.ybew;
            if(d.exists == true) punkte ++;
            d.exists = false;
        }
        
        //kollision Ball - Brick e
        if(checkCollision(e.myX, e.myY, e.width, e.hight, b.myX, b.myY, b.durchmesser, b.durchmesser) == true){
            if(b.myX > e.myX+e.width-3 && e.exists == true) b.xbew = -b.xbew;
            if(b.myX+b.durchmesser < e.myX+3 && e.exists == true) b.xbew = -b.xbew;
            if(b.myY > e.myX+e.hight-3 && e.exists == true) b.ybew = -b.ybew;
            if(b.myY+b.durchmesser < e.myY+3 && e.exists == true) b.ybew = -b.ybew;
            if(e.exists == true) punkte ++;
            e.exists = false;
        }
        
        //kollision Ball - Brick f
        if(checkCollision(f.myX, f.myY, f.width, f.hight, b.myX, b.myY, b.durchmesser, b.durchmesser) == true){
            if(b.myX > f.myX+f.width-3 && f.exists == true) b.xbew = -b.xbew;
            if(b.myX+b.durchmesser < f.myX+3 && f.exists == true) b.xbew = -b.xbew;
            if(b.myY > f.myX+f.hight-3 && f.exists == true) b.ybew = -b.ybew;
            if(b.myY+b.durchmesser < f.myY+3 && f.exists == true) b.ybew = -b.ybew;
            if(f.exists == true) punkte ++;
            f.exists = false;
        }
        
        //kollision Ball - Brick g
        if(checkCollision(g.myX, g.myY, g.width, g.hight, b.myX, b.myY, b.durchmesser, b.durchmesser) == true){
            if(b.myX > g.myX+g.width-3 && g.exists == true) b.xbew = -b.xbew;
            if(b.myX+b.durchmesser < g.myX+3 && g.exists == true) b.xbew = -b.xbew;
            if(b.myY > g.myX+g.hight-3 && g.exists == true) b.ybew = -b.ybew;
            if(b.myY+b.durchmesser < g.myY+3 && g.exists == true) b.ybew = -b.ybew;
            if(g.exists == true) punkte ++;
            g.exists = false;
        }
        
        //kollision Ball - Brick h
        if(checkCollision(h.myX, h.myY, h.width, h.hight, b.myX, b.myY, b.durchmesser, b.durchmesser) == true){
            if(b.myX > h.myX+h.width-3 && h.exists == true) b.xbew = -b.xbew;
            if(b.myX+b.durchmesser < h.myX+3 && h.exists == true) b.xbew = -b.xbew;
            if(b.myY > h.myX+h.hight-3 && h.exists == true) b.ybew = -b.ybew;
            if(b.myY+b.durchmesser < h.myY+3 && h.exists == true) b.ybew = -b.ybew;
            if(h.exists == true) punkte ++;
            h.exists = false;
        }
        
        //kollision Ball - Brick i
        if(checkCollision(i.myX, i.myY, i.width, i.hight, b.myX, b.myY, b.durchmesser, b.durchmesser) == true){
            if(b.myX > i.myX+i.width-3 && i.exists == true) b.xbew = -b.xbew;
            if(b.myX+b.durchmesser < i.myX+3 && i.exists == true) b.xbew = -b.xbew;
            if(b.myY > i.myX+i.hight-3 && i.exists == true) b.ybew = -b.ybew;
            if(b.myY+b.durchmesser < i.myY+3 && i.exists == true) b.ybew = -b.ybew;
            if(i.exists == true) punkte ++;
            i.exists = false;
        }
        
        //kollision Ball - Platte
        if(b.myY+b.durchmesser > p.myY){
            if(b.myX < p.myX+p.width){
                if(b.myX+b.durchmesser > p.myX){
                    if(b.ybew > 0){
                        b.ybew = -b.ybew;
                    }
                }
            }
        }

        //kollision Ball - Wände
        if(b.myY+b.durchmesser > 686){
            isRunning = false;
        }
        
        if(b.myY < 0){
            b.ybew = -b.ybew;
        }
        
        if(b.myX < 0){
            b.xbew = -b.xbew;
        }
        
        if(b.myX+b.durchmesser > 1024){
            b.xbew = -b.xbew;
        }
        
        //kollision Platte - Wände
        if(p.myX < 0){
            p.myX = 0;
        }
        
        if(p.myX > 1024-p.width){
            p.myX = 1024-p.width;
        }
        
        //logik der Bewegung des Balls
        b.myX += b.xbew;
        b.myY += b.ybew;
 
        //RENDERING to the screen
        glClear(GL_COLOR_BUFFER_BIT);
 
        glPushMatrix(); //Start rendering phase

        glOrtho(0,1024,686,0,-1,1); //Set the matrix
        
        glColor4ub(0,0,0,255);
        
        //erstellen der Platte
        glBegin(GL_QUADS);
        
        glVertex2f(p.myX, p.myY);
        glVertex2f(p.myX+p.width, p.myY);
        glVertex2f(p.myX+p.width, p.myY+p.hight);
        glVertex2f(p.myX, p.myY+p.hight);
        
        glEnd();

        //erstellen des Balls
        glBegin(GL_QUADS);
                
        glColor4ub(255,0,0,255);
        
        glVertex2f(b.myX, b.myY);
        glVertex2f(b.myX+b.durchmesser, b.myY);
        glVertex2f(b.myX+b.durchmesser, b.myY+b.durchmesser);
        glVertex2f(b.myX, b.myY+b.durchmesser);
        
        glEnd();
        
        if(c.exists == true){
            //erstellen Brick c
            glBegin(GL_QUADS);
            
            glColor4ub(0,255,0,255);
            
            glVertex2f(c.myX, c.myY);
            glVertex2f(c.myX+c.width, c.myY);
            glVertex2f(c.myX+c.width, c.myY+c.hight);
            glVertex2f(c.myX, c.myY+c.hight);
            
            glEnd();
        }
        
        if(d.exists == true){
            //erstellen Brick d
            glBegin(GL_QUADS);
            
            glColor4ub(0,255,0,255);
            
            glVertex2f(d.myX, d.myY);
            glVertex2f(d.myX+d.width, d.myY);
            glVertex2f(d.myX+d.width, d.myY+d.hight);
            glVertex2f(d.myX, d.myY+d.hight);
            
            glEnd();
        }

        if(e.exists == true){
            //erstellen Brick e
            glBegin(GL_QUADS);
            
            glColor4ub(0,255,0,255);
            
            glVertex2f(e.myX, e.myY);
            glVertex2f(e.myX+e.width, e.myY);
            glVertex2f(e.myX+e.width, e.myY+e.hight);
            glVertex2f(e.myX, e.myY+e.hight);
            
            glEnd();
        }

        if(f.exists == true){
            //erstellen Brick f
            glBegin(GL_QUADS);
            
            glColor4ub(0,255,0,255);
            
            glVertex2f(f.myX, f.myY);
            glVertex2f(f.myX+f.width, f.myY);
            glVertex2f(f.myX+f.width, f.myY+f.hight);
            glVertex2f(f.myX, f.myY+f.hight);
            
            glEnd();
        }

        if(g.exists == true){
            //erstellen Brick g
            glBegin(GL_QUADS);
            
            glColor4ub(0,255,0,255);
            
            glVertex2f(g.myX, g.myY);
            glVertex2f(g.myX+g.width, g.myY);
            glVertex2f(g.myX+g.width, g.myY+g.hight);
            glVertex2f(g.myX, g.myY+g.hight);
            
            glEnd();
        }

        if(h.exists == true){
            //erstellen Brick h
            glBegin(GL_QUADS);
            
            glColor4ub(0,255,0,255);
            
            glVertex2f(h.myX, h.myY);
            glVertex2f(h.myX+h.width, h.myY);
            glVertex2f(h.myX+h.width, h.myY+h.hight);
            glVertex2f(h.myX, h.myY+h.hight);
            
            glEnd();
        }

        if(i.exists == true){
            //erstellen Brick i
            glBegin(GL_QUADS);
            
            glColor4ub(0,255,0,255);
            
            glVertex2f(i.myX, i.myY);
            glVertex2f(i.myX+i.width, i.myY);
            glVertex2f(i.myX+i.width, i.myY+i.hight);
            glVertex2f(i.myX, i.myY+i.hight);
            
            glEnd();
        }

        glPopMatrix(); //End rendering phase
 
        SDL_GL_SwapBuffers();
        
        SDL_Delay(1);
    }
 
    SDL_Quit();
    
    std::cout << punkte << std::endl;
 
    return 0;
}

BlueCobold

Community-Fossil

Beiträge: 10 738

Beruf: Teamleiter Mobile Applikationen & Senior Software Engineer

  • Private Nachricht senden

2

21.01.2015, 10:33

Cursor in die entsprechende Zeile stellen.
F9 drücken.
F5 drücken.
Spielen und den Zustand herbeiführen -> Programm hält an
Mit F10 die aktuelle Zeile ausführen, mit der Maus oder dem Inspect-View die Werte von Variablen auswerten.
Analysieren was nicht stimmt und warum welche Zeile ausgeführt wird.

Viel Spaß mit dem Debugger, dem wichtigsten Werkzeug gleich nach dem Compiler und dem eigenen Kopf.
Teamleiter von Rickety Racquet (ehemals das "Foren-Projekt") und von Marble Theory

Willkommen auf SPPRO, auch dir wird man zu Unity oder zur Unreal-Engine raten, ganz bestimmt.[/Sarkasmus]

DeKugelschieber

Community-Fossil

Beiträge: 2 641

Wohnort: Rheda-Wiedenbrück

Beruf: Software-Entwickler

  • Private Nachricht senden

3

21.01.2015, 15:23

Jop bitte erstmal den Code weiter zerlegen, in Klassen packen und besser strukturieren, Dopplungen rausnehmen und wiederverwertbaren Code schreiben. Dann wirst du auch eine Methode schreiben (hat SDL sowas nicht schon?) die dir die Kollision zwischen Kreis <-> Rechteck zurückgibt. Dann brauchen wir uns nur das angucken, falls du dann noch Hilfe brauchst.

multipilz5

Frischling

  • »multipilz5« ist der Autor dieses Themas

Beiträge: 4

Beruf: Keiner (Wunschberuf Programmierer bei einer Spielesoftwarefirma)

  • Private Nachricht senden

4

21.01.2015, 21:59

Danke für die Tipps. :)

Werbeanzeige