How to detect contact points?

I'm really sorry to ask this, because I'm sure that this has been asked a million times. However, every time I Google, I seem to get a dearth of articles or out-of-date references (e.g., b2ContactPoint).

I have an application that has a number of dynamic b2Body objects (circle shape). When the objects collide, I'd like to determine the point of contact (and, as a bonus, the angle of impact). My intent is to play a particle effect at the collision position.

If there are any samples/examples using the Box2D 2.1.2 code base, I'd be hugely obliged. Thanks in advance!
In the testbed, contact points are recorded in a list, to use when rendering if the 'Contact Points' checkbox is checked.
Check the files Test.cpp and Test.h.

Code: Select all

void Test::PreSolve(b2Contact* contact, const b2Manifold* oldManifold)
{
   const b2Manifold* manifold = contact->GetManifold();

   if (manifold->pointCount == 0)
   {
      return;
   }

   b2Fixture* fixtureA = contact->GetFixtureA();
   b2Fixture* fixtureB = contact->GetFixtureB();

   b2PointState state1[b2_maxManifoldPoints], state2[b2_maxManifoldPoints];
   b2GetPointStates(state1, state2, oldManifold, manifold);

   b2WorldManifold worldManifold;
   contact->GetWorldManifold(&worldManifold);

   for (int32 i = 0; i < manifold->pointCount && m_pointCount < k_maxContactPoints; ++i)
   {
      ContactPoint* cp = m_points + m_pointCount;
      cp->fixtureA = fixtureA;
      cp->fixtureB = fixtureB;
      cp->position = worldManifold.points[i];
      cp->normal = worldManifold.normal;
      cp->state = state2[i];
      ++m_pointCount;
   }
}
Thanks, irresistible force -- that has got me up and running!

I don't quite get the b2WorldManifold.normal property, and how I can get the angle from that. Could you point me to how I might use that?

Thanks again!
I believe the normal of the manifold is the direction along which the bodies should be moved to resolve the overlap between them.
However 'angle of impact' sounds more like you are talking about GetLinearVelocity() of the body. A common way to get an angle from this is to normalize the vector and then atan2(y, x)

edit: oh... but that would not help you if the body was stationary and hit something because it was spinning around. I guess what you really want is the velocity of the body at the contact point, which can be obtained with (from b2Body.h):

Code: Select all

   /// Get the world linear velocity of a world point attached to this body.
   /// @param a point in world coordinates.
   /// @return the world velocity of a point.
   b2Vec2 GetLinearVelocityFromWorldPoint(const b2Vec2& worldPoint) const;
Take a look around the header files for more useful functions like this.

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