libGDX SpiriteBatch.draw() 未定位在正确的位置
这是一个对我来说无法正常运行的示例代码,我做错了什么吗?
textureCharacter = new Texture(Gdx.files.internal("data/character1.png"));
if (Gdx.input.isTouched()) {
spriteBatch.draw(textureCharacter, Gdx.input.getX(), Gdx.input.getY());
}
当我触摸位于 X=5 和 Y=5 的 SpiriteBatch 时(例如),它会在 X 5 处绘制纹理,但 Y 是 Gdx.graphics.getHeight() - 5px ???通过向下移动输入 Y,纹理向上移动...
Gdx.input.getX() 和 Gdx.input.getY() 返回值: X=5, Y=5
我想做的只是将纹理移动到我正在触摸/移动的输入位置。
Here is a sample code that is not correctly functioning for me, am I doing something wrong?
textureCharacter = new Texture(Gdx.files.internal("data/character1.png"));
if (Gdx.input.isTouched()) {
spriteBatch.draw(textureCharacter, Gdx.input.getX(), Gdx.input.getY());
}
When I touch the the SpiriteBatch at the location X=5 and Y=5 (for example) it draws me the texture at X 5 but Y is the Gdx.graphics.getHeight() - 5px ??? By moving the input Y down, the texture moves up...
Gdx.input.getX() and Gdx.input.getY() are returning the values: X=5, Y=5
What I'm trying to do is just move the texture to the input positions I'm touching/moving.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
屏幕坐标不一定与模型空间坐标相同。您的相机定义是什么样的? (因为它定义了模型空间到屏幕的映射。)
Gdx.intput.getX()
调用返回屏幕空间中的一个点。Y 轴(默认情况下)在屏幕上和 GL 空间中指向相反的方向。 (在屏幕坐标中从屏幕顶部向下生长,在 GL 坐标中向屏幕顶部向上生长)。您可以修复相机以匹配屏幕坐标,也可以将触摸坐标映射到 GL 坐标。
请参阅以下位置中对
camera.unproject()
的调用:https://code.google.com/p/libgdx/source/browse/trunk/tests/gdx-tests/src/com/badlogic/gdx/tests/examples/MoveSpriteExample.java
Screen coordinates are not necessarily the same as model-space coordinates. What does your camera definition look like? (Since that defines the mapping of model space to the screen.) The
Gdx.intput.getX()
call returns a point in screen space.The Y-axis (by default) points in opposite directions on the screen and in GL space. (grows down from the top of the screen in screen coordinates, and grows up towards the top of the screen in GL coords). You can either fix your camera to match the screen coordinates or map your touch coordinates into GL coordinates.
See the call to
camera.unproject()
in:https://code.google.com/p/libgdx/source/browse/trunk/tests/gdx-tests/src/com/badlogic/gdx/tests/examples/MoveSpriteExample.java