I have a question about rectangular collision is as follows.
I am using the following code to the collision and movement.
/* Movimentation */
void key_pressed(int *key, SDL_Rect *move)
{
if(key[SDLK_RIGHT] || collision(player->player_rect, wall->wall_rect))
++move->x;
if(key[SDLK_LEFT] || collision(player->player_rect, wall->wall_rect))
--move->x;
if(key[SDLK_DOWN] || collision(player->player_rect, wall->wall_rect))
++move->y;
if(key[SDLK_UP] || collision(player->player_rect, wall->wall_rect))
--move->y;
}
/* Collision */
int collision(SDL_Rect A, SDL_Rect B)
{
int leftA, leftB;
int rightA, rightB;
int downA, downB;
int upA, upB;
leftA = A.x;
rightA = A.x + A.w;
upA = A.y;
downA = A.y + A.h;
leftB = B.x;
rightB = B.x + B.w;
upB = B.y;
downB = B.y + B.h;
if(downA <= upB)
return 0;
if(upA >= downB)
return 0;
if(rightA <= leftB)
return 0;
if(leftA >= rightB)
return 0;
return 1;
}
The code of collision is the tutorial site lazyfoo.
My question is this, move the code I'm using | | to check if this bumping, but what I do not understand and because of using | |, because, in my view even if it is colliding it will walk, for :
If I press right or is bumped.
He Moves.
With a simple check I found the following cases.
1 - Key pressed (true) and not colliding (false) -> Move the image;
2 - Key pressed (true) and collidindo (true) -> Move the image;
3 - No key pressed (false) and collidindo (true) -> Move the picture because I'm using or evaluating this case and the sentence true.
4 - Key not pressed (false) and not colliding (false) -> Do not move the image.
I'm a little confused I'm not getting right what is wrong with my analysis that a little confusing. This is my question.
