• libgdx学习记录17——照相机Camera


    照相机在libgdx中的地位举足轻重,贯穿于整个游戏开发过程的始终。一般我们都通过Stage封装而间接使用Camera,同时我们也可以单独使用Camera以完成背景的移动、元素的放大、旋转等操作。

    Camera分为PerspectiveCamera(远景照相机)和OrthographicCamera(正交照相机)。

    PerspectiveCamera为正常的照相机,当距离物体越远,则物体越小,一般在3D空间中使用。

    OrthographicCamera忽略了其Z轴,不管距离物体多远,其大小始终不变,一般在2D平面中使用。

    由于我们所涉及的游戏界面都是在2D平面中,因此我们现在只讨论OrthographicCamera。

    OrthographicCamera继承自Camera。

    主要函数:

    SetToOtho(ydown,width,height)指定是否y轴朝下,width和height分别表示照相机的视口宽度和高度。此函数同时还将位置设定在其中心,并执行一次update。

    position.set( x, y )指定照相机的位置,一般设置在视口中心,width/2,height/2。

    update()更新,就是更新其位置、角度和放大倍数等参数。

    translate(x,y,z) 移动

    具体代码:

     1 package com.fxb.newtest;
     2 
     3 import com.badlogic.gdx.ApplicationAdapter;
     4 import com.badlogic.gdx.Gdx;
     5 import com.badlogic.gdx.Input;
     6 import com.badlogic.gdx.InputAdapter;
     7 import com.badlogic.gdx.graphics.GL10;
     8 import com.badlogic.gdx.graphics.OrthographicCamera;
     9 import com.badlogic.gdx.graphics.Texture;
    10 import com.badlogic.gdx.graphics.g2d.SpriteBatch;
    11 
    12 public class Lib017_Camera extends ApplicationAdapter{
    13 
    14     OrthographicCamera camera;
    15     Texture texture;
    16     SpriteBatch batch;
    17     
    18     
    19     @Override
    20     public void create() {
    21         // TODO Auto-generated method stub
    22         super.create();
    23         batch = new SpriteBatch();
    24         texture = new Texture( Gdx.files.internal( "data/pal4_0.jpg" ) );
    25         
    26         camera = new OrthographicCamera();
    27         camera.setToOrtho( false, 480, 320 );
    28 
    29         camera.rotate( 45 );
    30         camera.zoom = 2f;
    31     }
    32 
    33     public void HandleInput(){
    34         if( Gdx.input.isKeyPressed( Input.Keys.LEFT ) ){
    35             camera.position.add( 2f, 0, 0 );
    36         }
    37         else if( Gdx.input.isKeyPressed( Input.Keys.RIGHT )  ){
    38             camera.position.add( -2f, 0, 0 );
    39         }
    40         else if( Gdx.input.isKeyPressed( Input.Keys.UP )  ){
    41             camera.position.add( 0, -2f, 0 );
    42         }
    43         else if( Gdx.input.isKeyPressed( Input.Keys.DOWN )  ){
    44             camera.position.add( 0, 2f, 0 );
    45         }
    46         else if( Gdx.input.isKeyPressed( Input.Keys.D )  ){
    47             camera.zoom -= 0.05f;
    48         }
    49         else if( Gdx.input.isKeyPressed( Input.Keys.F )  ){
    50             camera.zoom += 0.05f;
    51         }
    52         else if( Gdx.input.isKeyPressed( Input.Keys.A )  ){
    53             camera.rotate( 1 );
    54         }
    55         else if( Gdx.input.isKeyPressed( Input.Keys.S )  ){
    56             camera.rotate( -1 );
    57         }        
    58     }
    59     
    60     @Override
    61     public void render() {
    62         // TODO Auto-generated method stub
    63         super.render();
    64         Gdx.gl.glClearColor( 0, 1, 1, 1 );
    65         Gdx.gl.glClear( GL10.GL_COLOR_BUFFER_BIT );
    66         
    67         HandleInput();
    68         //camera.position.add( 0.5f, 0, 0 );
    69         camera.update();
    70         
    71         batch.setProjectionMatrix( camera.combined );
    72         batch.begin();
    73         batch.draw( texture, 0, 0 );
    74         batch.end();
    75         
    76     }
    77 
    78     @Override
    79     public void dispose() {
    80         // TODO Auto-generated method stub
    81         batch.dispose();
    82         texture.dispose();
    83         super.dispose();
    84     }
    85 
    86 }

    运行效果:

    从代码中也可以看出来:

    按左右上下按键分别会使图片左右上下移动,而Camera却是向相反的方向移动。

    ASDF按键分别对应逆时针旋转、顺时针旋转、放大、缩小等操作。

  • 相关阅读:
    无锁队列以及ABA问题
    bigworld源码分析(3)——dbMgr分析
    bigworld源码分析(4)——BaseAppMgr分析
    bigworld源码分析(5)——BaseApp分析
    bigworld源码分析(2)—— loginApp分析
    bigworld源码分析(1)—— 研究bigworld的意义和目标
    C++嵌入Python,以及两者混用
    B-Tree算法分析与实现
    通过sqlserver日志恢复误删除的数据
    win7启动时怎么自动进入桌面
  • 原文地址:https://www.cnblogs.com/MiniHouse/p/3773694.html
Copyright © 2020-2023  润新知