RayTrace Edge Polygons

See: http://code.google.com/p/box2d/issues/detail?id=28

A temporary fix is to replace line 249 in b2PolygonShape.cpp with the following:
Code:
if (upper - lower < -0.0001f)
There is also a bug when the ray is almost parallel to the edge.

Here is the test bed example:
Code:
#ifndef RAY_CAST2_H
#define RAY_CAST2_H

class RayCastCallback2 : public b2RayCastCallback
{
public:
   RayCastCallback2()
   {
      m_fixture = NULL;
   }

   float32 ReportFixture(   b2Fixture* fixture, const b2Vec2& point,
      const b2Vec2& normal, float32 fraction)
   {
      m_fixture = fixture;
      m_point = point;
      m_normal = normal;

      return fraction;
   }

   b2Fixture* m_fixture;
   b2Vec2 m_point;
   b2Vec2 m_normal;
};

class RayCast2 : public Test
{
public:

   RayCast2()
   {
      // Ground body
      {
         b2BodyDef bd;
         b2Body* ground = m_world->CreateBody(&bd);

         b2PolygonShape shape;
         shape.SetAsEdge(b2Vec2(-40.0f, 0.0f), b2Vec2(40.0f, 0.0f));
         ground->CreateFixture(&shape);
      }

      //horizontal
      {
         b2BodyDef bd;
         b2Body* body = m_world->CreateBody(&bd);
         
         b2PolygonShape shape;
         shape.SetAsEdge(b2Vec2(65.0f , 19.99f), b2Vec2(15.0f, 20.01f));

         body->CreateFixture(&shape);
      }

      //vertical
      {
         b2BodyDef bd;
         b2Body* body = m_world->CreateBody(&bd);

         b2PolygonShape shape;
         shape.SetAsEdge(b2Vec2(9.8f , 15.0f), b2Vec2(10.2f, -15.0f));

         body->CreateFixture(&shape);
      }

      m_angle = 0.0f;
      refpoint1.Set(2.15f, 10.0f);
      refpoint2.Set(2.0810061f, 20.013002f);
   }

   void Step(Settings* settings)
   {
      Test::Step(settings);
      

      float32 L = 20.0f;
      
      b2Vec2 d(1.0f, 0.0f);
      b2Vec2 point1 = refpoint1;
      b2Vec2 point2 = point1 + L*d;


      RayCastCallback2 callback;

      m_world->RayCast(&callback, point1, point2);

      if (callback.m_fixture)
      {
         m_debugDraw.DrawPoint(callback.m_point, 5.0f, b2Color(0.4f, 0.9f, 0.4f));

         m_debugDraw.DrawSegment(point1, callback.m_point, b2Color(0.8f, 0.8f, 0.8f));

         b2Vec2 head = callback.m_point + 0.5f * callback.m_normal;
         m_debugDraw.DrawSegment(callback.m_point, head, b2Color(0.9f, 0.9f, 0.4f));
      }
      else
      {
         m_debugDraw.DrawSegment(point1, point2, b2Color(0.8f, 0.8f, 0.8f));
      }


      point1 = refpoint2;
      point2 = point1 + L*d;

      callback = RayCastCallback2();

      m_world->RayCast(&callback, point1, point2);

      if (callback.m_fixture)
      {
         m_debugDraw.DrawPoint(callback.m_point, 5.0f, b2Color(0.4f, 0.9f, 0.4f));

         m_debugDraw.DrawSegment(point1, callback.m_point, b2Color(0.8f, 0.8f, 0.8f));

         b2Vec2 head = callback.m_point + 0.5f * callback.m_normal;
         m_debugDraw.DrawSegment(callback.m_point, head, b2Color(0.9f, 0.9f, 0.4f));
      }
      else
      {
         m_debugDraw.DrawSegment(point1, point2, b2Color(0.8f, 0.8f, 0.8f));
      }
   }

   static Test* Create()
   {
      return new RayCast2;
   }

   float32 m_angle;
   b2Vec2 refpoint1;
   b2Vec2 refpoint2;
};

#endif
Just a little note: if you allow for a delta of 0.0001 in the comparison between upper and lower, make sure that you also update the assert line a few lines below, so that you do not compare the maximum value with lower but rather (lower - 1).

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