LibGdx摧毁一个身体

时间:2014-04-08 10:33:15

标签: java android libgdx box2d game-physics

我编写了以下代码,通过计算冲动力来破坏碰撞后的特定对象,但是当游戏试图销毁显示错误的对象时游戏崩溃: 断言失败:(IsLocked() == false)function DestroyBody, 文件/Users/badlogic/jenkins/workspace/libgdx-mac/gdx/jni/Box2D/Dynamics/b2World.cpp,第134行。

请帮助。提前谢谢。

package com.me.mygdxgame;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL30;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
import com.badlogic.gdx.physics.box2d.Contact;
import com.badlogic.gdx.physics.box2d.ContactImpulse;
import com.badlogic.gdx.physics.box2d.ContactListener;
import com.badlogic.gdx.physics.box2d.Manifold;

public class MyGdxGame implements ApplicationListener {

private Levels level1;
private Box2DDebugRenderer renderer;
private Vector2 groundPositionLeft, groundPositionRight;
private SpriteBatch batch;
private Sprite sprite;
private boolean[] a;
private Input input;
private ContactListener conlis;

@Override
public void create() {
    contactlistener();
    level1 = new Levels();
    renderer = new Box2DDebugRenderer();
    batch = new SpriteBatch();
    input = new Input() {
    };
    groundPositionLeft = new Vector2(
            (-0.04f * Gdx.graphics.getWidth() / 2f), -28 / 720f
                    * Gdx.graphics.getHeight() / 2f * (6 / 7f));
    groundPositionRight = new Vector2(0.04f * Gdx.graphics.getWidth() / 2f,
            -28 / 720f * Gdx.graphics.getHeight() / 2f * (6 / 7f));
    level1.createIceHor(0, groundPositionLeft.y + 11.25f);
    sprite = new Sprite(new Texture("img/Metal.jpg"));
    sprite.setSize(4.5f, 2f);
    sprite.setOrigin(sprite.getWidth() / 2f, sprite.getHeight() / 2f);

}

@Override
public void dispose() {
}

@Override
public void render() {
    Gdx.gl.glClearColor(1, 1, 1, 1);
    Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
    level1.worldStep();

    batch.setProjectionMatrix(level1.getCamera().combined);
    batch.begin();
    batch.end();
    renderer.render(level1.getLevel(), level1.getCamera().combined);
    level1.getLevel().setContactListener(conlis);
    Gdx.input.setInputProcessor(input);

}

@Override
public void resize(int width, int height) {
}

@Override
public void pause() {
}

@Override
public void resume() {
}

private void contactlistener() {
    conlis = new ContactListener() {

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

        }

        @Override
        public void postSolve(Contact contact, ContactImpulse impulse) {
            // System.out.println(impulse.getNormalImpulses()[0]);
            if (impulse.getNormalImpulses()[0] >= 22.648895f) {
                if (contact.getFixtureA().getFriction() == 0.1f) {
                    level1.getLevel().destroyBody(
                            contact.getFixtureA().getBody());
                } else {
                    level1.getLevel().destroyBody(
                            contact.getFixtureB().getBody());
                }

            }
        }

        @Override
        public void endContact(Contact contact) {

        }

        @Override
        public void beginContact(Contact contact) {
        }
    };
}

}

1 个答案:

答案 0 :(得分:3)

看看this。它是为C ++编写的,但一般概念仍然适用。

当Box2D正在进行模拟步骤时,您不能销毁任何东西。在进行此模拟时会调用ContactListener,并立即破坏正文。这是不允许的。您必须存储要销毁的实体,并在world.step之后立即执行此操作。

Here您可以看到它是如何在测试中作为快速解决方案完成的,但它可能不是最佳解决方案。