【问题标题】:Rotate OrthographicCamera from a point (LibGdx)从一个点旋转 OrthographicCamera (LibGdx)
【发布时间】:2016-12-12 05:08:51
【问题描述】:

我正在研究wiki libgdx的Orthographic Camera的例子:

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 axis , float angle) 方法,没有得到预期的结果。

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

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

在我正在做的游戏中,玩家处于屏幕底部中间的固定位置,我围绕它转动屏幕,但没有将玩家带到屏幕中间然后旋转。

感谢您的帮助。

【问题讨论】:

    标签: java android libgdx


    【解决方案1】:

    您只需要在 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);
    

    【讨论】:

    • 丹尼兹感谢您的帮助。我设法旋转了相机。相机旋转后,坐标让我感到困惑。我有一台相机,一个角色和一张地图。该玩家仅在以下方向行走:北(90°),南(270°),东(0°),西(180°)。从玩家的位置旋转相机后,他开始向新的方向移动,作为替换的角度。有没有办法在不将地图移动到原始位置的情况下将原始位置重新定位回坐标?我将尝试再提出一个关于图像堆栈溢出的问题。感谢您的帮助。
    • 我在等你的问题。如果你提供一些关于玩家动作的代码会有所帮助。
    • 我把图片放在了有问题的地方。 stackoverflow.com/questions/38975166/…再次感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-26
    • 2014-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-02
    相关资源
    最近更新 更多