I am placing shapes on a grid and seeing if they touch each other. I had some issues with the collisions so I decided to test it with 2 squares that are both 1 units tall and wide. I placed them next to each other, but not overlapping and when I checked Collider1.IsTouching(Collider2), I noticed that there was a collision even if they were separated (1.02 difference in X position still caused a collision). I am also getting same results with OverlapCollider. Are the distances getting too small for a more accurate collision check? Is there some handy way to make sure there is no collision if the colliders are right next to each other but not overlapping?
The physics engine uses a contact offset to keep things stable as defined by Physics2D.defaultContactOffset for polygon collisions. It’s also NOT 1.02 difference so not sure where that’s coming from.
The small value is supposed to be visually insignificant but numerically significant for the physics system to keep things stable. Be cautious if you’re considering reducing this for visuals. It typically means you’re looking at things in the wrong scales. The default is extremely tiny.
In the end, the rigidbody physics engine is about rigidbody physics and isn’t there as a pure geometric intersection utility. If you want to check overlaps of rectangles (etc) then there’s much easier methods.
Thanks for the answer Melv! The 1.02 came from the positions of these two rectangles being (-3.52, -3.5) & (-2.5,-3.5) so their centers being 1.02 units away from each other - I guess it would have been more correct to say that they are 0.02 units way from each other.
Did you have some specific methods in mind that I should take a look at for geometric intersection? Is there some method that could check intersection for varying kinds of polygons? I haven’t defined all the shapes yet, but they won’t all be rectangles.
Not specifically no but there isn’t a solution without knowing exactly what the problem is and how it’s being used it.
Feel free to reduce the contact offset but test thoroughly. In the end, it should be visually insignificant. Maybe reducing the size of the colliders relative to any visuals if it’s an issue.
The IsTouching is returning true though because it’s seen contacts that the physics system produced on the last simulation step so the physics system considers them touching.
I also figured reducing the size of the colliders might be a suitable solution for my use case. Thanks for the help!
