# && vs || in collision detection

Hi everyone, first post here.

I was wandering about the section of the code in which we set the conditions for the collision detection. My thought process is that if 1 of the 4 conditions is fulfilled, the collision happens and to be honest, is what seems to be to happen on practice. However, the code states the following:

collision_with_axe =
(b_axe_y >= u_circle_y) &&
(u_axe_y <= b_circle_y) &&
(r_axe_x >= l_circle_x) &&
(l_axe_x <= r_circle_x);

I have little experience with coding, so forgive me if I am wrong, but I have a basic understanding of what’s happening because of the aforementioned. So, my logic was to use the OR operator (||) in this situation, which with my testing seems to be the correct choice (in terms of the logic): as soon as I touch just 1 corner of the axe, the game over is triggered (I lowered the framerate to test it out). The actual code gets this job done of course; however, the logic seems odd.

Given that we are using the AND (&&) operator for all 4 conditions, this should mean ALL 4 conditions should happen AT THE SAME TIME in order for the collision to work, but in the end, it works as soon as ONE of the conditions happens. Is this a C++ thing or am I missing the mark completely here?

Welcome to the community!
You posted this on blender forum There might be some kind of bug, dunno. Prolly better to post on correct subforum

You are generally correct, but if you test for collision you are likely to find that you dont really need to hit the circle to get the collision triggered. So we can see, that first condition checks if y value of Axe is greater than y value of circle. And this is true when they meet (collide) but its also true if Axe is higher than bottom od circle, but i’m completely different left- right position.
If you use || operator you will get true here. With all operators as &&, you are checking for every possibility od x.y coordinates overlap.

Hope this helps

I will move this to ‘Other Courses’ as there seems similar secondary tags in that section and posts about collision detection too! Example.

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.