【问题标题】:LibGDX multiple camerasLibGDX 多台摄像机
【发布时间】:2015-05-27 09:39:12
【问题描述】:

所以,我正在使用 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();

    }
}

【问题讨论】:

标签: java fonts camera libgdx bitmap-fonts


【解决方案1】:

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

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

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

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

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

另外,你需要在设置好它的位置后调用 fontcam.update()。

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

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

【讨论】:

  • 是的,宽度和高度以像素为单位。我应用了更改,但字体仍未绘制。
  • 你也应该在设置它的位置后更新 fontcam。这有帮助吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多