gpt4 book ai didi

java - 删除body box2d

转载 作者:行者123 更新时间:2023-12-02 04:55:32 25 4
gpt4 key购买 nike

谁能告诉我为什么会返回错误

“AL 库:(EE) alc_cleanup:1 个设备未关闭”

//mouseJoint碰撞回调

private QueryCallback queryCallback = new QueryCallback() {

@Override
public boolean reportFixture(Fixture fixture) {


if(fixture.getBody() == chest){

//add to remove list

bodiesToRemove.add(chest);

}

if (fixture.testPoint(tmp.x, tmp.y)){


reportFixture = fixture.getBody();
}


if (!fixture.testPoint(tmp.x, tmp.y))
return false;

//assigning bodyB to fixture
jointDef.bodyB = fixture.getBody();
jointDef.target.set(fixture.getBody().getWorldCenter());

//jointDef.target.set(tmp.x, tmp.y);// initial target point coincide with body anchor

joint = (MouseJoint) world.createJoint(jointDef);// creating the join the physics world
return false;
}

};

//主渲染循环

public void render(float delta) {
// TODO Auto-generated method stub
//code clears the screen with the given RGB colour (black)
Gdx.gl.glClearColor( 0f, 0f, 0f, 1f );
Gdx.gl.glClear( GL20.GL_COLOR_BUFFER_BIT );


stage.setCamera(camera);
stage.act(Gdx.graphics.getDeltaTime());




camera.position.x= chest.getPosition().x;


camera.update();

stage.draw();



world.step(1 / 60f, 8, 3);
renderer.render(world, camera.combined);


/////////////////////////////////////////////////////////////////

//这是当我尝试删除正文时导致错误的代码

    if(bodiesToRemove != null){

for(int i = 0; i <bodiesToRemove.size; i++){

Body b = bodiesToRemove.get(i);

if(b != null){
world.destroyBody(b);
b.setUserData(null);
b = null;
}
bodiesToRemove.clear();
}
}

////////////////////////////////////////////////////////////
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////我已经改变了这一点并且它有效(粘贴在下面),如果有人想解释执行此操作的正确方法或者如果这是正确的方法将会有所帮助。我刚刚添加了一个 boolean 值以防止它破坏关节

// ////////////////// TOUCH EVENT ///////////////////////////////
//Called when a finger was lifted or a mouse button was released.
@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
//If no mouseJoint do nothing

System.out.println("touch" + (screenY*0.01f));
System.out.println("touch" + (screenX*0.01f));



if (joint == null)
return false;


//When user is not touching Screen destroy the
//mouseJoint/set to null
if(ran == false){

world.destroyJoint(joint);
joint = null;}


ran = false;
return true;
}



////////////////////////////////

//////////////////////////////////

    if(!world.isLocked() && world.getBodyCount() > 0 ){

for(int i = 0; i <bodiesToRemove.size; i++){

Body b = bodiesToRemove.get(i);

if(b == null){
bodiesToRemove.removeIndex(i);
continue;
}
world.destroyBody(b);
ran = true;
bodiesToRemove.removeIndex(i);

}

}else{

bodiesToRemove.clear();
}

////////////////////////////////////////////////////////////

最佳答案

Box2D 抛出一些奇怪的错误,这不是其中之一。

我假设你正在使用 LibGDX,基本上发生的事情是由于 Box2D 导致的崩溃(我还假设它弹出说“java 或其他已经停止响应”),当应用程序退出时,你看到的这个错误是抛出的OpenAL 正在关闭流并清理资源。别太担心这个。

确保您在销毁尸体之前检查世界已锁定,它通常会抛出错误并打印一条明确的消息告诉您这一点,具体取决于您删除尸体的方式。

if(!world.isLocked())
// do clean up code here

您不需要清理本地范围内的变量,或清除主体内的用户数据作为主体,因为它将不再存在于引用中(除非您将其存储在其他地方)。

所以正确的代码应该是这样的:

if(!world.isLocked()){
for(int i = bodiesToRemove.size - 1; i--){
Body b = bodiesToRemove.get(i);
if(b == null){
bodiesToRemove.removeAt(i);
continue;
}
world.destroyBody(b);
bodiesToRemove.removeAt(i);
}
}

您还在迭代期间清除列表,这不是一个好主意。

编辑

我能想到的唯一导致此问题的另一件事是尝试删除实际上不在世界主体列表中的主体。

尝试以下操作:

// Ensure world is not locked AND that there is even bodies in the world
if(!world.isLocked() && world.getBodyCount() > 0){
for(int i = bodiesToRemove.size - 1; i--){
Body b = bodiesToRemove.get(i);
if(b == null){
bodiesToRemove.removeAt(i);
continue;
}
world.destroyBody(b);
bodiesToRemove.removeAt(i);
}
}else{
// There is no bodies in the world, so the bodies in our list are just
// random memory hogs, leave them for the GC by clearing the list
bodiesToRemove.clear();
}

编辑

正如OP所述,问题出在这段代码上:

私有(private) QueryCallback queryCallback = new QueryCallback() {

@Override
public boolean reportFixture(Fixture fixture) {
if(fixture.getBody() == chest){
//add to remove list
bodiesToRemove.add(chest);
}
if (fixture.testPoint(tmp.x, tmp.y)){
reportFixture = fixture.getBody();
}
if (!fixture.testPoint(tmp.x, tmp.y))
return false;
//assigning bodyB to fixture
jointDef.bodyB = fixture.getBody();
jointDef.target.set(fixture.getBody().getWorldCenter());
//jointDef.target.set(tmp.x, tmp.y);
joint = (MouseJoint) world.createJoint(jointDef);
return false;
}

};

更具体地说,这一行:

    joint = (MouseJoint) world.createJoint(jointDef);

查询回调在全局步骤期间触发。 在 Box2D 中,您无法在世界步骤中创建或销毁实体、固定装置和关节。

您必须在此方法之外创建它,最简单的方法是通过标志。将标志设置为 true 并将固定装置的实例存储在某处,并使用所述标志和固定装置在游戏循环外部处理它。

关于java - 删除body box2d,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28808083/

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