gpt4 book ai didi

java - 从一点旋转正交相机 (LibGdx)

转载 作者:行者123 更新时间:2023-12-02 03:26:36 24 4
gpt4 key购买 nike

我正在研究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());
}
}

在相机旋转时, map 会随着相机位于屏幕中点而旋转。我想从某个点旋转相机。例如,点 0,0 。我尝试使用rotateAround方法(Vector3点,Vector3轴, float 角度),但没有得到预期的结果。

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

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

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

非常感谢您的帮助。

最佳答案

你只需要在 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

关于java - 从一点旋转正交相机 (LibGdx),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38795083/

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