5

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!

Community
  • 1
  • 1
Alox
  • 651
  • 12
  • 24
  • I think you've just answered your own question no? But the sphere collider is definitely the fastest. – maraaaaaaaa Feb 02 '17 at 20:35
  • 1
    Determining if a point is within a circle is a simpler calculation than determining if a point is within a square/rectangle. But use what works best for your situation, and only optimize these details later if you need to. (Chances are the type of colliders you choose won't be the performance bottleneck in your game.) – Serlite Feb 02 '17 at 20:42
  • 2
    @maksymiuk unfortunately I didn't, the information displayed is for 3D environment, they most likely calculate 2D hit detection differently which could potentially make the box collider 2D faster than a circle collider 2D. – Alox Feb 03 '17 at 12:42
  • @Serlite the game I'm working on has prefabs which each have their own colliders, changing from box collider to circle collider could potentially be useful in the future if we were to use a lot of those prefabs at once especially since the company I work for has specific requirements from game performances. Would it be so minimal that even in a drastic scale it wouldn't affect the performance enough to notice? – Alox Feb 03 '17 at 12:47
  • A circle/sphere collider will always be faster than any other method. This is simply because checking for collision is calculating distance from a single point. All other forms require further calculations to be done. – maraaaaaaaa Feb 03 '17 at 15:28

2 Answers2

7

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.

Community
  • 1
  • 1
Serlite
  • 12,130
  • 5
  • 38
  • 49
0

Sphere > Capsule > Box > Mesh

Circle > No 2D Capsule :( > Box > not sure if Edge or Poly would be fastest.

SilentSin
  • 1,026
  • 9
  • 18
  • Would you have analytics or a link to provide clarification as to how much different they are to one another? Without analytics it's just an assumption based off of the question I shared in my question. – Alox Feb 03 '17 at 12:38
  • Just a note that CapsuleCollider2D was added in Unity 5.5. – juanitogan Oct 04 '18 at 07:41