gpt4 book ai didi

java - libGDX 正交相机 View 尺寸(使用 Box2D)

转载 作者:太空宇宙 更新时间:2023-11-04 12:34:56 26 4
gpt4 key购买 nike

我有一个简单的类(class),屏幕周围有一个球和四堵墙:

SpriteBatch batch;
Box2DDebugRenderer debugRenderer;
World world;
OrthographicCamera camera;

public float VIRTUAL_WIDTH = 720f;
public float VIRTUAL_HEIGHT = 1280f;

@Override
public void create () {

batch = new SpriteBatch();

camera = new OrthographicCamera(VIRTUAL_WIDTH, VIRTUAL_HEIGHT);
world = new World(new Vector2(0, -9), true);

debugRenderer = new Box2DDebugRenderer();

BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyDef.BodyType.DynamicBody;

CircleShape circleShape = new CircleShape();
circleShape.setRadius(50);

FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = circleShape;
fixtureDef.restitution = 1;

Body body = world.createBody(bodyDef);
body.createFixture(fixtureDef);

createWall(0f, -VIRTUAL_HEIGHT/2, VIRTUAL_WIDTH/2, 30f);
createWall(0f, VIRTUAL_HEIGHT/2, VIRTUAL_WIDTH/2, 30f);
createWall(-VIRTUAL_WIDTH/2, 0f, 30f, VIRTUAL_HEIGHT/2);
createWall(VIRTUAL_WIDTH/2, 0f, 30f, VIRTUAL_HEIGHT/2);

}

private void createWall(float x, float y, float hx, float hy){

BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyDef.BodyType.StaticBody;
bodyDef.position.set(x, y);

PolygonShape polygonShape = new PolygonShape();
polygonShape.setAsBox(hx, hy);

FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = polygonShape;

world.createBody(bodyDef).createFixture(fixtureDef);

}

@Override
public void render () {

camera.update();

Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

world.step(1 / 60.0f, 8, 3);
batch.setProjectionMatrix(camera.combined);

batch.begin();
debugRenderer.render(world, camera.combined);
batch.end();

}

它在我的设备上执行下一个屏幕(可能不是完美的屏幕截图):

enter image description here

屏幕稍微向左移动,是因为舍入错误吗?

更新:

enter image description here

最佳答案

而不是这个

public float VIRTUAL_WIDTH  = 720f;
public float VIRTUAL_HEIGHT = 1280f;

试试这个

public float VIRTUAL_WIDTH  = Gdx.graphics.getWidth();
public float VIRTUAL_HEIGHT = Gdx.graphics.getHeight();

关于java - libGDX 正交相机 View 尺寸(使用 Box2D),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37428583/

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