confused about collision detection in SDL, i dont understand this snippit of lazt foos tutorial

been learning the lazy foos tutorials, they dont explain so much but theyre better than the sdl documentation, here im stck trying to understand the tut on colision detection, i understand why in my code i can go past the whole area that is greater than 'wall.x' i dont see how lazy foos function for collision detection in his tutorial works in comparison.


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
wall.h=200;
wall.w=200;
wall.x=100;
wall.y=100;


bool quit = false;




while (quit!=true)
{
    if (SDL_PollEvent (&event))
    {
        if (event.type ==SDL_KEYDOWN)
        {
            switch (event.key.keysym.sym)
            {
               case SDLK_UP:velx-=1; break;
               case SDLK_DOWN:velx+=1 ; break;
               case SDLK_LEFT:vely-=1 ; break;
               case SDLK_RIGHT: vely+=1; break;
            }
        }
       else if (event.type ==SDL_KEYUP)
       {
            switch (event.key.keysym.sym)
            {
                 case SDLK_UP: velx+=1; break;
               case SDLK_DOWN: velx-=1; break;
               case SDLK_LEFT: vely+=1; break;
               case SDLK_RIGHT: vely-=1; break;
               case SDLK_ESCAPE: quit=true;break;
            }
       }
     else if (event.type == SDL_QUIT)
         {
             quit = true;
         }
       }

obj1.x = x+=vely;
obj1.y = y+=velx;
obj1.w = 15;
obj1.h = 15;
if ((obj1.x<0)||(obj1.x>SCREEN_WIDTH-obj1.w))
{
    x-=vely;
}
else if (obj1.x >wall.x+wall.w )///here when the + is a - the square doesnt 
///move at all, i really dont get that, as it is i can move the square as far as 
//the end of the wall rect sitting in the middle of my cod, 
{
    x-=vely;
}

SDL_FillRect (screen,NULL, SDL_MapRGB( screen->format, 255, 255, 255));
SDL_FillRect( screen, &wall, SDL_MapRGB( screen->format, 77, 77, 77 ) );
SDL_FillRect(screen,&obj1,SDL_MapRGB( screen->format,0,0,0));

 SDL_Flip (screen);
}



this snippet is messy heres the link instead http://lazyfoo.net/SDL_tutorials/lesson17/index.php, i been stuck trying to see this for a few hours


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
bool check_collision( SDL_Rect A, SDL_Rect B ) {

 //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;


//Here we have the actual function that checks for a collision.

//First thing the function does is take in the SDL_Rects and calculate their //sides.

//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; } 


1
2
3
4
else if (obj1.y+obj1.h>=wall.y )
{
    x-=vely;///doing this is even more unusual!!
}


appreciate the help guys :)
Last edited on
oh my bad i will send the entire working code here

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
#include <sdl/sdl.h>
#include <sdl_image.h>
#include <string>

SDL_Surface *screen=NULL;
SDL_Surface *object=NULL;

SDL_Rect wall,obj1;

const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;


SDL_Event event;

SDL_Surface *load_image( std::string filename )
{
SDL_Surface* loadedImage = NULL;
SDL_Surface* optimizedImage = NULL;
loadedImage = IMG_Load( filename.c_str() );
optimizedImage = SDL_DisplayFormat( loadedImage );
SDL_FreeSurface( loadedImage );
return optimizedImage;
}

void apply_surface(int x,int y, SDL_Surface *source,SDL_Surface *Destination,SDL_Rect *clip=NULL)
{
    SDL_Rect offset;
    offset.x=x;
    offset.y=y;
    SDL_BlitSurface (source,clip,Destination,&offset);
}


int main(int argc,char *args[]){

int x,y,velx,vely;
x=0;
y=0;
velx=0;
vely=0;
SDL_Init(SDL_INIT_EVERYTHING);
screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);
 SDL_WM_SetCaption( "Can I Move the Dot though?", NULL );



wall.h=200;
wall.w=200;
wall.x=100;
wall.y=100;


bool quit = false;




while (quit!=true)
{
    if (SDL_PollEvent (&event))
    {
        if (event.type ==SDL_KEYDOWN)
        {
            switch (event.key.keysym.sym)
            {
               case SDLK_UP:velx-=1; break;
               case SDLK_DOWN:velx+=1 ; break;
               case SDLK_LEFT:vely-=1 ; break;
               case SDLK_RIGHT: vely+=1; break;
            }
        }
       else if (event.type ==SDL_KEYUP)
       {
            switch (event.key.keysym.sym)
            {
                 case SDLK_UP: velx+=1; break;
               case SDLK_DOWN: velx-=1; break;
               case SDLK_LEFT: vely+=1; break;
               case SDLK_RIGHT: vely-=1; break;
               case SDLK_ESCAPE: quit=true;break;
            }
       }
     else if (event.type == SDL_QUIT)
         {
             quit = true;
         }
       }

obj1.x = x+=vely;
obj1.y = y+=velx;
obj1.w = 15;
obj1.h = 15;
if ((obj1.x<0)||(obj1.x>SCREEN_WIDTH+obj1.w))
{
    x-=vely;
}
else if ( )
{
    x-=vely;
}

SDL_FillRect (screen,NULL, SDL_MapRGB( screen->format, 255, 255, 255));
SDL_FillRect( screen, &wall, SDL_MapRGB( screen->format, 77, 77, 77 ) );
SDL_FillRect(screen,&obj1,SDL_MapRGB( screen->format,0,0,0));

 SDL_Flip (screen);
}

return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
    obj1.x = x+=vely;
    obj1.y = y+=velx; 
    obj1.w = 15;
    obj1.h = 15;
    if ((obj1.x<0)||(obj1.x>SCREEN_WIDTH+obj1.w))
    {
        x-=vely;
    }
    else if ( )
    {
        x-=vely;
    }


Why are you adding the y velocity to x and the x velocity to y?

What does else if() mean? Why are you making the same adjustment in both branches of the if/else?

Why are you adding the object width to the screen width?



the whole y x thing got mixed up so i just swapped them round cos i knew what they did even though they werent true to their names :/ the else if is a bit pointless i guess if is just as good, its all for experimentation this code, i plan to get the box to detect at least one edge of the rectangle in the middle, i have no problem understanding the edge detection for the end of the screen, its the collisions with other boxxes thats so confusing
leftA is the x coordinate of the left side of rectangle A.
rightA is the x coordinate of the right side of rectangle A.
topA is the y coordinate of the top side of rectangle A.
bottomA is the y coordinate of the bottom side of rectangle A.

leftB is the x coordinate of the left side of rectangle B.
rightB is the x coordinate of the right side of rectangle B.
topB is the y coordinate of the top side of rectangle B.
bottomB is the y coordinate of the bottom side of rectangle B.

If bottomA is above topB that must mean that rectangle A is above rectangle B and they are not colliding. Think about it. If you don't get it, draw it on paper and you will see that it has to be that way. Since they are not colliding the function should return false in this case and that's what this line is all about:
if( bottomA <= topB ) { return false; }

If topA is below bottomB that means that rectangle A is below rectangle B and they are not colliding so the function should return false:
if( topA >= bottomB ) { return false; }

If rightA is to the left of leftB that means that rectangle A is to the left of rectangle B and they are not colliding so the function should return false:
if( rightA <= leftB ) { return false; }

If leftA is to the right of rightB that means that rectangle A is to the right of rectangle B and they are not colliding so the function should return false:
if( leftA >= rightB ) { return false; }

If none of the above cases is true that must mean that the two rectangles are overlapping so the function should return true in this case:
return true;


The collision detection doesn't really have anything to do with SDL. They use SDL_Rect but what's important is that you have something to represent a rectangle with position and size. If you find other resources that explains collision detection between rectangles in a better way, even if they use a different language, you could still learn from it because the algorithm will work the same no matter what language and libraries are used.
Last edited on





I GET IT NOW :D thanks for going to the trouble of explaining a tutorial, what i didnt understand is that i thought left a and left b statments were to work out if it was touching, i had understood the tut inside out so to speak, i dont like rating answer systems in forums because negative feedback is pointless, but dude, if i could give you 5 stars each time i would, i think i understood most of the other stuff better...cheers mr peter your cool.
Topic archived. No new replies allowed.