Libgdx缩放不同设备的坐标

时间:2015-08-19 10:38:26

标签: java libgdx

我做了一个简单的游戏,你点击一个对象,它应该消失。它在尺寸为240 x 480的桌面上工作正常,但在我的手机上尺寸更宽,如1920 x 1080所以触地坐标不同所以在桌面上它可能会记录100 x 50的触摸,但如果我在手机上点击同一个地方它将是400 x 200,所以我只想缩放它们或使用它们。

private double WidthScale = (272 / Gdx.graphics.getWidth());
private double HeightScale = (408 / Gdx.graphics.getHeight());

private Array<Rectangle> rockets;
private long lastDropTime = 0;

private float tap_X = 0;
private float tap_Y = 0;

public GameRenderer() {
    cam = new OrthographicCamera();
    cam.setToOrtho(false, 408, 272);
    batch = new SpriteBatch();

    rockets = new Array<Rectangle>();
    spawnRocket();
}

public void render(){
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    cam.update();

    batch.setProjectionMatrix(cam.combined);

    batch.begin();
    for(Rectangle rocket : rockets) {
        batch.draw(AssetLoader.rocket ,rocket.x, rocket.y,
                rocket.width, rocket.height);
    }
    batch.end();

    if (Gdx.input.justTouched()){
        tap_X = (int) (Gdx.input.getX() * HeightScale);
        tap_Y = (int) ((Gdx.graphics.getHeight()-Gdx.input.getY()) * WidthScale);

        Gdx.app.log("MyTag", String.valueOf(tap_X));
        Gdx.app.log("MyTag", String.valueOf(tap_Y));
        Gdx.app.log("MyTag", String.valueOf(HeightScale));
        Gdx.app.log("MyTag", String.valueOf(WidthScale));
    }

    Iterator<Rectangle> iter = rockets.iterator();
    while(iter.hasNext()) {
        Rectangle rocket = iter.next();
        rocket.y -= 70 * Gdx.graphics.getDeltaTime();
        if(rocket.y + 32 < 0) iter.remove();

        if (rocket.x < tap_X && tap_X < rocket.x + rocket.width) {
            Gdx.app.log("MyTag", "getRekt");
            if (tap_Y > rocket.y && tap_Y < rocket.y + rocket.height) {
                Gdx.app.log("MyTag", "poo");
                iter.remove();
            }
        }
    }

    if(TimeUtils.nanoTime() - lastDropTime > 1000000000) spawnRocket();
}

private void spawnRocket() {
    Gdx.app.log("MyTag", "Rocket Spawned");
    Rectangle rocket = new Rectangle();
    rocket.x = MathUtils.random(0 , 272 - 16);
    rocket.y = 408 + rocket.height;
    rocket.height = 32;
    rocket.width = 16;
    rockets.add(rocket);
    lastDropTime = TimeUtils.nanoTime();
}

1 个答案:

答案 0 :(得分:1)

为了使用不同的屏幕尺寸,你必须处理视口,有entirely section in libGDX wiki显示如何使用它。一个快速的例子可能如下(但是,我建议你自己阅读wiki并尝试其他视口):

// Declare a viewport object
private Viewport v;

public GameRenderer() {
    // initialize after your camera initialization
    v = new FitViewport(408, 272, cam); // <- I have use a FitViewport 
                                        //    but you can use others
    // I guess you could remove the setToOrtho() but not too sure...
    // ... rest of code
}

您必须妥善处置和更新此对象 *

你应该考虑的一点是,当你使用像Gdx.input.getX()这样的东西时,你得到一个屏幕当前位置,你必须将这个坐标系转换成你的世界坐标系。无论你在哪里使用类似的东西,你都必须做到以下几点:

Vector3 v = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
v = cam.unproject(v);
// now v has your coordinate world system and you can properly make use of it
// you can do now something like:
tap_x = (int) v.x;
tap_y = (int) v.y;

希望你觉得这很有用。