3
\$\begingroup\$

I am trying to filter a category in my ray casting (jBox2D within libGDX), which should return the closest object that does not belong to the category LEVEL0. I've tried a plethora of approaches, but none of them seem to work.

This post, for example, instructed me to do so:

private static class RayCast implements RayCastCallback {
    Fixture f;
    Vector2 point;
    float fraction;

    public RayCast() {
       this.fraction = 1f;
    }

    @Override
    public float reportRayFixture(Fixture fixture, Vector2 point, Vector2 normal, float fraction) {
       if (fixture.getFilterData().categoryBits == Corpo.LEVEL0)
          return 1f;

       if (fraction <= this.fraction) {
           this.f = fixture;
           this.point = point;
           this.fraction = fraction;
       }
       return 1f;
    }
}

Yet, it ignores the LEVEL0 bodies only sometimes, and sometimes ignores other bodies as well (though it seems to work on most cases, it fails frequently enough to be unacceptable). I've tried a few other solutions, including: returning -1 in the first if (which should mean ignore this collision), return this.fraction always, instead of 1, but nothing works - this, in fact, seems to be a very common problem, as the internet is filled with a variety of workarounds, none of them working for me.

I think my library is updated, or at least is the version that is included in the latest libGDX (which, according to my quick research, is the actual latest).

I've posted this same question here, but I'm not sure about the official forum's activity, so I decided to ask here too, just in case. Whichever website gives the answer first, I shall copy it to the other.

\$\endgroup\$
3
\$\begingroup\$

(I haven't got enough rep to comment)

Unless there's a bug in the LibGDX Box2D, the only thing I can think if is your condition - are you sure categoryBits is going to be exclusively LEVEL0? Or do you need to do a bitwise AND:

if ((...categoryBits & Corpo.LEVEL0) == 0) { ... } ?

Are you always setting the categoryBits for every fixture?

Can you step through the callback in a debugger to check what is coming through it?

\$\endgroup\$
4
  • \$\begingroup\$ Objects can have only one category. Yet, I changed that, and started to print whenever it entered the if, to see if it'd work. Your solution didn't seem to change anything; printing the results, however, showed me that they were right. A little more investigation and I found out that the fixture variable was correct - the only problem was with the point, that was causing the laser to render wrong. In a lucky guess, I decided to create a copy of the point (this.point = point.cpy();), and it magically worked. So I guess it just changes the point, and it changes my reference as well. \$\endgroup\$ – Luan Nico Oct 7 '13 at 23:02
  • \$\begingroup\$ PS. I don't have enough rep to upvote you, but thanks anyway :) And I guess it is a bug, or at least a very poorly documented scenario. \$\endgroup\$ – Luan Nico Oct 7 '13 at 23:07
  • \$\begingroup\$ Ah yes, the docs say the two vectors are reused... so I assume the problem wasn't the raycasting at all, the point was in the wrong place? \$\endgroup\$ – ThorinII Oct 8 '13 at 0:06
  • \$\begingroup\$ Exactly, just making a copy, as warned in the libGDX docs, solved the problem. jBox2D docs, on the other hand, make no metion whatsoever of the problem... \$\endgroup\$ – Luan Nico Oct 8 '13 at 14:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.