Libgdx,什么时候用camera.position.set?
Libgdx , When to use camera.position.set?
我真的很困惑与视口和正交相关的两个例子。虽然我知道 Viewport 是我们设置为在屏幕和相机投影上查看的尺寸的大小。我正在学习 libgdx,无法完成正交相机和视口示例,这让我完全困惑。代码在两个示例中都运行良好,并且在屏幕上显示了正确的结果。
这是一个示例,其中 camera.position.set 用于定位相机。
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.position.set(SCENE_WIDTH * 0.5f, SCENE_HEIGHT * 0.5f, 0.0f);
这是另一个不使用 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 方向进行了初始化。这是专门针对相机的。
camera = new OrthographicCamera();
因此,此代码从 libgdx 位创建者创建的 OrthographicCamera class 创建了一个相机对象。从 class 查看 class here 的文档,您可以看到它何时构建它接受 viewport_height 和 viewport_width。 (在您的示例中,您将其留空,因此这些暂时为 0。)
viewport = new FitViewport(SCENE_WIDTH, SCENE_HEIGHT, camera);
这行代码定义了视口的宽度、高度以及应使用哪个相机。查看 FitViewport 的文档 class here
因此,当调用 camera.position.set 时,它会根据视口的宽度和高度设置 x 和 y 方向。整个示例定义了整个视口的视口尺寸。
这个例子和第二个例子的区别在于相机是围绕已经加载到屏幕上的纹理设置的。因此视口的 x 和 y 方向已经定位,texture/camera 的宽度、高度、originX、originY 也已定义:
int width = cavemanTexture.getWidth();
int height = cavemanTexture.getHeight();
float originX = width * 0.5f;
float originY = height * 0.5f;
Libgdx 然后允许您使用 spritebatch class 绘制纹理以绘制纹理和围绕该纹理的视口。
总结
示例 1 允许您在不绘制任何纹理的情况下自行定义视口。这将允许您在设置相同视口的情况下绘制多个纹理(游戏创建的正常过程)
但是在示例二中,如果您想让视口说话,请跟随屏幕上的主要角色。您可以定义纹理周围的视口以跟随该纹理。
就个人而言,我总是追求第一个示例,因为您可以为任何游戏宽度或高度定义一个视口,然后我会在顶部创建第二个视口以跟随我在屏幕上绘制的任何纹理。他们都工作,只是出于不同的原因。
希望这可以帮助您理清头绪。
编码愉快,
布拉德利
我真的很困惑与视口和正交相关的两个例子。虽然我知道 Viewport 是我们设置为在屏幕和相机投影上查看的尺寸的大小。我正在学习 libgdx,无法完成正交相机和视口示例,这让我完全困惑。代码在两个示例中都运行良好,并且在屏幕上显示了正确的结果。
这是一个示例,其中 camera.position.set 用于定位相机。
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.position.set(SCENE_WIDTH * 0.5f, SCENE_HEIGHT * 0.5f, 0.0f);
这是另一个不使用 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 方向进行了初始化。这是专门针对相机的。
camera = new OrthographicCamera();
因此,此代码从 libgdx 位创建者创建的 OrthographicCamera class 创建了一个相机对象。从 class 查看 class here 的文档,您可以看到它何时构建它接受 viewport_height 和 viewport_width。 (在您的示例中,您将其留空,因此这些暂时为 0。)
viewport = new FitViewport(SCENE_WIDTH, SCENE_HEIGHT, camera);
这行代码定义了视口的宽度、高度以及应使用哪个相机。查看 FitViewport 的文档 class here
因此,当调用 camera.position.set 时,它会根据视口的宽度和高度设置 x 和 y 方向。整个示例定义了整个视口的视口尺寸。
这个例子和第二个例子的区别在于相机是围绕已经加载到屏幕上的纹理设置的。因此视口的 x 和 y 方向已经定位,texture/camera 的宽度、高度、originX、originY 也已定义:
int width = cavemanTexture.getWidth();
int height = cavemanTexture.getHeight();
float originX = width * 0.5f;
float originY = height * 0.5f;
Libgdx 然后允许您使用 spritebatch class 绘制纹理以绘制纹理和围绕该纹理的视口。
总结
示例 1 允许您在不绘制任何纹理的情况下自行定义视口。这将允许您在设置相同视口的情况下绘制多个纹理(游戏创建的正常过程)
但是在示例二中,如果您想让视口说话,请跟随屏幕上的主要角色。您可以定义纹理周围的视口以跟随该纹理。
就个人而言,我总是追求第一个示例,因为您可以为任何游戏宽度或高度定义一个视口,然后我会在顶部创建第二个视口以跟随我在屏幕上绘制的任何纹理。他们都工作,只是出于不同的原因。
希望这可以帮助您理清头绪。 编码愉快,
布拉德利