gpt4 book ai didi

java - LibGDX 多相机

转载 作者:行者123 更新时间:2023-11-30 08:10:23 26 4
gpt4 key购买 nike

所以,我正在使用 LiBGDX 和 Box2D,使用 PixelsPerMeter 转换,并且我有一个跟随玩家的相机,但是当我打开 Debug模式时,我需要能够在屏幕上绘制带有 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);

关于java - LibGDX 多相机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30478645/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com