Libgdx,何时使用camera.position.set?
我对两个与viewport和Orthographic相关的示例感到非常困惑。尽管我知道Viewport是我们设置在屏幕上查看的尺寸大小,并且相机投影了它。我正在学习libgdx,无法通过正交摄影机和视口示例完成学习,这让我完全困惑。对于这两个示例,代码运行良好,并且在屏幕上显示正确的结果 下面是一个示例,其中使用camera.position.set来定位相机Libgdx,何时使用camera.position.set?,libgdx,Libgdx,我对两个与viewport和Orthographic相关的示例感到非常困惑。尽管我知道Viewport是我们设置在屏幕上查看的尺寸大小,并且相机投影了它。我正在学习libgdx,无法通过正交摄影机和视口示例完成学习,这让我完全困惑。对于这两个示例,代码运行良好,并且在屏幕上显示正确的结果 下面是一个示例,其中使用camera.position.set来定位相机 public class AnimatedSpriteSample extends GdxSample { private static
public class AnimatedSpriteSample extends GdxSample {
private static final float WORLD_TO_SCREEN = 1.0f / 100.0f;
private static final float SCENE_WIDTH = 12.80f;
private static final float SCENE_HEIGHT = 7.20f;
private static final float FRAME_DURATION = 1.0f / 30.0f;
private OrthographicCamera camera;
private Viewport viewport;
private SpriteBatch batch;
private TextureAtlas cavemanAtlas;
private TextureAtlas dinosaurAtlas;
private Texture background;
private Animation dinosaurWalk;
private Animation cavemanWalk;
private float animationTime;
@Override
public void create() {
camera = new OrthographicCamera();
viewport = new FitViewport(SCENE_WIDTH, SCENE_HEIGHT, camera);
batch = new SpriteBatch();
animationTime = 0.0f;
camera = new OrthographicCamera();
。。。
...
下面是另一个不使用camera.position.set的示例,结果仍然相同
@Override
public void create() {
camera = new OrthographicCamera();
viewport = new FitViewport(SCENE_WIDTH, SCENE_HEIGHT, camera);
batch = new SpriteBatch();
oldColor = new Color();
cavemanTexture = new Texture(Gdx.files.internal("data/caveman.png"));
cavemanTexture.setFilter(TextureFilter.Nearest, TextureFilter.Nearest);
}
@Override
public void dispose() {
batch.dispose();
cavemanTexture.dispose();
}
@Override
public void render() {
Gdx.gl.glClearColor(BACKGROUND_COLOR.r,
BACKGROUND_COLOR.g,
BACKGROUND_COLOR.b,
BACKGROUND_COLOR.a);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(camera.combined);
batch.begin();
int width = cavemanTexture.getWidth();
int height = cavemanTexture.getHeight();
float originX = width * 0.5f;
float originY = height * 0.5f;
// flipX, flipY
// Render caveman centered on the screen
batch.draw(cavemanTexture, // Texture itselft
-originX, -originY, // pass in the world space coordinates where we to draw, Considering the camera is centered at (0,0). by default we need to position
// out cavement at -originX, -originY.
originX, originY, // coordinates in pixels of our texture that we consider to be the origin starting from the bottom-left corner.
// in our case, we want the origin to be the center of the texture. then we pass the dimensions of the texture and the scale
// and the scale along both axes (x and Y).
width, height, // width, height
WORLD_TO_SCREEN, WORLD_TO_SCREEN, // scaleX, scaleY
0.0f, // rotation
0, 0, // srcX, srcY
width, height, // srcWidth, srcHeight
false, false); // flipX, flipY
真正让我困惑的是,为什么在第二个示例中不使用camera.position.set来调整相机的视图,以及为什么在第一个示例中使用它很重要
我真的希望这个问题是合法的和有意义的。我在这里搜索了论坛,没有找到任何线索。希望有人能指引正确的方向
非常感谢 在第一个示例中,已经为相机在x方向和y方向的位置初始化了二维向量。这是针对相机的
public class AnimatedSpriteSample extends GdxSample {
private static final float WORLD_TO_SCREEN = 1.0f / 100.0f;
private static final float SCENE_WIDTH = 12.80f;
private static final float SCENE_HEIGHT = 7.20f;
private static final float FRAME_DURATION = 1.0f / 30.0f;
private OrthographicCamera camera;
private Viewport viewport;
private SpriteBatch batch;
private TextureAtlas cavemanAtlas;
private TextureAtlas dinosaurAtlas;
private Texture background;
private Animation dinosaurWalk;
private Animation cavemanWalk;
private float animationTime;
@Override
public void create() {
camera = new OrthographicCamera();
viewport = new FitViewport(SCENE_WIDTH, SCENE_HEIGHT, camera);
batch = new SpriteBatch();
animationTime = 0.0f;
camera = new OrthographicCamera();
因此,此代码从libgdx创建者创建的正交摄影机类创建摄影机对象。查看该类的文档,在构建该类时,您可以看到该类同时接受视口高度和视口宽度。(在您的示例中,您将其留空,因此这些值暂时为0。)
这行代码定义了宽度、高度以及视口应使用的摄影机。查看FitViewport类的文档
因此,当调用camera.position.set时,它会根据视口的宽度和高度设置x和y方向。整个示例定义了整个视口的视口尺寸
此示例与第二个示例之间的区别在于,摄影机围绕已加载到屏幕上的纹理进行设置。因此,视口的x和y方向已定位,纹理/摄影机的宽度、高度、原点、原点也已定义:
int width = cavemanTexture.getWidth();
int height = cavemanTexture.getHeight();
float originX = width * 0.5f;
float originY = height * 0.5f;
然后,Libgdx允许您使用spritebatch类绘制纹理,以绘制纹理和该纹理周围的视口
摘要
示例一允许您自己定义视口,而无需绘制任何纹理。这将允许您在设置相同视口的情况下绘制多个纹理(游戏创建的正常过程)
但在示例2中,如果希望视口显示,请在屏幕上跟随主要角色。可以定义纹理周围的视口以跟随该纹理
就我个人而言,我一直在追求第一个例子,因为你可以为任何游戏宽度或高度定义一个视口,然后我会在桌面上创建第二个视口,以跟随我在屏幕上绘制的任何纹理。它们都起作用,只是原因不同
希望这能帮你把事情弄清楚。
快乐编码
布拉德利。不用担心,伙计,不管怎样,尝试创建多个视口,一个用于菜单和界面,另一个用于世界视图。正在运行的演示superjumper可能有助于解决这一问题。但是,如果你感到困惑,不要担心,只要先自己尝试一下就可以了。