Shape-shape testing?

Hey there.

I'm a bit curious about something - why does Box2D not have any shape-shape testing API, or if it does, how do I access it?

See, I ran into this issue where I need to, basically, test if any static objects are within a polygon. It turned out to be surprisingly difficult, I essentially had to copy the entire Contact class, make changes so that I can make a dynamic one, and then use the b2Collide* functions to do tests.

Box2D should really have a built-in version of doing this, like an overload of the b2Contact constructor that accepts two fixtures and tests them as if it was a contact that the world created.

EDIT: I should clarify that I'm talking about shapes that aren't connected to a body or a fixture. For instance, if you wanted to see if anyone in the map is inside of an irregular shape without making a temporary body and all that stuff. It should definitely be possible, and it sounds like something that would be really wanted in this.

-P
Box2D is designed to do this. See b2Collision.h. For example, you can use b2CollidePolygons. You do not need to create any bodies or have a b2World to do collision.
Yeah but it's inconvenient. An interface should be provided to access this like Contacts, where I can just initialize a "virtual" contact and get if the two objects are touching within like, two lines of code, as I have done with my "b2QuickContact" class; here's a sample from within a callback:

Code:

         if (fixture == ignore)
            return true;
         else if (onlyWorld && (fixture->GetBody()->GetUserData() != null &&
            ((Entity*)fixture->GetBody()->GetUserData())->GetID() != EntityID::World))
            return true;

         b2Transform tf1 (testPos, b2Rot());
         tf1.q.SetIdentity();
         var tf2 = fixture->GetBody()->GetTransform();
         var cont = b2QuickContact::Create(tf1, &testShape, 0, tf2, fixture->GetShape(), 0, false);
         cont->Update();

         if (cont->IsTouching())
            collisions.Add(fixture);

         b2QuickContact::Destroy(cont);
         return true;


Obviously it's dirty code and still pretty close to the original contact, but exposed publicly and without requiring to get the proper functions in the right order, etc etc, as it's already provided.

It just seems like a waste to have written b2Contact perfectly suited for this sort of operation, but not allowing users to create their own virtual contacts to test shapes without requiring to access the more advanced aspects of the API (manifolds, collision functions, etc).

-P
Have you seen the PolyShapes.h code? There is no need to create a b2Contact.

Code:
main ...
      PolyShapesCallback callback;
      callback.m_circle.m_radius = 2.0f;
      callback.m_circle.m_p.Set(0.0f, 2.1f);
      callback.m_transform.SetIdentity();
      callback.m_debugDraw = &m_debugDraw;

      b2AABB aabb;
      callback.m_circle.ComputeAABB(&aabb, callback.m_transform, 0);

      m_world->QueryAABB(&callback, aabb);


Code:
/// This callback is called by b2World::QueryAABB. We find all the fixtures
/// that overlap an AABB. Of those, we use b2TestOverlap to determine which fixtures
/// overlap a circle. Up to 4 overlapped fixtures will be highlighted with a yellow border.
class PolyShapesCallback : public b2QueryCallback
{
public:
   
   enum
   {
      e_maxCount = 4
   };

   PolyShapesCallback()
   {
      m_count = 0;
   }

   void DrawFixture(b2Fixture* fixture)
   {
      b2Color color(0.95f, 0.95f, 0.6f);
      const b2Transform& xf = fixture->GetBody()->GetTransform();

      switch (fixture->GetType())
      {
      case b2Shape::e_circle:
         {
            b2CircleShape* circle = (b2CircleShape*)fixture->GetShape();

            b2Vec2 center = b2Mul(xf, circle->m_p);
            float32 radius = circle->m_radius;

            m_debugDraw->DrawCircle(center, radius, color);
         }
         break;

      case b2Shape::e_polygon:
         {
            b2PolygonShape* poly = (b2PolygonShape*)fixture->GetShape();
            int32 vertexCount = poly->m_vertexCount;
            b2Assert(vertexCount <= b2_maxPolygonVertices);
            b2Vec2 vertices[b2_maxPolygonVertices];

            for (int32 i = 0; i < vertexCount; ++i)
            {
               vertices[i] = b2Mul(xf, poly->m_vertices[i]);
            }

            m_debugDraw->DrawPolygon(vertices, vertexCount, color);
         }
         break;
      }
   }

   /// Called for each fixture found in the query AABB.
   /// @return false to terminate the query.
   bool ReportFixture(b2Fixture* fixture)
   {
      if (m_count == e_maxCount)
      {
         return false;
      }

      b2Body* body = fixture->GetBody();
      b2Shape* shape = fixture->GetShape();

      bool overlap = b2TestOverlap(shape, 0, &m_circle, 0, body->GetTransform(), m_transform);

      if (overlap)
      {
         DrawFixture(fixture);
         ++m_count;
      }

      return true;
   }

   b2CircleShape m_circle;
   b2Transform m_transform;
   b2Draw* m_debugDraw;
   int32 m_count;
};
Ah yes, THAT's what I was looking for, but that's in contribs isn't it? I haven't looked through there. Considering the code is already there in b2Contact that's why I'm kind of perplexed as to why it wasn't already there.

EDIT: Oh, you meant the test. Okay, I see now you mean b2TestOverlap; I didn't even see this function. I'll try these two instead and see if it's quicker than what I was doing with copied b2Contact code.

-P
You don't want to use b2Contact. It is designed to be created as part of a world (like b2Body, b2Joint, etc). For example, a b2Body is not designed to be useful by itself.

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