help with collision
I am making a game and checking collision with the following method:
//enemy named "imran" variables
if(player_x + player_width > imran_x && player_x < imran_x + imran_width && player_y + player_height > imran_y && player_y < imran_y + imran_width )
cout<<"collision theindi payee\n";}
You are not going to see the output "collision theindi payee" even if you have as condition the number 1 (or any different number from zero).
return true is located before the cout, thus the function will return before cout.
Also, I do not really remember the priority of the + and > operators, but I won't even bother to check. Use parentheses in order to be sure, like this
if( ( (player_x + player_width) > imran_x) && ( player_x < (imran_x + imran_width) ) && ...
Classic case of laziness leading to wasted effort and code that is more fragile: too many unnecessary brackets in expressions tend to make the code harder to understand, and therefore easier to get wrong.
Originally Posted by std10093
Numeric operators are higher precedence than comparison operators. So expressions mean what anyone would expect if they paid even minimal attention during primary school or secondary school mathematics classes.
So "a + b > c" is equivalent to "(a + b) > c", not to "a + (b > c)".
I can understand your caution for mixing (say) pointer operations with incrementing (since people don't learn rules in real life outside programming that are applicable), but not for basic math expressions.
How much can some words make you feel bad?
Collision is simpler to test for if you eliminate what is impossible. What remains must be true.