ong ding chao
ong ding chao

Reputation: 29

SDL collision detection

i'm currently making a very simple Sprite to wall collision detection program, my stickman will stop at the walls if it is touching it. My code runs fine, but the problem is my stickman sprite will not stop at the left side and top of the wall(Which in this case is the screen height and screen width for left and top only). Here is my code , can you figure out where the problem is, and how do i solve it.

bool check_CollisionWall(oSprite* Sprite1)
{    
    //top left
    if(Sprite1->m_vPosition.x <= 0 && Sprite1->m_vPosition.x + Sprite1->m_nFrameWidth >= SCREEN_WIDTH)
    {
        if(Sprite1->m_vPosition.y <= 0 &&  Sprite1->m_vPosition.y + Sprite1->m_nFrameHeight >= SCREEN_HEIGHT)
        {
            return true;
        }
    }

    //top right
    if(Sprite1->m_vPosition.x+Sprite1->m_nFrameWidth > 0 && Sprite1->m_vPosition.x+Sprite1->m_nFrameWidth >= SCREEN_WIDTH)
    {
        if(Sprite1->m_vPosition.y <= 0 &&  Sprite1->m_vPosition.y+Sprite1->m_nFrameHeight >= SCREEN_HEIGHT)
        {
            return true;
        }
    }

    //bottom left
    if( Sprite1->m_vPosition.x > 0 && Sprite1->m_vPosition.x + Sprite1->m_nFrameWidth <= SCREEN_WIDTH )
    {
        if( Sprite1->m_vPosition.y + Sprite1->m_nFrameHeight > 0 && Sprite1->m_vPosition.y + Sprite1->m_nFrameHeight <= SCREEN_HEIGHT )
        {
            return true;
        }
    }

    //bottom right
    if(Sprite1->m_vPosition.x + Sprite1->m_nFrameWidth > 0 && Sprite1->m_vPosition.x + Sprite1->m_nFrameWidth <= SCREEN_WIDTH)
    {
        if(Sprite1->m_vPosition.y + Sprite1->m_nFrameHeight > 0 &&  Sprite1->m_vPosition.y + Sprite1->m_nFrameHeight <= SCREEN_HEIGHT)
        {
            return true;
        }
    }

    return false;
}

Upvotes: 1

Views: 1875

Answers (2)

vladimirm
vladimirm

Reputation: 261

This should fix your problem, the checks for collision are disjunctive, i.e they are independent. So in essence this checks for left collision OR right, and returns true if either is true, same for y direction.

bool check_CollisionWall(oSprite* Sprite1)
{  

//left or right
if(Sprite1->m_vPosition.x <= 0 || Sprite1->m_vPosition.x + Sprite1->m_nFrameWidth >= SCREEN_WIDTH)
{
    return true;
}

//top or bottom
if(Sprite1->m_vPosition.y <= 0 ||  Sprite1->m_vPosition.y + Sprite1->m_nFrameHeight >= SCREEN_HEIGHT)
{
    return true;
}

return false;
}

Upvotes: 2

Talha Sayed
Talha Sayed

Reputation: 2259

You just need to check collision with the left, top, right and bottom walls. There is no left-top, bottom right combinations to check for.

There will be 4 simple checks.

  1. Left wall collision - check if left side of sprite <=0
  2. Right wall collision - check if x coord + width of sprite is >= right wall index.
  3. & 4. Similar to the left and right wall logic just use it for top and bottom.

These will be 4 independent if statements. There is no need for nested if's the way I see it.

Even if one of them returns positive, you have a collision and the function should return true.

Upvotes: 0

Related Questions