如何在JBox2D中删除一个简单的主体?

时间:2014-07-03 09:28:57

标签: libgdx box2d jbox2d

每当我试图在JBox2D中摧毁一个身体时,它总是给我一个NullPointerException,我已经在网上查看过,并且他们“安全地”展示了我不明白的各种方法。我想知道是否有人可以输入一些简单的伪代码来展示你如何在Box2D中摧毁一个物体。我也在使用LibGDX API。

这是我使用的代码:

public void render(float delta) {

    if(bodiesContainer.size() > 0)
    {
        for(Body body:bodiesContainer)
        {
            world.destroyBody(body);
        }
    }

    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);


    stage.act(delta);
    stage.draw();


    world.step(TIMESTEP, VELOCITYITERATIONS, POSITIONITERATIONS);

    camera.update();

    debugRenderer.render(world, camera.combined);

    batch.begin();
    batch.end();

}

 public void show() {
    // TODO Auto-generated method stub
    batch = new SpriteBatch();
    camera = new OrthographicCamera(Gdx.graphics.getWidth()/5, Gdx.graphics.getHeight()/5);
    world = new World(new Vector2(0,0f), true);
    debugRenderer = new Box2DDebugRenderer();
    stage = new Stage();

    particleCreator = new ParticleCreator(0, 0, 1);
    particleCreator.destroyParticle(world, particleBody);

    if (particleCreator == null)
    {
        System.out.println("true");
    }

    if (particleBody == null)
    {
        System.out.println("true particleBody");
    }

    if (particleBodyDef == null)
    {
        System.out.println("true particleBodyDef");
    }


    Gdx.input.setInputProcessor(new InputProcessor() {

        @Override
        public boolean touchUp(int arg0, int arg1, int arg2, int arg3) {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public boolean touchDragged(int arg0, int arg1, int arg2) {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public boolean touchDown(int arg0, int arg1, int arg2, int arg3) {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public boolean scrolled(int arg0) {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public boolean mouseMoved(int arg0, int arg1) {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public boolean keyUp(int arg0) {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public boolean keyTyped(char arg0) {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public boolean keyDown(int keycode) {
            switch(keycode) {
            case Keys.SPACE:

                break;
            }
            return false;
        }
    });
}

public class ParticleCreator 
{   



//Default constructor
public ParticleCreator()
{

}

public ParticleCreator(World world, BodyDef bodyDef, Body body, float position_x, float position_y, float particle_radius)
{
    bodyDef = new BodyDef();
    bodyDef.type = BodyType.DynamicBody;
    bodyDef.position.set(position_x, position_y);

    CircleShape circleShape = new CircleShape();
    circleShape.setRadius(particle_radius);

    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = circleShape;
    fixtureDef.restitution = 0;

    body = world.createBody(bodyDef);
    body.createFixture(fixtureDef);
}

public void destroyParticle(World world, Body body)
{
    world.destroyBody(body);
    System.out.println("This Particle has been destroyed !!!");
}

}

bodiesContainer是一个ArrayList,而particleBody是我希望销毁的JBox2D体,但每次按空格键时它都会崩溃并给我一个NullPointerException。

0 个答案:

没有答案