1
votes

I am developing an Android game using the libgdx box2d library in Eclipse. But I am stuck on a problem. I want to drop a static body whenever the the moving ball touches that static body (a polygon shape box), the method is called in the ContactListener.beginContact().

I tried to create a new body with the same properties but the body type changed to dynamicbody. But if I do this, I am getting NullPointerException every time the ball touches the static body. Also getting a NullPointerException when tried to setLinearVelocity(0) on a kinematicbody.

How can I implement this?

1
Welcome to Stackoverflow! Please, show some code when you ask a question to help us understand your problem.Lajos Arpad

1 Answers

4
votes

You can't create new bodies inside the World.step. The world is locked then.

The ContactListener callbacks are inside the world.step. So if you want to create a new body, set a flag like:

boolean createnewbody = false;

Inside your collision callback:

createnewbody = true;

And in your render method:

if(createnewbody){

    //create the new body code here

    createnewbody = false;
}