[SOLVED] Collision Detection Doesn't Work

EDIT: I've downloaded the box2d libary and i'll start using it , i'll follow the tutorials for it on youtube as i saw it has all i need to work properly, so no need to help me anymore , thanks though!



Dear programmers,
I've been gaining education in SDL through lazy foo's tutorial, trail and error and also quite few YouTube videos.
after sometime I've finally arrived to the skill standard which i allowed myself to start programming gravity , collision detection and movement , but after testing it out alot , many errors were made and i just couldn't figure out why, so that's why i'm here, to ask you people for help.

As for the code , i wraped it in lot's of classes to make it look nice and clean
( perhaps for some people ) or just to make it easier for me, anyway , I did program the algorithms for movement , collision , setting and rendering the dot's position according to the movement , setting gravity and etc. but i have an error that i just couldn't make go away, here's the code:

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

#include <SDL.h>
#include <SDL_image.h>
#include <stdio.h>
#include <string>
using namespace std;

const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int gravity = 2;
const int DOT_VEL = 10;
SDL_Window* gWindow;
SDL_Renderer* gRenderer;


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class LTexture // class for loading the dot image and rendering it's position
{
public:
    void loadimage(string path);
    void renderimage(int x , int y);
    int getwidth();
    int getheight();
private:
    SDL_Texture* mTexture;
    int mWidth;
    int mHeight;
}ltext;

void LTexture::loadimage(string path) // once we pass in the image , we wont have to pass it in anymore
{
SDL_Surface* newsurface = IMG_Load(path.c_str());
SDL_Texture* newtexture;
mWidth = newsurface->w;
mHeight = newsurface->h;
SDL_SetColorKey(newsurface,SDL_TRUE,SDL_MapRGB(newsurface->format,0,0xFF,0xFF));
newtexture = SDL_CreateTextureFromSurface(gRenderer,newsurface);
mTexture = newtexture;
}

void LTexture::renderimage(int x , int y)
{
    SDL_Rect renderquad = {x , y , mWidth , mHeight};
    SDL_RenderCopy(gRenderer,mTexture,NULL,&renderquad);
}

int LTexture::getwidth()
{
   return mWidth;
}

int LTexture::getheight()
{
    return mHeight;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////




class DDott // the class for handling the dot's movement events
            // updating it's position and applying the gravity on the dot.
{
public:
    void eventhandle(SDL_Event a); // receiving event from the int main to
                                   // handle the dot's movement.

    void movement(SDL_Rect thewall); // add " wall " inside the brackets later
    int gravitywork(); // function prototype for applying gravity on th edot
    void render(int a,int b);
    DDott();
    int getposx();
    int getposy();
private:
int mvelx , mvely; // the velocities of the dot on the x and y axis
int mposx , mposy; // the position of the dot on the x and y axis
SDL_Rect dotmov; // i warped the dot in a rectangle to make things easier

}mdot;

DDott::DDott() 
{
    mvelx = 0;
    mvely = 0;
    dotmov.x = 0;
    dotmov.y = 0;
    dotmov.w = ltext.getwidth();
    dotmov.h = ltext.getheight();
}

void DDott::eventhandle(SDL_Event e)
{

 if(e.type == SDL_KEYDOWN && e.key.repeat == 0) // if were pressing down a key
 {
     if(e.key.keysym.sym == SDLK_UP)
     {
         mvely = mvely - DOT_VEL;
     }
     if(e.key.keysym.sym == SDLK_DOWN)
     {
         mvely = mvely + DOT_VEL;
     }
     else if(e.key.keysym.sym == SDLK_RIGHT)
     {
         mvelx = mvelx + DOT_VEL;
     }
     else if(e.key.keysym.sym == SDLK_LEFT)
     {
         mvelx = mvelx - DOT_VEL;
     }
 }
 if(e.type == SDL_KEYUP && e.key.repeat == 0) // if a key is released
 {
     if(e.key.keysym.sym == SDLK_UP)
     {
         mvely = mvely + DOT_VEL;
     }

        if(e.key.keysym.sym == SDLK_DOWN)
     {
         mvely = mvely - DOT_VEL;
     }
     else if(e.key.keysym.sym == SDLK_RIGHT)
     {
         mvelx = mvelx - DOT_VEL;
     }
     else if(e.key.keysym.sym == SDLK_LEFT)
     {
         mvelx = mvelx + DOT_VEL;
     }
 }

 }

bool checkCollision( SDL_Rect a, SDL_Rect b ) //the collision checking function
                                              //between "wall" rect and the dot.
{
    //The sides of the rectangles
    int leftA, leftB;
    int rightA, rightB;
    int topA, topB;
    int bottomA, bottomB;

    //Calculate the sides of rect A
    leftA = a.x;
    rightA = a.x + a.w;
    topA = a.y;
    bottomA = a.y + a.h;

    //Calculate the sides of rect B
    leftB = b.x;
    rightB = b.x + b.w;
    topB = b.y;
    bottomB = b.y + b.h;

    //If any of the sides from A are outside of B
    if( bottomA <= topB )
    {
        return false;
    }

    if( topA >= bottomB )
    {
        return false;
    }

    if( rightA <= leftB )
    {
        return false;
    }

    if( leftA >= rightB )
    {
        return false;
    }

    //If none of the sides from A are outside B
      return true;

}

void DDott::movement(SDL_Rect thewall) // "thewall" is inside to allow a pass of 
                                      // this parameter to another function to 
                                       // check for collisions.
{
    dotmov.x = dotmov.x + mvelx;
    if( (dotmov.x< 0) || (dotmov.x + dotmov.w >= (SCREEN_WIDTH - DOT_VEL)) || (checkCollision(dotmov,thewall)) ) // here we are checking the collision if the dot went out of the screen's region and if the ball hit "thewall" rectangle                       
    {
        dotmov.x = dotmov.x - mvelx;
    }
    dotmov.y = dotmov.y + mvely;
    if((dotmov.y < 0 )|| (dotmov.y + dotmov.h > (SCREEN_HEIGHT - 22) )|| (checkCollision(thewall,dotmov)) )
    {
        dotmov.y = dotmov.y - mvely;
    }

}



int DDott::gravitywork() // this is for applying the gravity on the dot
{
    if(dotmov.y + dotmov.h > (SCREEN_HEIGHT - 22))
    {
        dotmov.y = dotmov.y - mvely;
    }
    else
    {
    dotmov.y = dotmov.y + gravity;
    }

}

void DDott::render(int a , int b) // rendering the dot's x and y to the screen.
{
    ltext.renderimage(a , b);
}

int DDott::getposx()
{
    return dotmov.x;
}

int DDott::getposy()
{
    return dotmov.y;
}

void init()
{
    gWindow = SDL_CreateWindow("Collision + movment",SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,SCREEN_WIDTH,SCREEN_HEIGHT,SDL_WINDOW_SHOWN);
    gRenderer = SDL_CreateRenderer(gWindow,-1,SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
    ltext.loadimage("dot.bmp"); // from here we go to the second part in our program the texture class to load the image and create a copy of it
}

int main(int argc, char* argv[])
{
    SDL_Rect wall = {200,420,500,20};
    bool quit = false;
    init();
    SDL_Event e;
    while(quit == false)
    {
       while(SDL_PollEvent(&e)!=0)
       {
        if(e.type == SDL_QUIT)
        {
            quit = true;
        }
        mdot.eventhandle(e);
       }
       mdot.gravitywork();
       mdot.movement(wall); // mwall.y - mwall.h
       SDL_SetRenderDrawColor(gRenderer,0xFF,0xFF,0xFF,0xFF);
       SDL_RenderClear(gRenderer);
       mdot.render(mdot.getposx(),mdot.getposy());
       SDL_SetRenderDrawColor(gRenderer,0xFF,0x00,0x00,0xFF);
       SDL_RenderFillRect(gRenderer,&wall);
       SDL_RenderPresent(gRenderer);
    }
return 0;
}






So, The problem is, I can't seem to get the dot to collide with the rectangle ( which is the red rectangle) but it does collide with the screen's width and height.
when the dot get's to the bottom of the screen it starts flipping out of it if i press the " UP " key when the dot is collided with it, on.


I'm begging you guys to help me out solve these two problems , one of them is to make the dot collide the with red rectangle properly while gravity is on
and the other is so the ball wont fall when i'm pressing on the " up " key when the ball is collided with the screen's height.

Please help me out guys! , i'll really appreciate it! i really want to make a newbie 2d platformer game but this problem is just blocking me :(
Last edited on
Topic archived. No new replies allowed.