# Libgdx box2d crashing

im making a game in libgdx which i having a bunch of circle bodies floating around in space and when the player's body crashes into another body, that body deletes and creates two smaller bodies around its location.

problem is that the game randomly crashes when i'm colliding with more than a few bodies. i tried limiting the amount of bodies in the scene to less than 10 but then it still crashes just after a long period. All the crashes happen on collision of the play body with a different body and when i comment out the code that generates the bodies the crashing stops.

I do my spawning and deleting before world.step. look:

@Override
public void render() {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

BlobAlien.BirthChildren(this); // crashing stops when i delete this line
if(TimeUtils.timeSinceMillis(lastSpawn) > 5000) {
BlobAlien.SpawnRandom(this);
lastSpawn = TimeUtils.millis();
}

world.step(1 / 60f, 8, 3);
renderer.render(world, camera.combined);

batch.setProjectionMatrix(camera.combined);
batch.begin();

BlobAlien.RenderHorde(batch);
ship.render(batch);

batch.end();

}


and here are my static methods:

public static void RenderHorde(SpriteBatch batch) {
for(BlobAlien blob : blobs)
blob.render(batch);
}

public static void BirthChildren(App app) {
if(birthQueqe.empty())
return;

while(!birthQueqe.empty()) {
BlobAlien blob = birthQueqe.pop();
if(totalWeight >= WEIGHT_LIMIT) {
blob.CreateMe();
}
}
}

public static void CleanUpDeadBlobs(App app) {
}


here are mt non static method

private void CreateMe() {
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyDef.BodyType.DynamicBody;
bodyDef.gravityScale = 0.5f;
CircleShape ballShape = new CircleShape();
totalWeight += weight;

SetBody(app.world.createBody(bodyDef));
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = ballShape;
fixtureDef.density = 1;
fixtureDef.friction = 0;
body.createFixture(fixtureDef);
ballShape.dispose();

body.setTransform(startPos, 45);
sprite = new Sprite(app.blobImage);

birthDay = TimeUtils.millis();
}


thanks for reading any explanation on how Box2d works and what makes it crash would be greatly appreciated. I'n not expecting too much from a physics engine I think it should be able to do a lot more than what I'm trying to do here if it has such reputation and is so widely used. correct me if I'm wrong.

BTW I'm running java 8 on manjaro linux 64 bit using intellij idea and I'm aiming for this game to run on android phones.

thanks again :)

Edit

this is the console output of the crash

#
# A fatal error has been detected by the Java Runtime Environment:
#
#  SIGSEGV (0xb) at pc=0x00007ff5c0e0698b, pid=4700, tid=140693673539328
#
# JRE version: OpenJDK Runtime Environment (8.0_31-b13) (build 1.8.0_31-b13)
# Java VM: OpenJDK 64-Bit Server VM (25.31-b07 mixed mode linux-amd64 compressed oops)
# Problematic frame:
# C  [libgdx-box2d64.so+0x2198b]  b2BlockAllocator::Allocate(int)+0x4b
#
# Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
#
# /home/amit/Documents/blobmob/hs_err_pid4700.log
#
# If you would like to submit a bug report, please visit:
#   http://bugreport.java.com/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#


and this is the log file generated from the crash

http://pastebin.com/vCLYi98d

here is my collision listener

world.setContactListener(new ContactListener() {
@Override
public void beginContact(Contact contact) {
GameObject a = (GameObject)contact.getFixtureA().getBody().getUserData();
GameObject b = (GameObject)contact.getFixtureB().getBody().getUserData();
if(a!=null && b!=null) {
Vector2 contactPos = contact.getWorldManifold().getPoints()[0];
Vector2 va = contact.getFixtureA().getBody().getLinearVelocityFromWorldPoint(contactPos);
va.sub(contact.getFixtureB().getBody().getLinearVelocityFromWorldPoint(contactPos));
float force1 = va.len();
a.OnCollide(b, force1);
b.OnCollide(a, force1);
}
}

@Override
public void endContact(Contact contact) {
GameObject a = (GameObject)contact.getFixtureA().getBody().getUserData();
GameObject b = (GameObject)contact.getFixtureB().getBody().getUserData();
if(a!=null && b!=null) {
a.OffCollide(b);
b.OffCollide(a);
}
}

@Override
public void preSolve(Contact contact, Manifold oldManifold) {

}

@Override
public void postSolve(Contact contact, ContactImpulse impulse) {
Fixture fixtureA = contact.getFixtureA();
Fixture fixtureB = contact.getFixtureB();

GameObject a = (GameObject)contact.getFixtureA().getBody().getUserData();
GameObject b = (GameObject)contact.getFixtureB().getBody().getUserData();
if(a!=null && b!=null) {
if ((a.name.contains("HammerShip") && b.name.contains("Blob"))
|| (b.name.contains("HammerShip") && a.name.contains("Blob"))) {
if (impulse.getNormalImpulses()[0] < 1) {
StickyInfo si = new StickyInfo();
si.b = fixtureA.getBody();
si.a = fixtureB.getBody();
} else if (impulse.getNormalImpulses()[0] < 1) {
StickyInfo si = new StickyInfo();
si.b = fixtureB.getBody();
si.a = fixtureA.getBody();
}
}
}
}
});

• Show your collide function and stacktrace of crash. Feb 5 '15 at 15:28

Had the same problem with the following code:

private void restart() {
setScreen(new FooScreen);
}


Solved it simply by wrapping it in

private void restart() {
Gdx.app.postRunnable(() -> {
setScreen(screen);
});
}


That happens in native body creation, and in your code only these lines are directly connected with that:

BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyDef.BodyType.DynamicBody;
bodyDef.gravityScale = 0.5f;

SetBody(app.world.createBody(bodyDef));


Box2d manual recommends to set body positions from the beginning, avoiding putting multiple bodies to the same point.

Caution Do not create a body at the origin and then move it. If you create several bodies at the origin, then performance will suffer.

It speaks only of performance, not crashes, but since the problem is related to body creation, and I can't see any other problems with your code, why don't you add something like this:

  bodyDef.position.set(startPos)


and see if that fixes it.

• First of all thanks for the response. I tried what you have suggested but still i did not manage to solve the problem. In my game there a collisions which spawn more bodies and there are collisions which don't, I noticed that my game crashes only on collision but not just collisions that cause spawning, also collisions that don't. I dont know if that helps but thanks any way Feb 20 '15 at 22:16
• Could you post your collisions listener then? From where you supposedly call these body creation methods you already posted. Feb 22 '15 at 1:21