Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I would like to get the shapes of my bodies, but I can't get anything, only null. Why?
Here is the relevant code section:

Body b;
while((b = box2d.physics.PhysicsWorld.world.getBodyList().getNext()) !=null) {
  Shape shape;
  while((shape = b.getShapeList().getNext()) != null) {

    Log.e("name",""+b.getUserData().toString()+" "+shape+" ");

    opengl.saveMatrix();
    Meshes.select(b.getUserData().toString())
              .translate((b.getPosition().x)*RATIO, (b.getPosition().y)*RATIO)
              .rotate((int) ((int) b.getAngle() * (180 / Math.PI)), 0, 0, 1)
              .draw(shape, 1, 1, 1);

    opengl.loadMatrix();

  }
}

shape = b.getShapeList().getNext() always returns null, and the log entry never ran.

share|improve this question
add comment

1 Answer

up vote 5 down vote accepted

I'm guessing by the presence of Log.e and toString() that this is Java on Android. I don't know much about JBox2D in Java, but...

while((b = box2d.physics.PhysicsWorld.world.getBodyList().getNext()) !=null)

You're only ever going to get the second body here. I can't imagine this loop would ever terminate if it was entered. Your other loop has a similar problem:

while((shape = b.getShapeList().getNext()) != null)

In Box2D, shapes and bodies form a linked list. getBodyList and getShapeList return the head of that linked list, which is itself a body/shape, not an iterator. (Your loop is wrong even if it was an iterator, though! If something returns an iterator, you need to keep using the one it returned, not ask for it again.)

The loop you want is of the form:

for (Body b = box2d.physics.PhysicsWorld.world.getBodyList();
     b != null;
     b = b.getNext())
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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