LibGDX 多个摄像头

LibGDX multiple cameras

所以,我正在使用 LiBGDX 和 Box2D,使用 PixelsPerMeter 转换,我有一个跟随玩家的相机,但是当我打开调试模式时,我需要能够绘制一个屏幕上带有 fps 的字体,所以我总能在角落看到它。问题是,当我尝试缩小 BitmapFont 时,它看起来很正常,因为 Box2D 导致其他所有内容都缩小了。我发现了一些关于使用多个摄像头的内容,所以我不必缩小字体,但它没有任何文档。我试图弄清楚该怎么做,但没有运气。我应该如何将字体绘制到屏幕上?

这是我尝试多摄像头的游戏状态:

package com.platformer.gamestates;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.PerspectiveCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
import com.badlogic.gdx.physics.box2d.World;
import com.platformer.Entities.Player;
import com.platformer.game.Game;
import com.platformer.generation.MenuTiles;
import com.platformer.generation.TiledMaps;
import com.platformer.managers.GameContacts;
import com.platformer.managers.GameStateManager;

import static com.platformer.managers.B2DVars.PPM;

public class MenuState extends GameState {

    World world;
    Box2DDebugRenderer debugRenderer;

    Texture close, far, house;

    BitmapFont font;
    OrthographicCamera fontCam;

    public static Player player;
    MenuTiles menuTiles;

    SpriteBatch batch;

    float camx,camy;

    float lerp=0.1f,lerpy=0.2f;

    GameContacts gameContacts;


    public MenuState(GameStateManager gsm){
        super(gsm);
    }


    public  void init(){

        close=new Texture(Gdx.files.internal("menu_backgrounds/background_close.png"));
        far=new Texture(Gdx.files.internal("menu_backgrounds/background_far.png"));
        house=new Texture(Gdx.files.internal("menu_backgrounds/house_selected.png"));

        batch=new SpriteBatch();

        font=new BitmapFont();

        fontCam=new OrthographicCamera();
        fontCam.setToOrtho(false,Game.WIDTH,Game.HEIGHT);
        fontCam.position.set(Game.WIDTH/2/PPM,Game.HEIGHT/2/PPM,0);

        world=new World(new Vector2(0,-9.81f),true);
        world.setVelocityThreshold(0);
        gameContacts=new GameContacts();
        world.setContactListener(gameContacts);
        debugRenderer=new Box2DDebugRenderer();

        player=new Player(world,960/2,49+20);
        menuTiles=new MenuTiles(world);

    }
    public  void update(float dt){

        world.step(1/60f,6,2);

        player.update(dt,gameContacts);

        if(player.shouldPlay){gsm.setState(GameStateManager.PLAY);dispose();}

        camx+=(player.x-camx)*lerp;
        camy+=(player.y-camy)*lerpy;

    }

    public  void draw(OrthographicCamera camera){

        camera.position.x=Math.min(Math.max(camx,Game.WIDTH/2/PPM),(MenuTiles.levelWidth/PPM)-(Game.WIDTH/2/PPM));
        camera.position.y=Math.min(Math.max(camy,Game.HEIGHT/2/PPM),(MenuTiles.levelHeight/PPM)-(Game.HEIGHT/2/PPM));

        batch.begin();
        batch.draw(far, 0, 0, 960 / PPM, 720 / PPM);
        batch.draw(close,0,0,960/PPM,720/PPM);
        batch.end();

        //draw map
        menuTiles.draw(camera);

        batch.begin();
        if(gameContacts.isOnHouse())batch.draw(house,192/PPM,48/PPM,2*MenuTiles.tileSize/PPM,2*MenuTiles.tileSize/PPM);
        batch.end();

        //draw player
        player.draw(batch);

        if(player.DebugOn){
            debugRenderer.render(world, camera.combined);
            batch.setProjectionMatrix(fontCam.combined);
            batch.begin();
            font.draw(batch,"fps",0,0);
            batch.end();
            System.out.println(Gdx.graphics.getFramesPerSecond()+" fps");
        }

        camera.update();
        batch.setProjectionMatrix(camera.combined);

    }

    public  void handleInput(){}

    public  void dispose(){

        menuTiles.dispose();
        close.dispose();
        far.dispose();
        house.dispose();

    }
}

我认为你不想在这一行除以 PPM...

fontCam.position.set(Game.WIDTH/2/PPM,Game.HEIGHT/2/PPM,0);

我假设 Game.WIDHT 和 HEIGHT 以像素为单位(这在您的代码片段中并不清楚)。如果不是这种情况,那么您应该将 PPM 的除法包括到这一行中...

fontCam.setToOrtho(false,Game.WIDTH,Game.HEIGHT);

目前并没有得到一致应用。

另外,设置位置后需要调用fontcam.update()

另外: 这与你的问题本身无关,但我不确定为什么你在渲染的末尾有以下几行。我会在更改标准相机的位置后立即放置它们。

camera.update();
batch.setProjectionMatrix(camera.combined);