RayCastCallback "returns" unexpected fraction values

edited October 2017 in Library Questions

The reportFixture() Function is supposed to return the fraction of the ray (I need this to tell how far away and obstacle is from the object). However, I can't figure out where the value is returned to when box2d.world.raycast(callback, point1, point2) is called. To work around this I did the following but it seems to cause bugs in my code. (sometimes ray cast callback returns fractios values less than1 (which signifies a collision) even when the ray didn't collide with anything.

class RayCastClosestCallback implements RayCastCallback {

  boolean m_hit;
  Vec2 m_point;
  Vec2 m_normal;
  float m_fraction;

  public float reportFixture(Fixture fixture, Vec2 point, Vec2 normal, float fraction) {
    /*
      public float reportFixture() - triggers wehn collision with ray occurs
     returns distance of collision
     */
    m_hit = true;
    m_point = point;
    m_normal = normal;
    m_fraction = fraction;
    return fraction;
  }
}

This is where I call the rayCastCallback

void checkSensors() {
    /*
      void checkSensors() - give the car inputs from its sensors
     */

    Vec2 p1 = box2d.coordPixelsToWorld(box2d.getBodyPixelCoord(body));
    for (int i = 0; i < numSensors; i++) {
      Vec2 p2  = sensors[i].mul(box2d.scalarPixelsToWorld(rayLength));
      p2.addLocal(p1);
      box2d.world.raycast(ccallback, p1, p2);
      if (ccallback.m_hit) {
        sensorFractions[i] = ccallback.m_fraction;
        input[0][i] = 1.0 - ccallback.m_fraction;
      } else {
        input[0][i] = 0;
        sensorFractions[i] = 1;
      }
      println("Fraction : " + str(ccallback.m_fraction));
    }
  }

I made the fraction a member variable of the class.

The car is in red and the fraction of the rayCast is diplayed in the console. As you can see they shouldn't be that small

This is what a correct implementation would sort of look like. (Note the bug is still present in this shot it's just less noticeable)

Tagged:
Sign In or Register to comment.