Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length

 
Advanced search

1075928 Posts in 44152 Topics- by 36120 Members - Latest Member: Royalhandstudios

December 29, 2014, 03:57:48 PM
TIGSource ForumsDeveloperTechnical (Moderators: Glaiel-Gamer, ThemsAllTook)Rectangular Collision.
Pages: [1]
Print
Author Topic: Rectangular Collision.  (Read 492 times)
Carlos_PRO
Guest
« on: July 22, 2012, 09:08:05 AM »

I have a question about rectangular collision is as follows.
I am using the following code to the collision and movement.
Code:
/* 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. Waaagh!
Logged
Eigen
Level 10
*****


Jebus backups.


View Profile WWW
« Reply #1 on: July 22, 2012, 09:22:27 AM »

I usually handle collision differently.

1. You store the object's current position.
2. You move the object according to input.
3. You then check if the object collides with anything.
     If it does, you go back to the position stored in step 1
     If it does not, you do nothing and go on.

That way you will never get stuck.

Your conditions are quite weird. If the object collides with something and you don't press any keys, you first increase x, then decrease x, then increase y and then decrease y. It doesn't move.
« Last Edit: July 22, 2012, 09:28:23 AM by Eigen » Logged

Carlos_PRO
Guest
« Reply #2 on: July 22, 2012, 02:45:53 PM »

This piece of code I got in the collision lazyfoo, but the problem and I do not understand why the collision only works right with | |. This is the point I'm not understanding.
Logged
Eigen
Level 10
*****


Jebus backups.


View Profile WWW
« Reply #3 on: July 22, 2012, 09:43:46 PM »

That's because you don't actually check for collisions. You check if either left or right key is pressed and then move.

|| is an OR operator. This OR that. In your case it's returning true for key[SDLK_RIGHT] and the x is increased. It doesn't matter what collision(player->player_rect, wall->wall_rect) returns.
Logged

RandyGaul
Level 0
***

~~~


View Profile WWW
« Reply #4 on: July 23, 2012, 07:59:55 AM »

Rect to Rect collision check:
Code:
// Given rectangles A and B
if(leftA > rightB) or
if(leftB > rightA) or
if(topA < bottomB) or
if(topB < bottomA)
then no collision

So if all these conditions fail there is a collision. If you use || (or operator) between each check then the only way they'll return 0 is if each one returns 0. You'd understand this better if you knew what the comparison operators do.

The comparison operators like &&, ||, !, >, <, >=, <= all return 1 or 0 depending on what is to the left and to the right of them. With ||, it will return 1 if what is on its left or right is 1, and only returns 0 if both sides are zero.

So to set up your rectangular collisions you can use or:
Code:
return (leftA >= rightB || leftB >= rightA || topA <= bottomB || topB <= bottomA) ? FALSE : TRUE;

Knowing that "if all these conditions fail there is a collision" it sounds like you could also do:
Code:
// Given rectangles A and B
if!(leftA > rightB) and
if!(leftB > rightA) and
if!(topA < bottomB) and
if!(topB < bottomA)
then no collision
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic