I’m trying to get the vertical contact by box casting in the up direction and filtering by normals using ContactFilter2D to avoid contacts that doesn’t have a pointing down normal.
The problem is the result is inconsistent. It looks like the box cast hits EdgeCollider2D vertices even is their normal shouldn’t be -y.
I don’t know if I’m misunderstanding the EdgeCollider2D/BoxCast behavior or if it’s a bug.
To demonstrate the problem i’ve set up this example:
Unity 2020.3.25:
Box cast origin is the green box.
Box cast result is the yellow box.
Box cast result point is the magenta sphere.
Box cast result normal is the magenta line.
Contact filter:
UpContactFilter.SetNormalAngle(260f, 280f);
On the first screenshot the result is ok, but if I create a new vertice on the left edge the result of the box cast change even if it shouldn’t (from what I understood)
Could you describe or annotate the images, it’s really not clear what you’re showing and I’m not following. To be clear, I know you described some of it but I don’t get why there’s a green line on the left that isn’t hit.
All I can say is your contact filter is set to only return downwards normals.
In the end, none of these kinds of problems are related to a specific collider. A query result ends up with a hit and then the filter is used to filter it but it’s done way outside of any specific collider. Also, it’s done way outside of any collider at all and operates on the lowest level primitives of which there are only 4: Edge, Capsule, Circle & Polygon.
Ah so you assume the edge normals are only the line segments. Their vertices also can be contacted if you start in an overlap. You won’t get this if you don’t start in an overlap.
Thank you for your answer.
As you said, I was assuming that the normal was orthogonal to the line segment even for the vertice case.
It looks like when there is a collision with a vertice the normal will be minus the direction of the cast, is that correct?
Is there a way to avoid/filter this kind of case or should i separate vertical and horizontal colliders?
To make it clear:
Green box is the box used to box cast (upward cast)
Yellow box is the box cast result box
Magenta sphere is the RaycastHit2D.point
Magenta line is the RaycastHit2D.normal
Green Line is a EdgeCollider2D collider
You can ignore the red box/spheres
One more screenshot:
I was assuming that the actual collision would be filtered, and it would hit the horizontal edge
Maybe this can be useful for people that will read this thread later: One segment should be seen as a line, but more like a really small box and so the vertice collision make more sense. It’s more visible/understandable when when you adjust the edge radius:
It would normally but you’re starting in essentially a “degenerate” position (at least from the physics POV) where you’re overlapping the edges already. If you were to do an overlap check you’d return the t=0 overlap result but as all shape casts work, they ignore an initial overlap.