libgdx摄像头的移动

简介: 本文讲解了在libgdx游戏框架中如何实现2D和3D摄像头的移动。对于2D,使用OrthographicCamera并通过键盘输入实现摄像头的平移和缩放;对于3D,使用PerspectiveCamera和FirstPersonCameraController实现类似第一人称射击游戏的视角控制,允许玩家使用WSAD键进行移动。文章提供了详细的代码示例和结果展示图。

概述:要知道,做一个游戏,摄像头是必不可少的。接下来,我将讲解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) {

        //调整窗口,相机位置不变
        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了一个缓冲。
接下来的代码:

        //调整窗口,相机位置不变
        camera.viewportWidth = 30f;
        camera.viewportHeight = 30f * height / width;
        camera.update();

就是单纯的再调试了一下窗口。
接下来的代码:

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

缓存区打开,然后画图,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可以上下左右移动。
由于篇幅可能过长,下一章也许会讲。

目录
相关文章
|
23天前
|
弹性计算 人工智能 架构师
阿里云携手Altair共拓云上工业仿真新机遇
2024年9月12日,「2024 Altair 技术大会杭州站」成功召开,阿里云弹性计算产品运营与生态负责人何川,与Altair中国技术总监赵阳在会上联合发布了最新的“云上CAE一体机”。
阿里云携手Altair共拓云上工业仿真新机遇
|
16天前
|
存储 关系型数据库 分布式数据库
GraphRAG:基于PolarDB+通义千问+LangChain的知识图谱+大模型最佳实践
本文介绍了如何使用PolarDB、通义千问和LangChain搭建GraphRAG系统,结合知识图谱和向量检索提升问答质量。通过实例展示了单独使用向量检索和图检索的局限性,并通过图+向量联合搜索增强了问答准确性。PolarDB支持AGE图引擎和pgvector插件,实现图数据和向量数据的统一存储与检索,提升了RAG系统的性能和效果。
|
20天前
|
机器学习/深度学习 算法 大数据
【BetterBench博士】2024 “华为杯”第二十一届中国研究生数学建模竞赛 选题分析
2024“华为杯”数学建模竞赛,对ABCDEF每个题进行详细的分析,涵盖风电场功率优化、WLAN网络吞吐量、磁性元件损耗建模、地理环境问题、高速公路应急车道启用和X射线脉冲星建模等多领域问题,解析了问题类型、专业和技能的需要。
2576 22
【BetterBench博士】2024 “华为杯”第二十一届中国研究生数学建模竞赛 选题分析
|
18天前
|
人工智能 IDE 程序员
期盼已久!通义灵码 AI 程序员开启邀测,全流程开发仅用几分钟
在云栖大会上,阿里云云原生应用平台负责人丁宇宣布,「通义灵码」完成全面升级,并正式发布 AI 程序员。
|
2天前
|
存储 人工智能 搜索推荐
数据治理,是时候打破刻板印象了
瓴羊智能数据建设与治理产品Datapin全面升级,可演进扩展的数据架构体系为企业数据治理预留发展空间,推出敏捷版用以解决企业数据量不大但需构建数据的场景问题,基于大模型打造的DataAgent更是为企业用好数据资产提供了便利。
162 2
|
20天前
|
机器学习/深度学习 算法 数据可视化
【BetterBench博士】2024年中国研究生数学建模竞赛 C题:数据驱动下磁性元件的磁芯损耗建模 问题分析、数学模型、python 代码
2024年中国研究生数学建模竞赛C题聚焦磁性元件磁芯损耗建模。题目背景介绍了电能变换技术的发展与应用,强调磁性元件在功率变换器中的重要性。磁芯损耗受多种因素影响,现有模型难以精确预测。题目要求通过数据分析建立高精度磁芯损耗模型。具体任务包括励磁波形分类、修正斯坦麦茨方程、分析影响因素、构建预测模型及优化设计条件。涉及数据预处理、特征提取、机器学习及优化算法等技术。适合电气、材料、计算机等多个专业学生参与。
1576 16
【BetterBench博士】2024年中国研究生数学建模竞赛 C题:数据驱动下磁性元件的磁芯损耗建模 问题分析、数学模型、python 代码
|
22天前
|
编解码 JSON 自然语言处理
通义千问重磅开源Qwen2.5,性能超越Llama
击败Meta,阿里Qwen2.5再登全球开源大模型王座
969 14
|
3天前
|
Linux 虚拟化 开发者
一键将CentOs的yum源更换为国内阿里yum源
一键将CentOs的yum源更换为国内阿里yum源
212 2
|
17天前
|
人工智能 开发框架 Java
重磅发布!AI 驱动的 Java 开发框架:Spring AI Alibaba
随着生成式 AI 的快速发展,基于 AI 开发框架构建 AI 应用的诉求迅速增长,涌现出了包括 LangChain、LlamaIndex 等开发框架,但大部分框架只提供了 Python 语言的实现。但这些开发框架对于国内习惯了 Spring 开发范式的 Java 开发者而言,并非十分友好和丝滑。因此,我们基于 Spring AI 发布并快速演进 Spring AI Alibaba,通过提供一种方便的 API 抽象,帮助 Java 开发者简化 AI 应用的开发。同时,提供了完整的开源配套,包括可观测、网关、消息队列、配置中心等。
732 10