【问题标题】:LibGdx Destroying a BodyLibGdx 破坏身体
【发布时间】:2014-05-20 23:49:36
【问题描述】:

我编写了以下代码,通过计算其脉冲力在碰撞后摧毁特定对象,但每当它试图摧毁显示错误的对象时游戏就会崩溃: 断言失败:(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) {
        }
    };
}

}

【问题讨论】:

    标签: java android libgdx box2d game-physics


    【解决方案1】:

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

    在 Box2D 执行模拟步骤时,您不得破坏任何东西。您的 ContactListener 在此模拟发生时被调用,您会立即破坏身体。这是不允许的。您必须存储要销毁的尸体并在 world.step 之后立即执行此操作。

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

    【讨论】:

      猜你喜欢
      • 2013-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-29
      • 1970-01-01
      相关资源
      最近更新 更多