libgdx摄像头的移动
生活随笔
收集整理的這篇文章主要介紹了
libgdx摄像头的移动
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
要知道,做一個游戲,攝像頭是必不可少的。接下來,我將講解libgdx里面攝像頭的移動
2d攝像頭OrthographicCamera也叫做正交相機
結果展示:
按上下左右是可以移動的
OrthographicCamera camera的使用:
完整代碼:
package com.brentaureli.mariobros.cam;
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.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.MathUtils;
import static com.badlogic.gdx.Gdx.gl;
public class T1 implements ApplicationListener {
private OrthographicCamera camera;
private float VIEWPORT_WIDTH=100f;
private float VIEWPORT_HEIGHT=100f;
private SpriteBatch batch;
private Sprite sprite;
@Override
public void create() {
sprite=new Sprite(new Texture(Gdx.files.internal("block.png")));
sprite.setSize(VIEWPORT_WIDTH,VIEWPORT_HEIGHT);
sprite.setPosition(0, 0);
float w = Gdx.graphics.getWidth();
float h = Gdx.graphics.getHeight();
camera=new OrthographicCamera(100,100*h/w);
camera.position.set(15f,11.25f,0);
camera.update();
batch=new SpriteBatch();
}
@Override
public void resize(int width, int height) {
//調(diào)整窗口,相機位置不變
camera.viewportWidth = 30f;
camera.viewportHeight = 30f * height / width;
camera.update();
}
@Override
public void render() {
handleInput();
resetCamera();
camera.update();
batch.setProjectionMatrix(camera.combined);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
sprite.draw(batch);
batch.end();
}
private void resetCamera() {
if (Gdx.input.isKeyPressed(Input.Keys.R)) {
camera.position.set(camera.viewportWidth / 2f, camera.viewportHeight / 2f, 0);
camera.zoom = 1;
camera.rotate(0, 0, 0, 1);
}
}
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);
}
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void dispose() {
sprite.getTexture().dispose();
batch.dispose();
}
}
主類完整代碼:
package com.brentaureli.mariobros.desktop;
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application;
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3ApplicationConfiguration;
import com.brentaureli.mariobros.cam.T1;
public class DesktopLauncher {
public static void main (String[] arg) {
Lwjgl3ApplicationConfiguration config = new Lwjgl3ApplicationConfiguration();
config.setForegroundFPS(60);
config.setTitle("camera-example");
new Lwjgl3Application(new T1(), config);
}
}
先設置了一個背景
private Sprite sprite;
sprite=new Sprite(new Texture(Gdx.files.internal("block.png")));
sprite.setSize(VIEWPORT_WIDTH,VIEWPORT_HEIGHT);
sprite.setPosition(0, 0);
設置了背景的圖片,背景的大小,背景位置。
接下來的代碼:
float w = Gdx.graphics.getWidth();
float h = Gdx.graphics.getHeight();
camera=new OrthographicCamera(100,100*h/w);
camera.position.set(15f,11.25f,0);
camera.update();
batch=new SpriteBatch();
設置了camera的范圍,camera的位置 camera.update();這個是每次更新camera的位置呀什么的都要進行一次更新。然后就是new了一個緩沖。
接下來的代碼:
//調(diào)整窗口,相機位置不變
camera.viewportWidth = 30f;
camera.viewportHeight = 30f * height / width;
camera.update();
就是單純的再調(diào)試了一下窗口。
接下來的代碼:
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);
}
}
添加了一下鍵盤輸入的按鍵,camera.zoom += 0.02;就是將攝像的范圍擴大同理camera.zoom -= 0.02;就是減少,而 camera.translate就是移動位置。
接下來的代碼:
private void resetCamera() {
if (Gdx.input.isKeyPressed(Input.Keys.R)) {
camera.position.set(camera.viewportWidth / 2f, camera.viewportHeight / 2f, 0);
camera.zoom = 1;
camera.rotate(0, 0, 0, 1);
}
}
就是單純的按r重置攝像頭
然后就是:
camera.update();
batch.setProjectionMatrix(camera.combined);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
sprite.draw(batch);
batch.end();
將攝像頭進行更新,然后 batch.setProjectionMatrix(camera.combined);這個,這個可以說是固定搭配,就是配置一下攝像頭。
batch.begin();
sprite.draw(batch);
batch.end();
緩存區(qū)打開,然后畫圖,end方法結尾。這算是固定搭配。
3D攝像頭PerspectiveCamera也叫做透視相機
完整代碼:
package com.brentaureli.mariobros.cam;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.PerspectiveCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.VertexAttributes;
import com.badlogic.gdx.graphics.g3d.Material;
import com.badlogic.gdx.graphics.g3d.Model;
import com.badlogic.gdx.graphics.g3d.ModelBatch;
import com.badlogic.gdx.graphics.g3d.ModelInstance;
import com.badlogic.gdx.graphics.g3d.attributes.TextureAttribute;
import com.badlogic.gdx.graphics.g3d.utils.FirstPersonCameraController;
import com.badlogic.gdx.graphics.g3d.utils.ModelBuilder;
import static com.badlogic.gdx.Gdx.gl;
public class T2 implements ApplicationListener {
private PerspectiveCamera camera;
private FirstPersonCameraController cameraController;
private ModelBatch batch;
private ModelBuilder builder;
private Texture texture;
private Model model;
private ModelInstance instance;
@Override
public void create() {
camera=new PerspectiveCamera(70, Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
camera.position.set(0,0,0);
camera.near=0.1f;
camera.far=1000f;
camera.update();
cameraController=new FirstPersonCameraController(camera);
Gdx.input.setInputProcessor(cameraController);
cameraController.update();
batch=new ModelBatch();
builder=new ModelBuilder();
texture=new Texture(Gdx.files.internal("block.png"));
Material material=new Material(new TextureAttribute(TextureAttribute.Diffuse,texture));
int attributes= VertexAttributes.Usage.Position|VertexAttributes.Usage.TextureCoordinates;
model= builder.createBox(3, 3, 3, material, attributes);
instance= new ModelInstance(model);
instance.transform.setToTranslation(0,0,-5);
}
@Override
public void resize(int width, int height) {
camera.viewportWidth=width;
camera.viewportHeight=height;
camera.update();
cameraController.update();
}
@Override
public void render() {
gl.glClear(GL20.GL_COLOR_BUFFER_BIT|GL20.GL_DEPTH_BUFFER_BIT);
camera.update();
cameraController.update();
batch.begin(camera);
batch.render(instance);
batch.end();
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void dispose() {
}
}
主類就換個類名就行了,接下來是結果展示圖:
按wsad可以上下左右移動。
由于篇幅可能過長,下一章也許會講。
總結
以上是生活随笔為你收集整理的libgdx摄像头的移动的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 混合专家模型 (MoE) 详解
- 下一篇: 兵线的移动速度