Public Group

Lazyfoo Tutorial Question

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

Recommended Posts

Hey Guys, I have a quick question regarding LazyFoo's SDL tutorial #19 if anyone has gone through this before. http://lazyfoo.net/SDL_tutorials/lesson19/index.php Basically, in the following line...
//If no sides from vertical A are outside of B
if( ( ( bottomAv <= topB ) || ( topAv >= bottomB ) || ( rightAv <= leftB ) || ( leftAv >= rightB ) ) == false )
{
//A collision is detected
return true;
}


...lazyFoo describes how to calculate whether a circular object has collided with a rectangular object. Works great and makes sense to me, but, is there a reason why he used the "== false" line in his if statement? Couldn't he have just flipped the greater thans to less thans and vice versa? Maybe I'm missing something huge here. [/source]

Share on other sites
Quote:
 is there a reason why he used the "== false" line in his if statement?

Not really; I wouldn't have written it that way.

Another way to negate the whole thing is to just put a '!' in front:

if( !( ( bottomAv <= topB ) || ( topAv >= bottomB ) || ( rightAv <= leftB ) || ( leftAv >= rightB ) ) )//  ^ there, so that it refers to the entire condition

Quote:
 Couldn't he have just flipped the greater thans to less thans and vice versa?

You would also have to change the ||'s to &&'s.

Share on other sites
Quote:
Original post by Zahlman
Quote:
 is there a reason why he used the "== false" line in his if statement?

Not really; I wouldn't have written it that way.

Another way to negate the whole thing is to just put a '!' in front:

if( !( ( bottomAv <= topB ) || ( topAv >= bottomB ) || ( rightAv <= leftB ) || ( leftAv >= rightB ) ) )//  ^ there, so that it refers to the entire condition

Quote:
 Couldn't he have just flipped the greater thans to less thans and vice versa?

You would also have to change the ||'s to &&'s.

I see. The logical not operator definitely makes it look cleaner. I'm going to use your method instead. Thanks for the tip.

• 10
• 18
• 14
• 18
• 15