Capsule Shape

I've run into some problems with the two circles + rectangle for a capsule shape, so I'm trying to write a real one. I'm not 100% sure I've gotten everything correct, but I'm stuck on the polygon and edge contacts.

How should the capsule shape collide with edges and polygons? Here's some of the ways a polygon and capsule can intersect. (The red line is the segment that defines the capsule; a capsule is defined as a line segment + radius.)

Image

I really don't know what algorithm to use for this or for edge shapes. Can someone point me in the right direction?

(BTW, I used an algorithm from Game Programming Gems 2, is that kosher on a free project?)
Capsule vs polygon (or edge) should be done as an extension of sphere vs polygon.

- measure the distance between the capsule and polygon (using GJK).
- if the distance is positive and within the capsule radius, look for an edge normal that matches the distance vector (to some tolerance).
- if found, then clip the capsule segment against the edge segment side-planes
- if not found, create a single contact point at the mid-point of the distance vector.
- otherwise, use SAT, similar to polygon-vs-polygon.

Erin
Thank you for the fast reply. It makes a lot of sense, but I have a few questions:

1) Should I create a new proxy for polygons and use the GJK code from b2Distance.cpp? or is there some better way to write a special case of the code?

2) Any rough estimate that might work for the edge normal tolerance?

3) Should I try to reuse code from the polygon SAT implementation or start from scratch?


(Added some debug drawing and tests for buoyancy, polygon shapes and compound shapes. No edge/polygon collision yet, and buoyancy isn't perfect.)
You should be able to use DistanceGenerice, just implement the needed functions on the capsule shape.

Use something like this for the normal tolerance: dot(n1, n2) > cos( 5 degrees )

You can duplicate then simplify the polygon SAT code.

BTW, I looked at the circle-vs-polygon code and I can see that it does not use GJK. It probably should, but I must have written it before I had GJK.
Sounds good. I tried to hack together a less efficient solution, but it looks like I will need GJK.

One last question: Should I reimplement the support function for polygons and edges so that it doesn't use slop for this test?
Probably. The whole slop thing will be cleaned up eventually.
OK. Took a crack at exposing the generic distance function. Going to try to use it next.

Code:
template <typename T1, typename T2>
float32 b2GJK(  b2Vec2* x1, b2Vec2* x2,
            const T1& supporter1,
            const T2& supporter2)
{
   *x1 = supporter1.GetFirstVertex();
   *x2 = supporter2.GetFirstVertex();

   float32 distanceSquared = 0.0f;
   const int32 maxIterations = 20;
   int32 iter = 0;
   for (; iter < maxIterations; ++iter)
   {
      b2Vec2 v = *x2 - *x1;
      b2Vec2 w1 = supporter1.Support(v);
      b2Vec2 w2 = supporter2.Support(-v);
      
      if (!b2RunGJK(&distanceSquared, x1, x2, v, w1, w2))
      {
         break;
      }
   }
   g_GJK_Iterations = iter;
   float32 distance = b2Sqrt(distanceSquared);
   distance = supporter1.AdjustDistance(x1, x2, distance);
   distance = supporter2.AdjustDistance(x2, x1, distance);
   return distance;
}


Code:
   /// Get a GJK supporter for this shape.
   b2PolygonSupport GetSupporter(const b2XForm& xf) const;
   
   /// Get a GJK supporter for this shape, using TOI Slop.
   b2PolygonSupport GetTOISupporter(const b2XForm& xf) const;


Code:
// This is a GJK support class for polygons.
class b2PolygonSupport
{
public:
   b2PolygonSupport(const b2XForm& xf, const b2Vec2* vertices, int32 vertexCount)
   {
      m_xf = xf;
      m_vertices = vertices;
      m_vertexCount = vertexCount;
   }
   
   b2Vec2 Support(const b2Vec2& d) const;

   b2Vec2 GetFirstVertex() const
   {
      return b2Mul(m_xf, m_vertices[0]);
   }
   
   float32 AdjustDistance(b2Vec2*, const b2Vec2*, float32 distance) const
   {
      return distance;
   }

private:   
   b2XForm m_xf;
   const b2Vec2* m_vertices;
   int32 m_vertexCount;
};
OK; I'm getting closer. It mostly works now, but suffers from instability:

1) Buoyancy is incorrect.
2) Capsule-capsule collision only uses 1 contact point and is unstable because of that.
3) Polygon-capsule collision is imperfect, but getting closer.
4) Edge-capsule collision is highly unstable, especially around convex corners.


A) Where should the contact points be located on a capsule-capsule collision? I still don't understand them well enough. Is it that any flat-side to flat-side collision should have two to restrict rotation, whereas corners and convex curves always only need one? Does it matter where the contact points are located? Is it better to have them far apart?

B) The purpose of contact IDs is just for them to be unique, right?

I think I need another pair of eyes to look at the polygon-capsule and edge-capsule code; I can't figure out what causes the instability.
I've been watching this thread and am very interested in this - keep at it!

Have you considered extending this to allow for rounded-rectangles (i.e. width/height/radius rather than just length/radius)?

From my understanding of contact points you seem to be on track with A: contacts involving a curve (an edge of a circle) always have one contact point. Flat-flat contacts (like a box resting on another box) have two contact points, and those points should represent the extents of the manifold (the distance from point1 to point2 should represent the full area the two shapes are flush against each other). Poly-poly (or line) contacts can have only one point though in the case of a corner-line contact.

And yes, B sounds correct too. I believe the contact IDs also let the engine "guess" what edges are in contact in the next step to increase speed, rather than re-calculating everything (may be wrong with that though).

In the case of Buoyancy, I suppose you could just approximate it as a rectangle to simplify things.

Good luck...
IIRC, the main point of contact point ids is so that the warm starting information can be persisted to the "same" contact point each frame.

Original thread: https://box2d.org/forum/viewtopic.php?t=2998