13
2018
03

Libgdx之正交相机 OrthographicCamera

描述

正交相机的操作非常简单,就像我们在现实世界中操作相机一样,文章中主要介绍:


相机的移动和旋转

相机的放到和缩小

改变相机的视窗大小

在窗口(widow)坐标系和(世界)坐标系之间切换点的位置


使用正交相机可以在不必去操作矩阵的情况下非常方便的来移动游戏世界,所以的投影矩阵和视图矩阵都在后台实现。 

下面的代码示例展示了怎样通过照相机在改变游戏世界


一、初始化方法及常见API

camera = new OrthographicCamera();
camera.setToOrtho(false, 480, 800);
camera.position.set(240, 400, 0);
camera.update();

二、应用举例

package com.camera.main;

import com.badlogic.gdx.ApplicationListener;
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.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.MathUtils;

public class MyGame implements ApplicationListener {
	private SpriteBatch batch;
	private Texture tex;
	private OrthographicCamera camera;
	private float rotationSpeed = 0.5f;

	@Override
	public void create() {
		batch = new SpriteBatch();
		tex = new Texture(Gdx.files.internal("worldMap.png"));
		tex.setFilter(TextureFilter.Linear, TextureFilter.Linear);
		camera = new OrthographicCamera();
		camera.setToOrtho(false, 480, 800);
		camera.position.set(240, 400, 0);
		camera.update();
	}

	@Override
	public void resize(int width, int height) {

	}

	@Override
	public void render() {
		Gdx.gl20.glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
		Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT);
		handleInput();
		camera.update();
		batch.setProjectionMatrix(camera.combined);
		batch.begin();
		batch.draw(tex, 0, 0, 6348, 4868);
		batch.end();
	}
	
	private void handleInput() {
		if (Gdx.input.isKeyPressed(Input.Keys.A)) {  
            camera.zoom += 0.02;  
        }  
        if (Gdx.input.isKeyPressed(Input.Keys.Q)) {  
        		camera.zoom -= 0.02;  
        }  
        if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) {  
        		camera.translate(-3, 0, 0);  
        }  
        if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {  
        		camera.translate(3, 0, 0);  
        }  
        if (Gdx.input.isKeyPressed(Input.Keys.DOWN)) {  
        		camera.translate(0, -3, 0);
        }  
        if (Gdx.input.isKeyPressed(Input.Keys.UP)) {  
        		camera.translate(0, 3, 0);  
        }  
        if (Gdx.input.isKeyPressed(Input.Keys.W)) {  
        		camera.rotate(-rotationSpeed, 0, 0, 1);  
        }  
        if (Gdx.input.isKeyPressed(Input.Keys.E)){
        		camera.rotate(rotationSpeed, 0, 0, 1);
        }
        camera.zoom = MathUtils.clamp(camera.zoom, 0.5f, Math.min(tex.getWidth(), tex.getHeight())/camera.viewportHeight);
        float effectiveViewportWidth = camera.viewportWidth * camera.zoom;
        float effectiveViewportHeight = camera.viewportHeight * camera.zoom;
        camera.position.x = MathUtils.clamp(camera.position.x, effectiveViewportWidth/2f, tex.getWidth()-effectiveViewportWidth/2f);
        camera.position.y = MathUtils.clamp(camera.position.y, effectiveViewportHeight/2f, tex.getHeight()-effectiveViewportHeight/2f);
	}
	
	@Override
	public void pause() {

	}

	@Override
	public void resume() {

	}

	@Override
	public void dispose() {
		if (tex != null) {
			tex.dispose();
		}
		if (batch != null) {
			batch.dispose();
		}
	}

}

、启动类

package com.camera.main;

import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;

public class DesktopLanucher {
	public static void main(String[] args) {
		LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
		config.width = 480;
		config.height = 800;
		config.resizable = false; 
		new LwjglApplication(new MyGame(), config);
	}
}


按下Q放大,A缩小,W,E旋转,方向键移动地图。

屏幕快照 2018-03-13 11.13.29.jpg

上一篇:Android 禁止屏幕旋转 screenorientation 下一篇:Libgdx Polygon多边形碰撞检测