gpt4 book ai didi

android - 我的 box2d body 下降太慢了

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

我几乎尝试了所有方法,但没有任何效果。如果你运行它,你会看到一个正方形非常缓慢地掉落。我是初学者,所以解释起来不要太复杂。关于此代码的任何问题 dylan.missu@gmail.com这是我的代码:

@Override
public void show() {
camera = new OrthographicCamera();
debugRenderer = new Box2DDebugRenderer();

BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyDef.BodyType.DynamicBody;
bodyDef.position.set(100,100);
body = world.createBody(bodyDef);
PolygonShape shape = new PolygonShape();
shape.setAsBox(Gdx.graphics.getHeight()/6,Gdx.graphics.getHeight()/6);
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = shape;
fixtureDef.density = 1f;
fixtureDef.restitution = 2;
Fixture fixture = body.createFixture(fixtureDef);
}
private void line(float X, float Y, float w, float h)
{
BodyDef bodyDef = new BodyDef();
bodyDef.type=BodyDef.BodyType.StaticBody;
bodyDef.position.set(X,Y);
PolygonShape polygonShape=new PolygonShape();
polygonShape.setAsBox(w,h);
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape=polygonShape;
fixtureDef.restitution=0.4f;
fixtureDef.friction=0.5f;
Body theFloor=world.createBody(bodyDef);
theFloor.createFixture(fixtureDef);
}
private void wall(float A)
{
// is there a better way of doing this?
line(0,Gdx.graphics.getHeight()/2-A,Gdx.graphics.getWidth()/2-A,0);
line(Gdx.graphics.getWidth()/2-A,0,0,Gdx.graphics.getHeight()/2-A);
line(0,-Gdx.graphics.getHeight()/2+A,Gdx.graphics.getWidth()/2-A,0);
line(-Gdx.graphics.getWidth()/2+A,0,0,Gdx.graphics.getHeight()/2-A);
}
@Override
public void render(float delta) {
world.step(1 / 5f, 6, 2);

OrthographicCamera camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

processAccelerometer();
wall(1);

Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
debugRenderer.render(world, camera.combined);
}

@Override
public void dispose() {
world.dispose();
debugRenderer.dispose();
}

private void processAccelerometer() {

float y = Gdx.input.getAccelerometerY();
float x = Gdx.input.getAccelerometerX();

if (prevAccelX != x || prevAccelY != y) {

world.setGravity(new Vector2(y, -x));

prevAccelX = x;
prevAccelY = y;
}
}
@Override
public void hide()
{
// TODO: Implement this method
}

@Override
public void resize(int p1, int p2)
{
// TODO: Implement this method
}

@Override
public void resume()
{
// TODO: Implement this method
}

@Override
public void pause()
{
// TODO: Implement this method
}

@Override
public void render()
{
// TODO: Implement this method
}

最佳答案

在 Box2D 中,您必须考虑 1 像素 = 1 米。所以,基本上,您在 Box2D 中模拟的所有内容默认情况下都是巨大的。并且 Box2D 模拟对于大距离、大质量、大速度...不准确...

因此,您所要做的就是使用转换因子转换视口(viewport),这样您就可以看到更容易模拟的小实体。

例如,假设您想要 100 像素 = 1 米,您可以在创建游戏画面时输入该代码:

WORLD_TO_BOX = 1/100f;  
BOX_TO_WORLD = 1/WORLD_TO_BOX;

//Creation of the camera with your factor 100
camera = new OrthographicCamera();
camera.viewportHeight = Gdx.graphics.getHeight() * WORLD_TO_BOX;
camera.viewportWidth = Gdx.graphics.getWidth() * WORLD_TO_BOX;
camera.position.set(camera.viewportWidth/2, camera.viewportHeight/2, 0f);
camera.update();

然后,您将根据 camera.viewportWidth 创建您的盒子和 camera.viewportHeight , 而不是 Gdx.graphics.getWidth()Gdx.graphics.getHeight()

根据您的情况,您将拥有:

PolygonShape shape = new PolygonShape();
shape.setAsBox(camera.viewportHeight/6,camera.viewportHeight/6);

你可以在我的代码中看到,还有一个BOX_TO_WORLD转换系数。当你想在你的 box2D 物体上渲染图形时会用到它。为此,请查看 answer of this question .

关于android - 我的 box2d body 下降太慢了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32058495/

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