简体   繁体   中英

Libgdx Box2d Java Crash - jniCreateBody

I'm playing with Libgdx since a while now, but encountered this problem the first time.

My Problem: Java environment crashes when I try to create a Body. Error Log:

Java frames: (J=compiled Java code, j=interpreted, Vv=VM code) 
    j com.badlogic.gdx.physics.box2d.World.jniCreateBody(JIFFFFFFFFZZZZZF)J+0
j  com.badlogic.gdx.physics.box2d.World.createBody(Lcom/badlogic/gdx/physics/box2d/BodyDef;)Lcom/badlogic/gdx/physics/box2d/Body;+80
j  xxx.xx.box2dTest.Enteties.SmallBee.init(FF)V+60
j  xxx.xx.box2dTest.Enteties.SmallBee.<init>(Lcom/badlogic/gdx/physics/box2d/World;Lcom/badlogic/gdx/graphics/OrthographicCamera;)V+112
J 1268 C1 xxx.xx.box2dTest.Organization.Ingame.Level.BossOne.BossOneWorld.spawnSmallBee()V (48 bytes) @ 0x000000000323e174 [0x000000000323dec0+0x2b4]
j  xxx.xx.box2dTest.Organization.Ingame.Level.BossOne.BossOne.extraStuff()V+223
J 1176 C1 xxx.xx.box2dTest.Organization.Ingame.Level.MotherLevel.render(F)V (269 bytes) @ 0x00000000031e1a0c [0x00000000031e0460+0x15ac]
J 1175 C1 xxx.xx.box2dTest.Organization.Ingame.Level.BossOne.BossOne.render(F)V (499 bytes) @ 0x00000000031d994c [0x00000000031d98c0+0x8c]
J 1144 C1xxx.xx.box2dTest.Box2DGame.render()V (5 bytes) @ 0x00000000031cb744 [0x00000000031cb520+0x224]
j  com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop()V+684
j  com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run()V+27
v  ~StubRoutines::call_stub

What I do: I create every now and then a body. Most of the time all is okay, still crashes SOME times.. The first creation never failed until now.

  public void spawnSmallBee(){
    if(TimeUtils.millis() - smallBeeTimer > 0){
        new SmallBee(getWorld(), getCamera());
        smallBeeTimer = TimeUtils.millis() + MathUtils.random(2000, 3000);
    }
}

this code snippet is where I create the Object, that will create the Body

public SmallBee(World world, OrthographicCamera camera) {
    this.world = world;
    this.camera = camera;
    MOVING_SPEED_Y= MathUtils.random(-3, 3);

    sprite = new Sprite(MyAssets.SMALL_BEE);
    sprite.setSize(WIDTH * 2, HEIGHT * 2);
    sprite.setOriginCenter();
    myEntity = new MyEntity("Bee", sprite);
    myEntity.setObject(this);

    float posx = camera.position.x - camera.viewportWidth / 2 - 2;
    float posy = MathUtils.random(1, 7);

    BodyDef platformDef = new BodyDef();
    platformDef.type = BodyDef.BodyType.KinematicBody;
    platformDef.position.set(posx, posy);
    body = world.createBody(platformDef);
    PolygonShape platformBox = new PolygonShape();
    platformBox.setAsBox(WIDTH, HEIGHT);

    body.createFixture(platformBox, 0.0f);

    body.setLinearVelocity(new Vector2(MOVING_SPEED_X, MOVING_SPEED_Y));
    myEntity.getSprite().setPosition(body.getPosition().x - WIDTH, body.getPosition().y - HEIGHT);
    body.setUserData(myEntity);
    // Remember to dispose of any shapes after you're done with them!
    // BodyDef and FixtureDef don't need disposing, but shapes do.
    platformBox.dispose(); }

this code snippet is the creation of the Object

As example, i got nearly the same Object, that doesnt crash

creation is one line before bee creation

public void spawnArrowShooter() {
    if(TimeUtils.millis() - arrowShooterTimer > 0){
        new ArrowShooter(getWorld(), MathUtils.random(200 - getCamera().viewportWidth / 2 + 2, 200 + getCamera().viewportWidth / 2 - 2), 3, getPlayer(), getCamera(), bossWasp);
        arrowShooterTimer = TimeUtils.millis() + MathUtils.random(10000, 15000);
    }
}

the initiation:

 public ArrowShooter(World world, float x, float y, Player player, OrthographicCamera camera, BossWasp bossWasp) {
    this.camera = camera;
    this.bossWasp = bossWasp;
    //PLATTFORM
    this.world = world;
    this.player = player;

    sprite = new Sprite(MyAssets.ARROW);
    sprite.setSize(WIDTH * 2, HEIGHT * 2);

    myEntity = new MyEntity("Arrow", sprite);
    myEntity.setObject(this);



    init(x, y);

}


 public void init(float x, float y){
    BodyDef platformDef = new BodyDef();
    // We set our body to dynamic, for something like ground which doesn't move we would set it to StaticBody
    platformDef.type = BodyDef.BodyType.DynamicBody;

    platformDef.position.set(x, y);
    body = world.createBody(platformDef);
    PolygonShape platformBox = new PolygonShape();
    platformBox.setAsBox(WIDTH, HEIGHT);
    Fixture fixture = body.createFixture(platformBox, 0.0f);
    myEntity.getSprite().setPosition(body.getPosition().x - WIDTH, body.getPosition().y - HEIGHT);

    body.setUserData(myEntity);
    // Remember to dispose of any shapes after you're done with them!
    // BodyDef and FixtureDef don't need disposing, but shapes do.
    platformBox.dispose();
    disappearTime = TimeUtils.millis() + 4000;
}

I cant figure the problem out..

if(world.isLocked()){
  return;
}

doesnt help either.

The body is 100% not created on another Object.

I'll try to investigate the Problem further, and provide any new informations that I could gather.

Please try to help me!

I realised, that I could spawn millions of Bees in seconds, without Problems, so I created about 10 every second. No crashes, till the arrow hit the Boss and got destroyed - immidiately game crash.

I watched earlier for some destroyed body modification, but I missed one line..

The problem was: I had set the velocity from a destroyed body!

No more crashes until now.

As I had read in another Forum:

"When you destroy a body, do: body = null This will help you to find your fault, if not you may get some hard to understand errors"

Hope you can get help from this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM