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.CleanUpDeadBlobs(this);
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();
blobs.add(blob);
}
}
}
public static void CleanUpDeadBlobs(App app) {
killCount += deadQueqe.size();
while(!deadQueqe.empty())
app.world.destroyBody(deadQueqe.pop().body);
}
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();
ballShape.setRadius(size);
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
#
# An error report file with more information is saved as:
# /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
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();
weldsToJoin.add(si);
} else if (impulse.getNormalImpulses()[0] < 1) {
StickyInfo si = new StickyInfo();
si.b = fixtureB.getBody();
si.a = fixtureA.getBody();
weldsToJoin.add(si);
}
}
}
}
});