gpt4 book ai didi

java - Box2D静态体碰撞性能问题

转载 作者:搜寻专家 更新时间:2023-11-01 03:54:06 26 4
gpt4 key购买 nike

我正在使用 JBox2d 在我正在处理的游戏项目中执行碰撞检测。我用静态物体代表世界上的障碍。每当动态 body (即游戏角色)与这些障碍之一发生碰撞时,性能就会出现非常明显的下降。 Fps 将从 ~120 下降到 ~5。当静态物体的角部受到碰撞时,这种情况似乎更常发生。

当我将世界障碍物的主体类型设置为动态而不是静态,具有非常高的密度(以防止 body 在碰撞时移动)时,这个问题消失了......这个解决方案并不理想然而我的情况......

关于可能导致这种巨大的 fps 下降的原因有什么想法吗?

这是我用来创建静态物体的代码:

BodyDef def = new BodyDef();
def.type = BodyType.STATIC; // If this line is commented and the other
//commented lines are uncommented, the issue goes away.
//def.type = BodyType.DYNAMIC;
def.position.set(worldBounds.getCenterX(), worldBounds.getCenterY());
Body staticBody = b2World.createBody(def);

PolygonShape box = new PolygonShape();
box.setAsBox(worldBounds.getWidth() * 0.5f, worldBounds.getHeight() * 0.5f);

FixtureDef fixture = new FixtureDef();
fixture.shape = box;
fixture.friction = 0.3f;
//fixture.density = 1000000000;
staticBody.createFixture(fixture);
//staticBody.setSleepingAllowed(true);
//staticBody.setFixedRotation(true);

我试过使用 CircleShape 而不是 PolygonShape,但这没有任何帮助。

谢谢!

最佳答案

这是我目前正在开发的游戏的代码,运行良好。希望如果您复制并粘贴,更改一些变量名称和内容,它可能会解决您的问题。我是 box2d 的新手,所以无法准确告诉您问题出在哪里。希望对您有所帮助。

    //bodydef
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyType.StaticBody;
bodyDef.position.set(position);
body = world.createBody(bodyDef);

//shape
PolygonShape shape = new PolygonShape();
shape.setAsBox(dimension.x / 2, dimension.y / 2);

//fixture
FixtureDef fixture = new FixtureDef();
fixture.friction = 0.3f;
fixture.shape = shape;

body.createFixture(fixture);
shape.dispose();

关于java - Box2D静态体碰撞性能问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13979069/

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