我试图让对象播放器不断跟随我的鼠标,但我在相机投影方面遇到了一些问题。
* Displays the Game<br>
*/
private void showGame() {
batch.setProjectionMatrix(camera.projection);
batch.setTransformMatrix(camera.view);
batch.begin();
bitfontBig.setColor(CurrentColor);
// draw the highscore
if (score < 10) {
bitfontBig
.draw(batch,
"0" + String.valueOf(score),
380 - (bitfontBig.getBounds("0"
+ String.valueOf(score)).width / 2),
(1200 / 2)
+ (bitfontBig.getBounds("0"
+ String.valueOf(score)).height / 2));
} else {
bitfontBig
.draw(batch,
String.valueOf(score),
380 - (bitfontBig.getBounds(String.valueOf(score)).width / 2),
(1200 / 2)
+ (bitfontBig.getBounds(String
.valueOf(score)).height / 2));
}
// Draw the Spikes
for (Spike spike : spikes) {
if (bird.flyToTheLeftSide) {
batch.draw(Texture_Spike_Left, (-30) + spike.size, spike.y);
} else {
batch.draw(Texture_Spike_Right, 672 - spike.size, spike.y);
}
}
// Draw the Bird
if (bird.flyToTheLeftSide) {
CurrentFrame = BirdAnimation.getKeyFrame(statetime, true);
} else {
CurrentFrame = BirdAnimationRight.getKeyFrame(statetime, true);
}
batch.draw(CurrentFrame, bird.position.x, bird.position.y);
batch.end();
////PLAYER
batch.begin();
PlayerSprite.draw(batch);
PlayerSprite.setPosition(Gdx.input.getX(), Gdx.input.getY());
batch.end();
shaperenderer.setProjectionMatrix(camera.projection);
shaperenderer.setTransformMatrix(camera.view);
shaperenderer.begin(ShapeType.Filled);
问题是当我向下移动鼠标时,Player 对象会上升,反之亦然。同时我认为它有一些延迟响应。
感谢任何帮助,
谢谢
最佳答案
libGDX 在内部反转 Y 轴,因此它指向上方,而不是下方。另外,原点从屏幕的左上角移动到左下角。由此产生的坐标系是数学家(和大多数其他人)所习惯的。要撤消反转,只需从屏幕高度中减去 y 值,如下所示:
PlayerSprite.setPosition(Gdx.input.getX(), Gdx.graphics.getHeight() - Gdx.input.getY());
如果玩家仍然表现得很奇怪,请尝试仅对 y 值求负。
PlayerSprite.setPosition(Gdx.input.getX(), -Gdx.input.getY());
另外,我会考虑尝试 Velijko 的建议:
shaperenderer.setProjectionMatrix(camera.combined);
响应延迟可能是由于您在更新 Player 之前绘制了 Player 造成的。因此,用户总是看到他“曾经”在哪里,而不是他“现在”在哪里。只需首先更新 Player,然后绘制它:
PlayerSprite.setPosition(Gdx.input.getX(), Gdx.graphics.getHeight() - Gdx.input.getY());
PlayerSprite.draw(batch);
我希望我能帮忙=)。
关于java - Libgdx 跟随鼠标 |投影?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26524401/