7
\$\begingroup\$

When using a physics engine in a game (such as Bullet), you usually need to define a collision margin for each object. I'm a beginner to game development, and I don't understand the purpose of a collision margin.

My basic understanding is that if you were to have two objects, each with a collision margin set to 0.01, then a collision would be detected when the two collision margins touch each other, such that the gap between the two objects is actually 0.02.

But why not just set the collision margin to 0, and cause a collision to be detected when the actual surfaces of the two objects collide? This is how physics works in reality.

One guess is the following. If a cube has a margin of 0.01, then a collision will be detected whenever a part of another object exists between 0 and 0.01 from the object's surface. This gives the simulation room for error, such that if it "misses" the point when the object passes the cube's collision margin (because the simulation step is long), then it still has time to detect a collision as the object then moves closer towards the cube's surface, within the 0.01 margin. Otherwise, if the margins are zero and a collision is missed by the simulation, you might get a situation where two solid bodies occupy the same space, which could cause problems.

Is this guess correct? Or is there another reason?

Thanks!

\$\endgroup\$
  • \$\begingroup\$ Are you sure it works entirely that way in reality? Can we make infinitely sharp edges then? Where one face has absolutely no rounding between it and adjacent faces? Or how about making a line segment in reality that could be collided with that had no volume? \$\endgroup\$ – Louis Langholtz Jun 10 '17 at 13:43
  • \$\begingroup\$ I understand your point about how physics seems to work at a classical level and I agree it makes some sense. It's just that I think reality can substantiate a vertex radius rounded model too. \$\endgroup\$ – Louis Langholtz Jun 10 '17 at 13:47
10
\$\begingroup\$

There are three reasons collision margins may exist in physics simulations.

  1. As you suggested, a collision margin gives the physics engine some room for error in detecting contacts and resolving contacts, prior to actual penetration. This helps with the appearance of realism as objects do not visibly poke through the ground, etc. There are nuances here for the calculation of the threshold for when contact constraints apply Baumgarte stabilization during solving to help separate bodies.

  2. As DMGregory notes, collision margins may be added to planes or triangles to provide an arbitrary thickness to primitives that are mathematically infinitely thin, effectively turning them them into a volume.

  3. Perhaps more important is that many collision detection engines use the Gilbert–Johnson–Keerthi (GJK) algorithm to perform convex-v-convex collision detection. This algorithm is efficient when performing collision detection between disjoint pairs - even if they are very close to one another. The moment the two convex shapes actually overlap each other, the cost of the GJK algorithm increases dramatically. Furthermore, to generate an effective contact point the Expanding Polytope Algorithm (EPA) must be run, which is even more costly. As a result many physics engines (Havok, Bullet) create a shell around the convex hull, which is the usually named something like collision margin or convex shell.

Erwin Coumans, the author of bullet answers some questions here with an overview below:

GJK doesn't calculate penetration depth by default. You can add a margin to collision shapes, which allows you to use GJK to calculate shallow penetration depth (not deeper then the added margins of the 2 involved objects). For a sphere, you can use the full radius as margin, and use a point as shape. As long as there is no overlap (without the margins) when GJK terminates, the simplex solver (either johnson or 'geometric/voronoi' can be used as world-space contact points. For deeper penetrations, use either EPA (expanding polytope algorithm) or sample the pentration depth in several directions, using the support mapping.

\$\endgroup\$
  • 2
    \$\begingroup\$ Adding onto this, I've also heard of adding a margin for collision/intersection tests against planes/triangles without inside/outside information, where without a margin the volume of interest would be infinitely thin. Because of the limits of precision in fixed or floating point numbers, the closest representable point on a trajectory might not lie truly "on" the surface, even if an exact solution does exist for infinite-precision real numbers. Adding a margin in this case helps avoid false negatives due to rounding errors by thickening the volume (similar to point 1, but for another reason) \$\endgroup\$ – DMGregory Dec 29 '15 at 1:17
  • \$\begingroup\$ @DMGregory Thanks, I've updated the answer to include that useful point. \$\endgroup\$ – Steven Dec 29 '15 at 7:27

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.