gpt4 book ai didi

java - LibGdx 摧毁一个 body

转载 作者:行者123 更新时间:2023-11-29 05:25:11 26 4
gpt4 key购买 nike

我编写了以下代码,通过计算碰撞后的冲量来销毁特定对象,但游戏在试图销毁对象时崩溃并显示错误:断言失败:(IsLocked() == false)函数 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) {
}
};
}

最佳答案

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

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

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

关于java - LibGdx 摧毁一个 body ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22934386/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com