I’m… not sure how 1 line became 4…
That doesnt make a lot of sense…
What I said was:
That’s it. The full check.
What you have written is:
if((startA <= startB) && (startA >= startB)) OR
if((startA >= endB) && (startA <= endB)) OR
if((endA <= startB) && (endA >= startB)) OR
if((endA >= endB) && (endA <= endB))
For the moment I will ignore the idea of putting multiple IF statements back to back tacked together with OR’s…
Consider one of your clauses.
(startA <= startB) && (startA >= startB)
Does that make sense? Can startA be both less than and greater than startB at the same time? Can ANY of those clauses be simultaneously true? Only if they were exactly the same.
You’ve invented a collision detector - but it only detects things that meet each other at an edge.
Practical Example
Consider a set of conditions:
A is a span from 1-3.
B is a span from 2-4.
Your logical brain determines they collide. We can draw that out:
But your code doesnt -
StartA is 1, EndA is 3;
StartB is 2, EndB is 4.
if((startA <= startB) && (startA >= startB)) OR
if((startA >= endB) && (startA <= endB)) OR
if((endA <= startB) && (endA >= startB)) OR
if((endA >= endB) && (endA <= endB))
=>
if((1 <= 2) && (1 >= 2)) //true && false = false.
if((1 >= 4) && (1 <= 4)) //false && true = false.
if((3 <= 2) && (3 >= 2)) //false && true = false.
if((3 >= 4) && (3 <= 4)) //false && true = false.
false OR false OR false OR false is… false. Your code says there is no collision.
Now lets consider what was said in post #5; both versions:
if (max(startA,startB) < min(endA,endB)) //substitute..
if (max(1,2) < min(3,4)) //simplify the functions...
if (2 < 3) //true. There was a collision.
or, the other version:
if((startA <= endB) && (endA >= startB)) //substitute.
if((1 <= 4) && (3 >= 2)) // Resolve inequalities.
if(true && true) // Resolve join.
true //there was a collision.
Proof by exhaustion
So there’s other possible conditions: A is wholly before B, A is wholly after B, or that they border one another.
A before B.
StartA = 1
EndA = 3
StartB = 6
EndB = 8
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
A |
A |
A |
|
|
|
|
|
|
|
|
|
|
B |
B |
B |
if((startA <= endB) && (endA >= startB)) //substitute.
if((1 <= 8) && (3 >= 6)) // Resolve inequalities.
if(true && false) // Resolve join.
false //there was not a collision.
A after B.
StartA = 6
EndA = 8
StartB = 1
EndB = 3
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
|
|
|
|
|
A |
A |
A |
B |
B |
B |
|
|
|
|
|
if((startA <= endB) && (endA >= startB)) //substitute.
if((6 <= 3) && (8 >= 1)) // Resolve inequalities.
if(false && true) // Resolve join.
false //there was not a collision.
A collides with B at the boundary
StartA = 3
EndA = 5
StartB = 1
EndB = 3
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
|
|
A |
A |
A |
|
|
|
B |
B |
B |
|
|
|
|
|
if((startA <= endB) && (endA >= startB)) //substitute.
if((3 <= 3) && (5 >= 1)) // Resolve inequalities.
if(true && true) // Resolve join.
true //there was a collision.
(You can prove the opposite end yourself if you want; suffice it to say, this is where the “=” part becomes important)