6
\$\begingroup\$

I have been reading through the Unity Manual and have come across some interesting information about mesh colliders and primitive colliders. It got me wondering if using many primitive colliders would be better than using a mesh collider for say a character object?

I was also wondering if there was any information on exactly how inefficient a mesh collider is to say having 20 box or cylinder colliders on a character mesh. I am guessing that the number of polygons on the mesh would be a factor but it would be interesting to see some raw comparisons between how this efficiency scales per polygon (or per 100 polys or some unit of measurement)

\$\endgroup\$
9
\$\begingroup\$

In my experience writing collision detection, mesh-based collision (triangles versus other triangles) are the most expensive form of collision in physics engines (PhysX, Havok). Unity uses PhysX internally, so this is no different. Because each computer and platform perform differently, exact numbers cannot be provided, but generally speaking the relative costs of collision detection of types are in the following order, from most expensive to least: triangle mesh, convex hull, box, capsule, sphere, plane, point.

Triangle meshes have the least-helpful characteristics for writing efficient collision detection. As a mesh, they don't guarantee convexivity, are not solid, and cannot be described implicitly through an equation. As a result, any detection is performed with each triangle individually.

To expand on triangle mesh collisions, and without direct knowledge of the source code, I would think Unity does collision detection against triangle mesh in the following way: a spatial data structure wraps the triangles. Likely a KD-tree, bounding volume hierarchy (BVH) or some other form of hierarchical structure. This accelerates queries between primitives and the mesh, yielding a list of candidate triangles for what is called narrow-phase collision algorithm. Then primitive-v-triangle tests are performed. For spheres, the basic sphere-v-triangle test calculates the distance of point from triangle testing if the distance is less than the radius of the sphere. More complicated primitives against the triangle use the GJK algorithm, or SAT algorithm against the triangle.

To address the high cost of triangle meshes, most games that require high fidelity collision detection with characters resort to using a combination of primitives to approximate the character - spheres, capsules or convex hulls bound to the joints of the skeleton hierarchy. The actual number of shapes used to represent the character are determined by requirements and the hardware the platform running on.

So to answer your question, I don't think there is always an objective answer to your question about 'exactly how inefficient a mesh collider is...'. But a handful of simple primitives is always faster then a deforming mesh geometry.

\$\endgroup\$
  • \$\begingroup\$ Minor nitpick. A sphere comes 2nd last after plane. A plane needs at least a width and a height of both objects. A sphere needs only the radius of the object. \$\endgroup\$ – Madmenyo Jan 16 '15 at 13:54
  • \$\begingroup\$ @MennoGouw By plane I meant an infinite plane. Sphere versus plane is a dot product - so I guess it is almost exactly the same. Thanks for the comment. \$\endgroup\$ – Steven Jan 16 '15 at 23:13
  • \$\begingroup\$ @MennoGouw I was actually thinking the same thing, I remember at Uni they mentioned that sphere colliders were very efficient. \$\endgroup\$ – Matthew Pigram Jul 13 '15 at 23:26
  • \$\begingroup\$ How to make a point collider (least expensive) in Unity? \$\endgroup\$ – XenoRo Jan 23 at 22:08
  • \$\begingroup\$ @xenoro not sure about Unity but its a sphere collider with a radius of zero. \$\endgroup\$ – Steven Jan 25 at 4:48
1
\$\begingroup\$

Use the profiler to do detailed checks on these things.

But you're best off using primitive colliders unless you really need to be able to shoot a bullet between someone's arm and his side, and possibly miss. For most purposes, a simple pill shape is good enough for character collision.

\$\endgroup\$
  • \$\begingroup\$ I am more interested in how the computations for the collision are performed on a mesh collider, also I do not have Unity Pro so I cannot use profiler. \$\endgroup\$ – Matthew Pigram Oct 29 '14 at 1:33
  • \$\begingroup\$ Well, without the source, we would only be guessing how exactly they do this stuff. \$\endgroup\$ – Almo Oct 29 '14 at 2:16
1
\$\begingroup\$

The only time when a mesh collider may be appropriate would be for terrain surfaces that are often not flat and it's important to give a more realistic feel to walking on the ground surface as possible.

\$\endgroup\$
0
\$\begingroup\$

For a character object, mostly a capsule collider is enough. But ofcourse it depends on your game. Primitive colliders are more efficient compared to mesh colliders.

You can use Profiler in Unity Pro versions and there's an internal profiler in Android and iOS for Indie versions. Although the pro Profiler obviously gives you detailed analysis, the internal profiler is useful for getting some basic efficiency checks.

Check here: http://docs.unity3d.com/Manual/iphone-InternalProfiler.html

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.