- Home /
Physics2D.OverlapArea inaccurate?
In my scene there are two GameObjects. Both with a BoxCollider2D. The left one has a script attached which uses the Physics2D.OverlapArea method to find collisions between the two objects. As parameter of the method I use the values of bounds of the BoxCollider2D. I log the maximum X of the collider (the right edge). If there is a collision I also log the minimum X (the left edge) of the other collider. You can see in the log that there is an overlapping recognized with an X-value of the right edge of the left object smaller than the left edge of the right object. How is that possible? The two colliders should not overlap.
In the screenshot you can see the small gap.
The overlapping code:
void Update() {
Collider2D result = Physics2D.OverlapArea(collider2D.bounds.min, collider2D.bounds.max, collidingLayer);
Debug.Log("Player maxX: " + collider2D.bounds.max.x);
if (result != null) {
Debug.Log("Block minX: " + result.bounds.min.x);
}
}
"The left one has a script attached which uses the Physics2D.OverlapArea method to find collisions between the two objects"
why? You have colliders. Why not just have them collide? The collision is already happening in the physics engine, so you're recalculating information that's already there for you to pick up if you just add an OnTriggerEnter2D
You are using it wrongly. Read the docs.
It wants opposing corners, not edges. $$anonymous$$in and $$anonymous$$ax are Depth parameters.
Baste: I don't use the built-in physics for colliding since my GameObject has multiple colliders, each colliders bounds colliding on a different layer (by script). Otherwise I can only deter$$anonymous$$e one layer per GameObject. The colliders don't use their "Is Trigger" ability. I usecolliders here to visualize the colliding bounds and to be able to change them by just modifying the according collider bounds.
meat5000: The docs say: "The rectangle is defined by two diagonally opposite corner coordinates in world space. You can think of these as top-left and bottom-right but the test will still work if the ordering of the points is reversed." So what's wrong with the parameters? I tried the following code earlier but it also has the same result as above.
void Update() {
Vector2 topLeft = new Vector2(
collider2D.bounds.center.x - collider2D.bounds.extents.x,
collider2D.bounds.center.y + collider2D.bounds.extents.y);
Vector2 bottomRight = new Vector2(
collider2D.bounds.center.x + collider2D.bounds.extents.x,
collider2D.bounds.center.y - collider2D.bounds.extents.y);
Collider2D result = Physics2D.OverlapArea(topLeft, bottomRight, collidingLayer);
Debug.Log("Player maxX: " + collider2D.bounds.max.x);
if (result != null) {
Debug.Log("Block $$anonymous$$X: " + result.bounds.$$anonymous$$.x);
}
}
In your text you said left and right edge :)
But your corner vectors look correct, only you use bounds max and $$anonymous$$ to debug, which might not be the same.
Debug.Log all the corner points of the offending colliders and see if this holds up.
I dug out an interesting post.
Is the distance between the objects greater than 4.381 Units? meat5000 is correct. the $$anonymous$$s and max would be the same if the objects were in the same spot.
Answer by hexagonius · Feb 17, 2015 at 12:45 PM
I know from a project that 2D colliders use some kind of a skin like the charactercontroller does. By zooming in on them I found it round about 0.03 units thick. Maybe thats also the case here. Why are you not using OnCollisionEnter2D?
Is this skin around physic bounds documented anywhere? I don't use the built-in mechanism because I use multiple bounds/colliders for different layers on one single GameObject (see my comment above)
What is your Penetration value in your physics manager? Is it negative?
No, the value is at its default (0.01). I did not change any physics settings.
Answer by wanobano · Feb 17, 2015 at 08:03 PM
May the reason be that the physics calculation is operating at a lower resolution (maybe for performance issues) than the world resolution???
Your answer
Follow this Question
Related Questions
Physics2D.OverlapBoxNonAlloc not return all overlapped collider 1 Answer
How to Detect if a Collider is in the Area, Move Away, Rinse and Repeat? 1 Answer
2D Box Colliders Overlapping On Collision 4 Answers
Physics2D.OverlapCircleAll and OverlapCapsuleAll behaves differently? 0 Answers
Physics2D.OverlapBoxAll and Physics2D.OverlapAreaAll are inaccurate. 0 Answers