美文网首页LibGDX
LibGDX图形模块之正交相机

LibGDX图形模块之正交相机

作者: 天神Deity | 来源:发表于2017-09-18 00:00 被阅读38次

    本页介绍了OrthographicCamera类和用法。 正交相机仅在2D环境中使用,因为它实现了平行(正投影)投影,不管对象放置在哪里,对于最终图像将不存在缩放因子。

    在这里,LibGDX.info提供了一个简单的相机缩放和移动示例代码:

    描述

    相机类具有以下功能:

    1. 移动并旋转相机
    2. 放大和缩小
    3. 改变视口
    4. 将窗口坐标投射到游戏世界坐标上

    使用相机是在游戏世界中移动的简单方式,而无需手动操作矩阵。 所有的投影和视图矩阵运算都隐藏在相机实现中。

    以下小应用程序演示了使用简单的OrthographicCamera来移动平坦的世界。

    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());
        }
    }
    

    上述课程是利用正射摄影机在全球各地移动的LibGDX应用程序。 我们的游戏世界大小是凭我们喜好定义的。 上述例子中,我们定义的大小是100×100单位。

        static final int WORLD_WIDTH = 100;
        static final int WORLD_HEIGHT = 100;
    
        private OrthographicCamera cam;  #1
        private SpriteBatch batch;       #2
     
        private Sprite mapSprite;        #3
        private float rotationSpeed;     #4
    

    代码详解:
    #1 - 我们将控制的OrthographicCamera实例来观察世界。
    #2 - 我们将用于渲染我们的世界的SpriteBatch实例
    #3 - 我们将用来绘制我们的世界地图的精灵
    #4 - 旋转相机的旋转速度

    @Override
    public void create() {
        rotationSpeed = 0.5f;                                                    #1
    
        mapSprite = new Sprite(new Texture(Gdx.files.internal("sc_map.png")));   #2
        mapSprite.setPosition(0, 0);                                             #3
        mapSprite.setSize(WORLD_WIDTH, WORLD_HEIGHT);                            #4
    
        float w = Gdx.graphics.getWidth();                                       #5
        float h = Gdx.graphics.getHeight();                                      #6
        cam = new OrthographicCamera(30, 30 * (h / w));                          #7
        cam.position.set(cam.viewportWidth / 2f, cam.viewportHeight / 2f, 0);    #8
        cam.update();                                                            #9
    
        batch = new SpriteBatch();                                               #10
    }
    

    当我们创建一个我们的ApplicationListener的新实例时,调用create方法,它是我们初始化变量的地方

    #1 - 将当前旋转速度设置为0.5度。
    #2 - 从使用文件的新纹理创建我们的Sprite:sc_map.png在此下载文件并将其放在assets /目录中。
    #3 - 我们将mapSprite的位置设置为0,0。 (这并不是严格要求,因为Sprite默认的x,y都为0。)
    #4 - 我们设置mapSprite的大小,宽度为WORLD_WIDTH,高度为WORLD_HEIGHT。 所以我们的精灵现在的尺寸为100x100,或者我们的世界的大小。
    #5 - 我们创建一个局部变量,其值为应用程序显示的当前宽度。 (以像素为单位)
    #6 - 我们创建一个局部变量,其值为应用程序显示的当前高度。 (以像素为单位)
    #7 - 我们创建正交摄影机。 2个参数指定将要创建的视口的宽度和高度。 这些值决定了我们在每个轴上可以看到的世界。

    在我们的例子中,我们的视口宽度为30,视口高度为30 *(h / w)。 宽度是微不足道的,我们可以在X轴上看到30个单位。 对于视口高度,我们使用30乘以显示器的宽高比。 这是我们看到的对象,我们绘制正确的比例。 想像一下,如果忽略宽高比,只要视口宽度和高度为30,除非我们有一个方形显示器,但很可能我们的显示器并不是方形的,例如当我们渲染一个尺寸为30x30的对象时,它会显示与我们的显示器相同形状的挤压矩形。 30x30的对象怎么可能不是正方形? 这是因为我们假定了30个视口宽度和30个视口高度,这与我们的设备的宽高比不符。

    #8 - 将相机的初始位置设置到地图左下角。 但是...相机的位置在相机的中心。因此,我们需要将相机的位置偏移+半视口宽度和+半视口高度,以便我们的相机的左下角实际上为0,0。

    #9 - 更新我们的相机! 当我们操纵我们的相机,因为它更新了引擎盖下的所有矩阵,这一步是至关重要的。

    #10 - 创建我们的SpriteBatch实例。

    我们设置好了 所以让我们来渲染和操纵相机。

    @Override
    public void render() {
        handleInput();                             #1
        cam.update();                              #2                             
        batch.setProjectionMatrix(cam.combined);   #3
    
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);  #4
    
        batch.begin();                             #5
        mapSprite.draw(batch);                     #6
        batch.end();                               #7
    }
    

    #1 - 通过按照不同的按键更新其位置,缩放,旋转来控制相机。

    #2 - 更新我们的OrthographicCamera,我们只是使用handleInput()方法来操作它,所以我们必须记住调用update()方法。

    #3 - 使用我们的相机的视图和投影矩阵更新我们的SpriteBatch实例。

    #4 - 清除屏幕(实际上是彩色缓冲区)。

    #5 - 开始我们的SpriteBatch

    #6 - 绘制我们的mapSprite!

    #7 - 结束我们的SpriteBatch

    我们来深入了解控制我们的相机,这些都是在我们的handleInput()方法中处理的。

    private void handleInput() {
            if (Gdx.input.isKeyPressed(Input.Keys.A)) {
                cam.zoom += 0.02;
                //If the A Key is pressed, add 0.02 to the Camera's Zoom
            }
            if (Gdx.input.isKeyPressed(Input.Keys.Q)) {
                cam.zoom -= 0.02;
                //If the Q Key is pressed, subtract 0.02 from the Camera's Zoom
            }
            if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
                cam.translate(-3, 0, 0);
                //If the LEFT Key is pressed, translate the camera -3 units in the X-Axis
            }
            if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
                cam.translate(3, 0, 0);
                //If the RIGHT Key is pressed, translate the camera 3 units in the X-Axis
            }
            if (Gdx.input.isKeyPressed(Input.Keys.DOWN)) {
                cam.translate(0, -3, 0);
                //If the DOWN Key is pressed, translate the camera -3 units in the Y-Axis
            }
            if (Gdx.input.isKeyPressed(Input.Keys.UP)) {
                cam.translate(0, 3, 0);
                //If the UP Key is pressed, translate the camera 3 units in the Y-Axis
            }
            if (Gdx.input.isKeyPressed(Input.Keys.W)) {
                cam.rotate(-rotationSpeed, 0, 0, 1);
                //If the W Key is pressed, rotate the camera by -rotationSpeed around the Z-Axis
            }
            if (Gdx.input.isKeyPressed(Input.Keys.E)) {
                cam.rotate(rotationSpeed, 0, 0, 1);
                //If the E Key is pressed, rotate the camera by rotationSpeed around the Z-Axis
            }
    
            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);
        }
    

    所以我们可以看到,这个方法轮询键,如果某个键被按下,我们对相机做一些操作。

    最后5行负责将相机保持在我们世界的范围内。

    我们需要确保相机的缩放不会增加或缩小到会反转我们的世界或显示我们的世界太多的信息。 为了做到这一点,我们可以计算effectiveViewportWidth和effectiveViewportHeight,它们只是viewportWidth / height * zoom(这给我们提供了当前缩放在世界上可以看到的)。 然后,我们可以将相机的缩放值限制到我们需要的值。 0.1f以防止过度放大 100 / cam.viewportWidth,以防止我们看到超过世界的整个宽度。

    最后两行负责确保我们无法在世界范围之外进行移动,在任一轴中应大于等于0小于等于100.

    当应用程序更改大小时该怎么办? 当您执行不同的策略来处理具有不同分辨率/宽高比的设备时。 我会列举一些基本的策略,给你提供基本的想法。

    如果你想要一个更高级别的处理方法,你应该使用视口 -> Wiki Article on Viewports

    以下调整大小策略将确保您始终可以在x轴上看到30个单位,无论您的设备具有什么像素宽度。

    @Override
        public void resize(int width, int height) {
            cam.viewportWidth = 30f;                 // Viewport of 30 units!
            cam.viewportHeight = 30f * height/width; // Lets keep things in proportion.
            cam.update();
        }
    

    以下调整大小策略将根据分辨率显示更少/更多的世界

    @Override
        public void resize(int width, int height) {
            cam.viewportWidth = width/32f;  //We will see width/32f units!
            cam.viewportHeight = cam.viewportWidth * height/width;
            cam.update();
        }
    

    引导监听器的主要应用是一个简单的LWJGL应用程序。

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

    以下展示了应用的最终结果:

    Paste_Image.png

    大多数情况下,不需要访问摄像头的内部,因为最常见的用例包括以下方法:

    Method Description
    lookAt(float x, float y, float z) 重新计算相机的方向以查看所有轴上坐标定义的点。 - 2D的z轴被忽略
    translate(float x, float y, float z) 在每个轴上将相机移动给定的量.- 请注意,对于OrthographicCamera,z被忽略
    rotate(float angle, float axisX, float axisY, float axisZ) 沿着给定的轴旋转给定的角度.
    update() 重新计算相机的投影和矩阵

    相关文章

      网友评论

        本文标题:LibGDX图形模块之正交相机

        本文链接:https://www.haomeiwen.com/subject/ylamsxtx.html