为什么我在LIbgdx中创建实体时有时会有box2Dcrash

时间:2017-01-20 14:42:53

标签: java libgdx box2d

我一直在经常"随机"我正在开发的libgdx迷你游戏中的致命错误。对于我收集的内容,它是一个box2d本机错误,与身体创建有关。造成这种情况的一个可能原因是在一个时间步长期间产生了一个身体,但我相信我采取了适当的对策。

以下情况似乎相似,但实际上是关于在循环中从数组中删除对象:Deleting and creating body in libGDX

关于我自己的问题,这是崩溃的输出:

<控制台>

EntityManager: entity generation begin ---------
IceSpike: generating physics
IceSpike: generated physics
IceSpike: generating graphics
EntityManager: entity generation registered
IceSpike: generating physics
IceSpike: generated physics
IceSpike: generating graphics
EntityManager: entity generation registered
IceSpike: generating physics
#
# A fatal error has been detected by the Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x0000000066bcbd0d, pid=5288, tid=0x00000000000013d4
#
# JRE version: Java(TM) SE Runtime Environment (8.0_111-b14) (build 1.8.0_111-b14)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (25.111-b14 mixed mode windows-amd64 compressed oops)
# Problematic frame:
# C  [gdx-box2d64.dll+0xbd0d]
#
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
#
# An error report file with more information is saved as:
# D:\Codage\projects\eclipse\git\spacegame\SpaceGame\android\assets\hs_err_pid5288.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.
#
AL lib: (EE) alc_cleanup: 1 device not closed

日志文件:http://pastebin.com/JjBGnTri

说到代码,我会尽量做到尽可能简单。希望我没有削减部分问题。

我有一个EntityManager来处理我的实体的生成和销毁。游戏中的所有实体都继承自Entity(一个大的抽象blob)。它们都有一个box2d体,它是在generatePhysics(World physics)生成的。

的EntityManager

private GameWorld world;
private Array<Entity> generation;
private Array<Entity> generationCpy;
private Array<Entity> trash;
private Array<Entity> trashCpy;

public void update(float delta)
{
    //generate new entities
    Gdx.app.debug(this.getClass().getSimpleName(), "entity generation begins ---------");
    if(generation.size != 0)
    {
        generationCpy = new Array<Entity>(generation);
        for (Entity entity : generationCpy)
        {
            entity.generate(world.getPhysics(),world.getAssetManager());
        }
    }

    //destroy old entities
    Gdx.app.debug(this.getClass().getSimpleName(), "entity destruction begins ---------");
    if(trash.size != 0)
    {
        trashCpy = new Array<Entity>(trash);
        for(Entity entity : trashCpy)
        {
            trash.removeValue(entity, true);
            entity.destroy(world.getPhysics());
            entity = null;
        }
    }
    Gdx.app.debug(this.getClass().getSimpleName(), "entity generation complete ------");

实体

public final void generate(World physics, AssetManager assetManager)
{
    if(state != EntityState.GENERATED)
    {
        generatePhysics(physics);
        generateGraphics(assetManager);
        setState(EntityState.GENERATED);
    }
    else
        Gdx.app.error(this.getClass().getSimpleName(), "entity couldn't be generated. It had allready been generated");
}

到目前为止,只有尖峰似乎受到关注,而且大多数时候,一切都顺利进行。其余时间,这是崩溃的部分:

IceSpikes

@Override
public void generatePhysics(World physics)
{
    Gdx.app.debug(this.getClass().getSimpleName(), "generating physics");

    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyType.DynamicBody;
    bodyDef.position.set(this.position.cpy().scl(1 / PPM));

    body = physics.createBody(bodyDef);
    body.setBullet(true);

    PolygonShape shape = new PolygonShape();
    Vector2 vertices[] = new Vector2[3];
    vertices[0] = new Vector2(-width/2,-height/2).scl(1f/PPM);
    vertices[1] = new Vector2(0,height/2).scl(1f/PPM);
    vertices[2] = new Vector2(width/2,-height/2).scl(1f/PPM);
    shape.set(vertices);        

    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = shape;
    fixtureDef.density = 1f;
    fixtureDef.restitution = 1f;
    fixtureDef.filter.categoryBits = CollisionManager.BIT_PROJECTILE;
    fixtureDef.filter.maskBits = CollisionManager.BIT_PLAYER;

    Fixture fixture = body.createFixture(fixtureDef);
    fixture.setUserData(this);

    shape.dispose();

    Gdx.app.debug(this.getClass().getSimpleName(), "generated physics");
}

这表明它是由于IceSpikes的时间创建,它是在碰撞之后(在world.step()中)。但是,这正是我整个实体管理系统的重点。它生成/移除world.step()之外的实体,如输出所示。

此外,复制数组的设置是为了防止实体在循环播放时从生成和垃圾数组中删除。

我一定忽视了什么,但是什么?知道如何从那里开始吗?

3 个答案:

答案 0 :(得分:0)

创建和删除实体(基本上有物理组件)应该用锁定绑定。

在物理世界解锁时必须执行创建和删除。

private World physicsWorld;
private PhysicsSystem physicsSystem;

@Override
protected void initialize() {

    physicsWorld =physicsSystem.getPhysicsWorld();
}

@Override
protected void processSystem() {

    if(!physicsWorld.isLocked()) {
          removeBodyFromWorld();    
          addBodyIntoWorld();
    }
}

在removeBodyFromWorld方法中删除所需的实体并在addBodyIntoWorld方法中创建新的主体。

希望它可能会有所帮助。
感谢

答案 1 :(得分:0)

至于您是否忽略了某些内容,从此处发布的代码中,您忽略shape关于verticesgeneratePhysics方法)。

指示多边形指示使用这些顶点(或任何其他顶点)。但是,在创建将使用多边形的夹具之前,必须设置3个或更多顶点。

答案 2 :(得分:0)

问题原因是由于我的entityManager中的多个引用导致同一主体的多次创建/销毁。我的实体管理系统基于听众。每个实体都有一个枚举EntityState,当状态发生变化时,实体管理器会被通知。我以为我确保这个状态在其余的代码中只改变一次,但事实证明,它没有。

一个简单的解决方法是添加if(this.state != state)条件。

以下是现在的样子:

public final void setState(EntityState state)
{
    if(this.state == state)
        Gdx.app.error(this.getClass().getSimpleName(), "the state was allready " + state);
    else
    {
        this.state = state;
        switch(state)
        {
            case CREATED:       notifyCreation(this);       break;
            case GENERATED:     notifyGeneration(this);     break;
            case TO_BE_REMOVED: notifyFlagForRemoval(this); break;
            case INACTIVE:      notifyDestruction(this);    break;
            default : Gdx.app.error(this.getClass().getSimpleName(), "entity state became " + state);
        }
    }
}

然而,&#34;为什么&#34;我仍然没有想到,因为我已经采取了一些对策,比如在摧毁之前检查身体是否为空:

protected final void destroyBody(World physics)
{
  Gdx.app.debug(this.getClass().getSimpleName(), "destroying body" );
  if(body != null)
  {
     final Array<JointEdge> joints = body.getJointList();
     while (joints.size > 0)
     {
        Gdx.app.log("GameWorld", "destroying joint" );
        Joint joint = joints.get(0).joint;
        physics.destroyJoint(joint);
        joints.removeIndex(0);
     }
     physics.destroyBody(body);
  }
  else
  {
     Gdx.app.error(this.getClass().getSimpleName(), "entity's body not found. No action taken." );
  }

}

就我而言,这是固定的,即使修复程序对我的情况非常具体,对我来说也有点不耐烦。