JBox2d - world.getBodyList () goes into an infinite loop

I am trying to create a class that draws the objects of my world JBox2d on canvas.

When updating, I have a call

render.draw(canvas,world);

which transfers the world and the canvas to the drawing class, which will have to cyclically move around the objects of the world and draw them into the canvas.

public void draw(Canvas canvas, World world)
{

    canvas.drawColor(0xFF6699FF);

    for ( Body b = world.getBodyList(); b!=null; b.getNext() )
    {
        Log.e("xy", String.valueOf( b.getPosition().x )+" "+String.valueOf( b.getPosition().y )  );
    }

}

while it seems to go into an infinite loop, the return button does not work, then it says "not responding" and offers to force close.

Any ideas how to move through the bodies in this case?

Thank!

+3
source share
2 answers

As mentioned in my comment - the loop should be as follows:

for ( Body b = world.getBodyList(); b!=null; b = b.getNext() )
{
    Log.e("xy", String.valueOf(b.getPosition().x)+ " " + String.valueOf(b.getPosition().y));
}
+6
source

. (/) :

public void draw(Canvas canvas){
    Body body = world.getBodyList();
    while(body != null){
        Fixture fixture = body.getFixtureList();
        while(fixture != null){
            ShapeType type = fixture.getType();
            if(type == ShapeType.POLYGON){
                PolygonShape shape = (PolygonShape)fixture.getShape();
                // draw shape
            }else if(type == ShapeType.CIRCLE){
                CircleShape shape = (CircleShape)fixture.getShape();
                // draw shape
            }
            fixture = fixture.getNext();
        }
        body = body.getNext();
    }       
}
0

All Articles