Collision Handling Issues - Bouncy Walls

I'm working on a game where a player moves and interacts with a wall. I want the player to be able to smoothly stop on the wall (not bounce) and be able to slide along it.

The way my code is written right now the player will bounce off walls, can not slide, and has a chance of getting caught inside the wall and inverting his movements (up is down, left is right, etc.).

Someone mentioned to me that I need to check the collision for the player's previous position(?) If you guys agree, how should I change the code to implement that?

Collision Detecting Function

1
2
3
4
5
6
7
8
9
10
11
12
bool GameSys::check_collision( SDL_Rect A, SDL_Rect B )
{
    if ( ( A.y <= ( B.y + B.h ) ) &&
         ( ( A.y + A.h ) >= B.y ) &&
         ( A.x <= ( B.x + B.w ) ) &&
         ( ( A.x + A.w ) >= B.x ) )
    {
        return true;
    }

    return false;
}


Transfer Collisions to Player Class

1
2
3
4
void Player::detect_collision( bool co )
{
    collision = co;
}


Player Movement Function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void Player::move_player()
{
    if ( plyr_co_box.x <= 0 || ( plyr_co_box.x + plyr_co_box.w ) >= 640 || collision == true )
    {
        plyr_co_box.x -= plyr_speedX;
    }
    else
    {
        plyr_co_box.x += plyr_speedX;
    }

    if ( plyr_co_box.y <= 0  || ( plyr_co_box.y + plyr_co_box.h ) >= 480 || collision == true )
    {
        plyr_co_box.y -= plyr_speedY;
    }
    else
    {
        plyr_co_box.y += plyr_speedY;
    }

    plyr_to.x = plyr_co_box.x;
    plyr_to.y = plyr_co_box.y;
}
Someone mentioned to me that I need to check the collision for the player's previous position(?)


No. Someone mentioned you couldn't treat a collision as if you were going out of bounds in the current frame, because the collision didn't happen in that frame - it happened in the previous one, so not moving the player doesn't avoid a collision. It just keeps you in the same place the collision occurred.

So, the first thing to do is to stop treating it the same way, and move the player. Your code hasn't changed since the last time you posted it. You don't supply enough info for anyone to determine how to accomplish what you want, therefore you should analyze your code and figure out how to achieve your goal.
Topic archived. No new replies allowed.