[SOLVED] +Box2D QueryAABB Not working?

Im using libgdx and box2D.

My screen have 4 EdgeShapes to block the player from moving outside the view.

Im trying to use QueryAABB to remove bodies that are outside the view.

So the logic is :

A = Get the bodies on the screen.
B = Get the bodies on the world.

Intesect “A.B”.

Remove anything that is not intersected

AKA

Remove the bodies outside the screen.

level.getRemoveBodies() have a list of bodies to be removed.

  public void cleanWorld(final Level level) {

        if (level.getRemoveBodies().size > 0) {
            System.out.println("Impossible to clean World at this moment!");
            return;
        }

        System.out.println("Cleaning World at this moment!");

        float lowerX = level.getLeftBodyWall().getPosition().x;
        float lowerY = level.getBottomBodyWall().getPosition().y;
        float upperX = Box2DUtils.width(level.getUpperBodyWall()) + lowerX;
        float upperY = Box2DUtils.height(level.getUpperBodyWall()) + lowerY;

        System.out.println("Debug : " + Box2DUtils.width(level.getUpperBodyWall()));

        System.out.println("Upper X :" + upperX);
        System.out.println("Upper Y :" + upperY);

        System.out.println("lower X :" + lowerX);
        System.out.println("lower Y :" + lowerY);

        Body spaceShipBody = level.getPlayer().getSpaceShipBody();

        System.out.println("SpaceShip Pos : ( " + spaceShipBody.getPosition().x + " ; " + spaceShipBody.getPosition().y + " )");

        final Array<Body> bodies = new Array<Body>();
        level.getWorld().getBodies(bodies);

        QueryCallback queryCallback = new QueryCallback() {

            @Override
            public boolean reportFixture(Fixture fixture) {

                Body body = fixture.getBody();

                boolean removeValue = bodies.removeValue(body, true);

                return false;
            }

        };

        level.getWorld().QueryAABB(queryCallback, lowerX, lowerY, upperX, upperY);

        level.getRemoveBodies().addAll(bodies);

    }

Not working!
What am i doing wrong?

You do “return false” instead of “return removeValue” in line 37 or so.

Still the same error :frowning:

i think im calculating the positions the wrong way. But im not sure

The javadoc of reportFixture says: “@return false to terminate the query.”
This means you stop searching once you found the first fixture/body. Return true instead.

Also try this:

Vector2 tmp = ...;
tmp.set(0, Gdx.graphics.getHeight()); // bottom left
camera.unproject(tmp);
float lowerX = tmp.x;
float lowerY = tmp.y;
tmp.set(Gdx.graphics.getWidth(), 0); // top right
camera.unproject(tmp);
float upperX = tmp.x;
float upperY = tmp.y;

I need to add the javaDoc URL in netbeans!

Ty ! It works perfectly!

I was thinking, if i should make this in a different thread…