Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Circle Collider 2D or Box Collider 2D

I've done some research but can't find the answer to which is most efficient, the box collider 2D or the circle collider 2D?

There's this question that got a great answer quickly which says the fastest is a sphere collider, followed by the capsule collider and then the box collider but I'm wondering about 2D colliders.

2500 Colliders

  • Capsule 453-481ms

  • Box 490-520ms

  • Sphere 190-233ms

Does anyone have information on which is quicker for a computer to treat? Thanks!

like image 672
Alox Avatar asked Feb 02 '17 20:02

Alox


1 Answers

The performance difference between the different 2D colliders can be compared on a strictly mathematical basis; a relative idea of their cost can be grasped by identifying the steps needed to determine whether a point (P) is within a particular shape:

Circle collider: Very simple calculation; just compare the distance between the circle's center and P with the circle's radius. If distance < radius, the point is within the circle. (It's even cheaper to calculate if we assume they're comparing distance2 < radius2, since that avoids a somewhat costly square root operation.)

Box collider: Also fairly straightforward, with just a touch of linear algebra; as per this solution, you need to calculate and compare 2 pairs of dot products between the vertices of the rectangle and point P. (The theory behind this is that P should form acute angles internally with all the vertices of the rectangle - if it doesn't, it's outside.) This isn't very expensive, as calculating a dot product is just a bit of multiplication and addition. However, relative to the circle collider it's still a lot more steps and will be slower.

Polygon collider: Determining if a point is within a polygon is where things can get very slow. Because Unity's polygon colliders can be concave, a simplified approach such as determining which side of each edge P lies on won't work.

One approach for potentially concave polygons is to perform a raycast that passes from outside of the polygon to P, and count how many edges it crosses - if it's odd, then P is inside the polygon. (I've implemented this once in 3D before, but I'm not sure if there's a faster approach.) There are other approaches too, but they are all slower than the preceding two collision detections; they all require comparison of P against each edge or vertex of the polygon, and need multiple steps of multiplication, addition, and sometimes even division (slow!) in order to make the determination of whether P is within the polygon.

Edge collider: With this collider, a point-based collision analogy doesn't really work. The easiest way to envision it working it to cast a ray between each pair of its vertices, to check if they intersect with any collider shapes. This is a bit of an apples-to-oranges comparison with the other colliders, since the vertices of the collider don't form a closed shape - there is no "inside" of the collider. As a result, your use cases for this collider will be limited (and doesn't seem applicable to what you want here). Anecdotally, this collider performs better than a polygon collider and trades blows with the box collider, but again, these are only valid comparisons in specific use cases (like static terrain/obstacles).

Hope this helps - I didn't include any of the implementation details or mathematical theory behind most of the approaches, but I included links where possible if you want to do some further reading on them. As noted in my comment, use whichever collider works best for the object you're using - after all, a circle collider may be cheaper than a box collider, but it also behaves very differently in physical interactions.

If you're scaling up the simulation to the point where these individual interactions are not longer relevant and strictly the collision detection matters, then perhaps you will want to switch to a more efficient collider. Of course, you know your project best so that's up to you.

like image 96
Serlite Avatar answered Sep 30 '22 07:09

Serlite