从一个点(LibGdx)旋转Orthographic相机

时间:2016-08-05 17:45:12

标签: java android libgdx

我正在研究wiki libgdx的正交相机示例:

https://github.com/libgdx/libgdx/wiki/Orthographic-camera#description

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.MathUtils;

public class OrthographicCameraExample implements ApplicationListener {

static final int WORLD_WIDTH = 100;
static final int WORLD_HEIGHT = 100;

private OrthographicCamera cam;
private SpriteBatch batch;

private Sprite mapSprite;
private float rotationSpeed;

@Override
public void create() {
    rotationSpeed = 0.5f;

    mapSprite = new Sprite(new Texture(Gdx.files.internal("sc_map.png")));
    mapSprite.setPosition(0, 0);
    mapSprite.setSize(WORLD_WIDTH, WORLD_HEIGHT);

    float w = Gdx.graphics.getWidth();
    float h = Gdx.graphics.getHeight();

    // Constructs a new OrthographicCamera, using the given viewport width and height
    // Height is multiplied by aspect ratio.
    cam = new OrthographicCamera(30, 30 * (h / w));

    cam.position.set(cam.viewportWidth / 2f, cam.viewportHeight / 2f, 0);
    cam.update();

    batch = new SpriteBatch();
}

@Override
public void render() {
    handleInput();
    cam.update();
    batch.setProjectionMatrix(cam.combined);

    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    batch.begin();
    mapSprite.draw(batch);
    batch.end();
}

private void handleInput() {
    if (Gdx.input.isKeyPressed(Input.Keys.A)) {
        cam.zoom += 0.02;
    }
    if (Gdx.input.isKeyPressed(Input.Keys.Q)) {
        cam.zoom -= 0.02;
    }
    if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
        cam.translate(-3, 0, 0);
    }
    if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
        cam.translate(3, 0, 0);
    }
    if (Gdx.input.isKeyPressed(Input.Keys.DOWN)) {
        cam.translate(0, -3, 0);
    }
    if (Gdx.input.isKeyPressed(Input.Keys.UP)) {
        cam.translate(0, 3, 0);
    }
    if (Gdx.input.isKeyPressed(Input.Keys.W)) {
        cam.rotate(-rotationSpeed, 0, 0, 1);
    }
    if (Gdx.input.isKeyPressed(Input.Keys.E)) {
        cam.rotate(rotationSpeed, 0, 0, 1);
    }

    cam.zoom = MathUtils.clamp(cam.zoom, 0.1f, 100/cam.viewportWidth);

    float effectiveViewportWidth = cam.viewportWidth * cam.zoom;
    float effectiveViewportHeight = cam.viewportHeight * cam.zoom;

    cam.position.x = MathUtils.clamp(cam.position.x, effectiveViewportWidth / 2f, 100 - effectiveViewportWidth / 2f);
    cam.position.y = MathUtils.clamp(cam.position.y, effectiveViewportHeight / 2f, 100 - effectiveViewportHeight / 2f);
}

@Override
public void resize(int width, int height) {
    cam.viewportWidth = 30f;
    cam.viewportHeight = 30f * height/width;
    cam.update();
}

@Override
public void resume() {
}

@Override
public void dispose() {
    mapSprite.getTexture().dispose();
    batch.dispose();
}

@Override
public void pause() {
}

public static void main(String[] args) {
    new LwjglApplication(new OrthographicCameraExample());
}
}

在相机旋转时,地图会随着相机在屏幕中点旋转。我想从某一点旋转相机。例如,点0,0。我试图使用方法rotateAround(Vector3 point,Vector3轴,float angle),我没有得到预期的结果。

cam.rotateAround(new Vector3(0,0,0), new Vector3(0,0,1), 1);

我知道可以将相机移动到0.0点然后旋转。但那不是我想要的。

在我正在做的游戏中,玩家位于中间屏幕底部的固定位置,我将屏幕转过来,但没有将玩家带到屏幕中间然后旋转。 / p>

我很感激帮助。

1 个答案:

答案 0 :(得分:2)

你需要在cam.rotateround后更新相机:) 这个对我有用。

 cam.rotateAround(new Vector3(270, 0, 0), new Vector3(0, 0, 1), 0.1f);      
 cam.update();

以下是我测试的屏幕截图。你可以看到屏幕围绕红点旋转。 (透视相机也在其自身坐标的同一点旋转。)

我还建议您使用

cam.setToOrtho(false, cam.viewportWidth, cam.viewportHeight);

而不是

cam.position.set(cam.viewportWidth / 2f, cam.viewportHeight / 2f, 0);

Arcon