SDL collision

The code works fine but the thing that i find confusing is the collision function
In line 7: if square's y-axis is greater than the walls y-axis + the wall's height shouldn't there be collision but it returns 0 and still works same is the case for line 9.

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
 //http://www.youtube.com/user/thecplusplusguy
//Thanks for the typed in code to Tapit85
#include <SDL.h>
 
bool collision(SDL_Rect* square,SDL_Rect* wall)
{
        if(square->y >= wall->y + wall->h)
                return 0;
        if(square->x >= wall->x + wall->w)
                return 0;
        if(square->y + square->h <= wall->y)
                return 0;
        if(square->x + square->w <= wall->x)
                return 0;
        return 1;
}
 
 
 
 
int width = 640;
int height = 480;
 
int main(int argc, char** argv)
{
        SDL_Init(SDL_INIT_EVERYTHING);
        SDL_Surface *screen;
        screen = SDL_SetVideoMode(width, height, 32, SDL_SWSURFACE);
        bool running = true;
        const int FPS = 30;
        Uint32 start;
        bool b[4] = {0,0,0,0};
        SDL_Rect rect;
        rect.x = 10;
        rect.y = 10;
        rect.w = 20;
        rect.h = 20;
        SDL_Rect recarr[5];
        recarr[0].x=70;
        recarr[0].y=0;
        recarr[0].h=200;
        recarr[0].w=10;
 
        recarr[1].x=300;
        recarr[1].y=200;
        recarr[1].h=20;
        recarr[1].w=80;
 
        recarr[2].x=200;
        recarr[2].y=100;
        recarr[2].h=100;
        recarr[2].w=10;
 
        recarr[3].x=40;
        recarr[3].y=300;
        recarr[3].h=20;
        recarr[3].w=60;
 
        recarr[4].x=100;
        recarr[4].y=0;
        recarr[4].h=200;
        recarr[4].w=10;
 
        Uint32 color2 = SDL_MapRGB(screen->format, 0xff,0xff,0xff);
        Uint32 color = SDL_MapRGB(screen->format, 0,0,0);
        for(int i = 0; i < 5; i += 1) {
                        SDL_FillRect(screen,&recarr[i],color2);
        }
        while(running) {
                start = SDL_GetTicks();
                SDL_Event event;
                while(SDL_PollEvent(&event)) {
                        switch(event.type) {
                                case SDL_QUIT:
                                        running = false;
                                        break;
                                case SDL_KEYDOWN:
                                        switch(event.key.keysym.sym) {
                                                case SDLK_UP:
                                                        b[0] = 1;
                                                        break;
                                                case SDLK_LEFT:
                                                        b[1] = 1;
                                                        break;
                                                case SDLK_DOWN:
                                                        b[2] = 1;
                                                        break;
                                                case SDLK_RIGHT:
                                                        b[3] = 1;
                                                        break;
                                       
                                        }
                                        break;
                                case SDL_KEYUP:
                                        switch(event.key.keysym.sym) {
                                                case SDLK_UP:
                                                        b[0] = 0;
                                                        break;
                                                case SDLK_LEFT:
                                                        b[1] = 0;
                                                        break;
                                                case SDLK_DOWN:
                                                        b[2] = 0;
                                                        break;
                                                case SDLK_RIGHT:
                                                        b[3] = 0;
                                                        break;
                                       
                                        }
                                        break;
                        }
                }
 
                SDL_FillRect(screen, &rect, color);
 
                //logic
                if(b[0]) {
                        rect.y--;
                        for(int i = 0; i < 5; i += 1)
                                if(collision(&rect,&recarr[i]))
                                        rect.y++;
                }
                if(b[1]) {
                        rect.x--;
                        for(int i = 0; i < 5; i += 1)
                                if(collision(&rect,&recarr[i]))
                                        rect.x++;
                }
                if(b[2]) {
                        rect.y++;
                        for(int i = 0; i < 5; i += 1)
                                if(collision(&rect,&recarr[i]))
                                        rect.y--;
                }
                if(b[3]) {
                        rect.x++;
                        for(int i = 0; i < 5; i += 1)
                                if(collision(&rect,&recarr[i]))
                                        rect.x--;
                }
 
 
                //render
                SDL_FillRect(screen, &rect, color2);
                SDL_Flip(screen);
 
                if(1000/FPS > SDL_GetTicks()-start) {
                        SDL_Delay(1000/FPS-(SDL_GetTicks()-start));
                }
        }
        SDL_Quit();
        return 0;
}

Last edited on
Topic archived. No new replies allowed.