# help with collision

This is a discussion on help with collision within the C++ Programming forums, part of the General Programming Boards category; I am making a game and checking collision with the following method: Code: //player variables int player_x; int player_y; int ...

1. ## help with collision

I am making a game and checking collision with the following method:

Code:
```//player variables
int player_x;
int player_y;
int player_x2;
int player_y2;
int player_x3;
int player_y3;
int player_x4;
int player_y4;
int player_width=(player_x2-player_x);
int player_height=(player_y3-player_y2);
//enemy named "imran" variables
float imran_x=100.0+enemy_a;
float imran_y=100.0+enemy_b;
float imran_x2=200.0+enemy_a;
float imran_y2=100.0+enemy_b;
float imran_x3=200.0+enemy_a;
float imran_y3=200.0+enemy_b;
float imran_x4=100.0+enemy_a;
float imran_y4=200.0+enemy_b;
int imran_width=(imran_x2-imran_x);
int imran_height=(imran_y3-imran_y2);
bool collision_enemy(){
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 )
{return true;
cout<<"collision theindi payee\n";}
else
return false;
}```

2. 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
Code:
`if( ( (player_x + player_width) > imran_x) && ( player_x < (imran_x + imran_width) ) && ...`

3. Originally Posted by std10093
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
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.

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.

4. How much can some words make you feel bad?

Anyway

5. Collision is simpler to test for if you eliminate what is impossible. What remains must be true.