Sign in to follow this  
ICUP

Collision Problems

Recommended Posts

MyImageLink MyImageLink I'm having collision problems. When the asteroid comes down on the left side of the robot, as soon as it hits the robots y-coord it decreases the life point even if it's not touching it.( Image2 ) However, the strange thing is that if the asteroid comes down on the right side it does what it's supposed to do just fine which is go off the screen without decreasing a life points unless it hits the player. (Image1) When the asteroid hits the player the life (nanobot) decreases like it's supposed to. It's the left side I'm having problems with. Even if the asteroid isn't touching the robot, as soon as the y+h coord of the asteroid is greater than the y-coord of the robot it decreases the life (nanobot) but it's not supposed to. (Image2) I'm using lazy foo's collision function as is
 
bool Asteroid::checkCollision( 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;
            
    //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;
}



Share this post


Link to post
Share on other sites
Yeah, the logic is correct...

heh...when I set the rectangle width it was the width of the whole image, not the clipped. That's why it was all funky. Good thing I learned how to use the debugger.

If you're reading this and you don't know how to use the debugger, LEARN IT! So useful.

Thanks for the reply though.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this