Search code examples
javalibgdxbox2daabb

LIBGDX Box2D World QueryAABB not working


So I tried to get the bodys in my world in a range, which is a square(AABB).

This is my Code:

public List<MovableObject> getObjectsInRange(float x, float y, float x2, float y2) {
    final List<Fixture> list = new ArrayList<Fixture>();
    world.QueryAABB(new QueryCallback() {
        @Override
        public boolean reportFixture(Fixture fixture) {
            System.out.println(fixture);
            list.add(fixture);
            return true;
        }
    }, x, y, x2, y2);
    List<MovableObject> l = new ArrayList<MovableObject>();
    for (Fixture fixture : list) {
        l.add((MovableObject) fixture.getBody().getUserData());
    }
    return l;
}

But what this actually does, is, I only get a report if the AABB is directly inside the fricture. I've got some images to show you what I mean.

Not working: Not working :(

Working: Working :)


Solution

  • I solved the problem, because of the upside down y system of libgdx, I had to modify it the following way

    public List<MovableObject> getObjectsInRange(float x, float y, float x2, float y2) {
        final List<Fixture> list = new ArrayList<Fixture>();
        world.QueryAABB(new QueryCallback() {
            @Override
            public boolean reportFixture(Fixture fixture) {
                list.add(fixture);
                return true;
            }
        }, Math.min(x, x2), Math.min(y, y2), Math.max(x, x2), Math.max(y, y2));
        List<MovableObject> l = new ArrayList<MovableObject>();
        for (Fixture fixture : list) {
            l.add((MovableObject) fixture.getBody().getUserData());
        }
        return l;
    }
    

    I added Math.min and Math.max