libGDX学习之路03:学习使用照相机——camera

简介

camera其实是在3D世界中定义的,每当我们观察一个物体的时候,都有一台照相机以不同的角度去“看到”这个物体,这就是照相机。

在libGDX中camera分为两大类,分别是PerspectiveCamera(远景照相机)和OrthographicCamera(正交照相机)。

PerspectiveCamera是正常的照相机,当距离物体越远,则物体越小,一般在3D空间中使用,本文暂不探讨。

OrthographicCamera是忽略了 Z 轴,不管物体距离照相机的远近,大小不变,一般在2D空间中使用。

OrthographicCamera继承了Camera。

用法

属性

public float zoom

该属性用来控制照相机对物体的距离(也就是放大和缩小)

常用方法

public void setToOrtho(boolean yDown,float viewportWidth,float viewportHeight)

功能:将该相机设置为正交投影,一般以(viewportWidth/2,viewportHeight/2)为中心,y轴指向上或下。

参数:
yDown - y轴是否朝下.

position.set(float x,float y,float z)

继承自Camera的方法

功能:指定相机的位置,正交相机中一般z轴为0

public void update()

也继承自Camera类

功能:重新计算此相机和平截头体平面的投影和视图矩阵。在改变相机的任何属性之后,都必须调用该方法才能成功更新照相机的参数
public void translate(float x,
                      float y)

给定x和y轴上的具体参数,移动镜头。

参数:

x - 在x轴上移动多少

y - 在y轴上移动多少

public void rotate(float angle, float axisX, float axisY, float axisZ)

围绕给定轴,按给定角度旋转此摄像机的方向和上方向向量

参数:

angle - 旋转角度(旋转多少度)

axisX - x轴的分量

axisY - y轴的分量

axisZ - z轴的分量

演示Demo

Orthographic camera - libGDXThis page presents the OrthographicCamera class and usage. The orthographic camera is to be used in 2D environments only as it implements a parallel (orthographic) projection and there will be...https://libgdx.com/wiki/graphics/2d/orthographic-camera

参照官方文档中的代码,运行后用户可以用小键盘上的↑↓←→来进行移动镜头,A键缩小地图,Q键可以放大地图,W键逆时针旋转地图,E键顺时针旋转地图。除此之外我添加了一个R键重置镜头的方法,但不能重置旋转

package com.tdsss.game;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
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 CameraDemo extends ApplicationAdapter {
    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("map/mapdemo.png")));
        mapSprite.setPosition(0, 0);
        mapSprite.setSize(WORLD_WIDTH, WORLD_HEIGHT);
        float w = Gdx.graphics.getWidth();
        float h = Gdx.graphics.getHeight();
        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();
        resetCamera();
        cam.update();
        batch.setProjectionMatrix(cam.combined);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        batch.begin();
        mapSprite.draw(batch);
        batch.end();
    }

    private void resetCamera() {
		//重置相机位置
        if (Gdx.input.isKeyPressed(Input.Keys.R)) {
            cam.position.set(cam.viewportWidth / 2f, cam.viewportHeight / 2f, 0);
            cam.zoom = 1;
            cam.rotate(0, 0, 0, 1);
        }
    }

    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 dispose() {
		//释放资源
        mapSprite.getTexture().dispose();
        batch.dispose();
    }
}

运行后如下图所示:

 按住A可以缩小地图,看到地图全貌

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TDSSS

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值