Public Group

# Collision Problems

This topic is 3536 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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 on other sites
The logic looks correct. Did you try setting a breakpoint on the return true line?

##### 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.

1. 1
2. 2
3. 3
Rutin
16
4. 4
5. 5

• 10
• 11
• 14
• 10
• 25
• ### Forum Statistics

• Total Topics
632652
• Total Posts
3007651
• ### Who's Online (See full list)

There are no registered users currently online

×