So I'm doing a 3D implementation of collision detection.
Considering
- Boxes are oriented on the axis.
- They are rotatable but has to be orientated on the axis.
- Box A and Box B cannot go through each other. However; one of there sides can have similar (x,y,z) points.
Here is a visual aid of what SHOULD happen.
COLLIDING(Examples in RED BOX) ------------------- NOT COLLIDING(Examples in GREEN BOX)
However, example 1 and Example 3 in the GREEN box are considered as collision when I don't want them to be. I want them to be considered not colliding.
Here is my code so far.
if((xMax1>=xMin2)||(xMin1<=xMin2)){
if((yMax1>=yMin2)||(yMin1<=yMin2)){
if((zMax1>=zMin2)||(zMin1<=zMin2)){
isSeperate = false;
}
}
}
What is a conditional that I should add to the code for my issue to be solved.
2 Answers 2
It looks like you don't want >=
and <=
, because then touching boxes are considered colliding. Use >
and <
instead.
-
But then again, Will this let my first and third GREEN box examples to work?AMDee– AMDee2015年07月10日 14:37:29 +00:00Commented Jul 10, 2015 at 14:37
-
@AMDee: you can easily try. The first thing I'd do would be to encode the five cases displayed as test cases, to constantly check my code against. Get (more) comfortable with a testing framework, e.g. JUnit.9000– 90002015年07月10日 15:19:10 +00:00Commented Jul 10, 2015 at 15:19
There are so many different cases to consider when developing collision detection that it can get confusing. I'd recommend implementing the Separating Axis Theorem(SAT) to detect collisions between convex objects. It takes into account position and rotation of objects and lets you handle the collisions. You can take a look at this video I made a few months ago of my implementation of it in C# for 2D shapes: https://youtu.be/VOBSijUuBgc
This will also work for 3D shapes it just needs to take into account the z-axis.