0

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 :)

bastelflp
  • 9,362
  • 7
  • 32
  • 67
Intektor
  • 321
  • 2
  • 13

2 Answers2

1

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

Intektor
  • 321
  • 2
  • 13
0

How are you calling getObjectsInRange ? Because QueryAABB will only report objects that are inside the area.

KenobiBastila
  • 539
  • 4
  • 16
  • 52