Java libGDX 相机坐标与 window 坐标不匹配时不会绘制精灵
Java libGDX will not draw sprites when the camera coordinates do not match the window coordinates
我的 libGDX 引擎有问题。我有一对变量来设置屏幕的宽度和高度,还有一对变量来设置相机的宽度和高度。但是,只要相机与屏幕的尺寸不同,引擎就不会渲染任何精灵。这是我的代码供您查看:
我的常量:
public abstract class Constants
{
public static final int WINDOW_WIDTH = 800;
public static final int WINDOW_HEIGHT = 600;
public static final int CAMERA_WIDTH = 800;
public static final int CAMERA_HEIGHT = 600;
}
创建 Sprite 的位置:
public class GameController
{
public static final String TAG = GameController.class.getName();
public Sprite[] testSprites;
public int selectedSprite;
public GameController()
{
init();
}
private void init()
{
initTestObjects();
}
private void initTestObjects()
{
testSprites = new Sprite[1];
int width = 32;
int height = 32;
Pixmap pixmap = createProceduralPixmap(width, height);
Texture texture = new Texture(pixmap);
for(int i = 0; i < testSprites.length; i++)
{
Sprite spr = new Sprite(texture);
spr.setSize(32, 32);
spr.setOrigin(spr.getWidth() / 2.0f, spr.getHeight() / 2.0f);
spr.setPosition(0, 0);
testSprites[i] = spr;
}
selectedSprite = 0;
}
private Pixmap createProceduralPixmap(int width, int height)
{
Pixmap pixmap = new Pixmap(width, height, Format.RGBA8888);
pixmap.setColor(1, 0, 0, 0.5f);
pixmap.fill();
pixmap.setColor(1, 1, 0, 1);
pixmap.drawLine(0, 0, width, height);
pixmap.drawLine(width, 0, 0, height);
pixmap.setColor(0, 1, 1, 1);
pixmap.drawRectangle(0, 0, width, height);
return pixmap;
}
public void update(float deltaTime)
{
updateTestObjects(deltaTime);
}
public void updateTestObjects(float deltaTime)
{
float rotation = testSprites[selectedSprite].getRotation();
rotation += 90 * deltaTime;
rotation %= 360;
testSprites[selectedSprite].setRotation(rotation);
}
}
绘图:
public class GameRenderer implements Disposable
{
public static final String TAG = GameRenderer.class.getName();
private OrthographicCamera camera;
private SpriteBatch batch;
private GameController gameController;
public GameRenderer(GameController gameController)
{
this.gameController = gameController;
init();
}
private void init()
{
batch = new SpriteBatch();
camera = new OrthographicCamera(Constants.CAMERA_WIDTH,
Constants.CAMERA_HEIGHT);
camera.position.set(0, 0, 0);
camera.update();
}
public void render()
{
renderTestObjects();
}
private void renderTestObjects()
{
batch.setProjectionMatrix(camera.combined);
batch.begin();
for(Sprite sprite : gameController.testSprites)
{
sprite.draw(batch);
}
batch.end();
}
public void resize(int width, int height)
{
camera.viewportWidth =
(Constants.CAMERA_HEIGHT / height) * width;
camera.update();
}
@Override
public void dispose()
{
batch.dispose();
}
}
上面的代码工作正常并且会在屏幕中央渲染一个精灵,但是当我将 CAMERA_WIDTH 和 CAMERA_HEIGHT 分别更改为 80 和 60 时,而不是将精灵设为 10x像它应该的那样大,它不画任何东西。有什么建议吗?
您将整数除以整数,因此最终得到的数字比例很差。例如:
camera.viewportWidth =
(Constants.CAMERA_HEIGHT / height) * width;
如果屏幕宽高分别为800和600,而CAMERA_HEIGHT
为60,则等式变为
camera.viewportWidth =
(60 / 600) * 800;
变成(由于纯整数数学):
camera.viewportWidth =
(0) * 800;
变为 0。你需要在除法之前将这些整数转换为浮点数!这可能不是唯一的问题,但它本身可能会导致您的问题。
我的 libGDX 引擎有问题。我有一对变量来设置屏幕的宽度和高度,还有一对变量来设置相机的宽度和高度。但是,只要相机与屏幕的尺寸不同,引擎就不会渲染任何精灵。这是我的代码供您查看:
我的常量:
public abstract class Constants
{
public static final int WINDOW_WIDTH = 800;
public static final int WINDOW_HEIGHT = 600;
public static final int CAMERA_WIDTH = 800;
public static final int CAMERA_HEIGHT = 600;
}
创建 Sprite 的位置:
public class GameController
{
public static final String TAG = GameController.class.getName();
public Sprite[] testSprites;
public int selectedSprite;
public GameController()
{
init();
}
private void init()
{
initTestObjects();
}
private void initTestObjects()
{
testSprites = new Sprite[1];
int width = 32;
int height = 32;
Pixmap pixmap = createProceduralPixmap(width, height);
Texture texture = new Texture(pixmap);
for(int i = 0; i < testSprites.length; i++)
{
Sprite spr = new Sprite(texture);
spr.setSize(32, 32);
spr.setOrigin(spr.getWidth() / 2.0f, spr.getHeight() / 2.0f);
spr.setPosition(0, 0);
testSprites[i] = spr;
}
selectedSprite = 0;
}
private Pixmap createProceduralPixmap(int width, int height)
{
Pixmap pixmap = new Pixmap(width, height, Format.RGBA8888);
pixmap.setColor(1, 0, 0, 0.5f);
pixmap.fill();
pixmap.setColor(1, 1, 0, 1);
pixmap.drawLine(0, 0, width, height);
pixmap.drawLine(width, 0, 0, height);
pixmap.setColor(0, 1, 1, 1);
pixmap.drawRectangle(0, 0, width, height);
return pixmap;
}
public void update(float deltaTime)
{
updateTestObjects(deltaTime);
}
public void updateTestObjects(float deltaTime)
{
float rotation = testSprites[selectedSprite].getRotation();
rotation += 90 * deltaTime;
rotation %= 360;
testSprites[selectedSprite].setRotation(rotation);
}
}
绘图:
public class GameRenderer implements Disposable
{
public static final String TAG = GameRenderer.class.getName();
private OrthographicCamera camera;
private SpriteBatch batch;
private GameController gameController;
public GameRenderer(GameController gameController)
{
this.gameController = gameController;
init();
}
private void init()
{
batch = new SpriteBatch();
camera = new OrthographicCamera(Constants.CAMERA_WIDTH,
Constants.CAMERA_HEIGHT);
camera.position.set(0, 0, 0);
camera.update();
}
public void render()
{
renderTestObjects();
}
private void renderTestObjects()
{
batch.setProjectionMatrix(camera.combined);
batch.begin();
for(Sprite sprite : gameController.testSprites)
{
sprite.draw(batch);
}
batch.end();
}
public void resize(int width, int height)
{
camera.viewportWidth =
(Constants.CAMERA_HEIGHT / height) * width;
camera.update();
}
@Override
public void dispose()
{
batch.dispose();
}
}
上面的代码工作正常并且会在屏幕中央渲染一个精灵,但是当我将 CAMERA_WIDTH 和 CAMERA_HEIGHT 分别更改为 80 和 60 时,而不是将精灵设为 10x像它应该的那样大,它不画任何东西。有什么建议吗?
您将整数除以整数,因此最终得到的数字比例很差。例如:
camera.viewportWidth =
(Constants.CAMERA_HEIGHT / height) * width;
如果屏幕宽高分别为800和600,而CAMERA_HEIGHT
为60,则等式变为
camera.viewportWidth =
(60 / 600) * 800;
变成(由于纯整数数学):
camera.viewportWidth =
(0) * 800;
变为 0。你需要在除法之前将这些整数转换为浮点数!这可能不是唯一的问题,但它本身可能会导致您的问题。