5

我试图让一个对象出现在人最后一次触摸的地方。但是,当我尝试这样做时,它出现在错误的位置。我认为这是因为输入返回的坐标与显示坐标不同,我的代码如下:

public class Core implements ApplicationListener, InputProcessor
          { //Has to be here otherwise the code formatting becomes buggy


private Mesh squareMesh;
private PerspectiveCamera camera;
private Texture texture;
private SpriteBatch spriteBatch;
Sprite sprite;
float moveX = 0;


private final Matrix4 viewMatrix = new Matrix4();
private final Matrix4 transformMatrix = new Matrix4();

@Override
public void create()
{
    Gdx.input.setInputProcessor(this);


    texture = new Texture(Gdx.files.internal("door.png"));

    spriteBatch = new SpriteBatch();
    sprite = new Sprite(texture);
    sprite.setPosition(0, 0);
    viewMatrix.setToOrtho2D(0, 0, 480, 320);

    float x = 0;
    float y = 0;

}

@Override
public void dispose()
{
}

@Override
public void pause()
{
}

@Override
public void render()
{
    viewMatrix.setToOrtho2D(0, 0, 480, 320);
    spriteBatch.setProjectionMatrix(viewMatrix);
    spriteBatch.setTransformMatrix(transformMatrix);
    spriteBatch.begin();
    spriteBatch.disableBlending();
    spriteBatch.setColor(Color.WHITE);

    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);


    //spriteBatch.draw(texture, 0, 0, 1, 1, 0, 0, texture.getWidth(),
    //      texture.getHeight(), false, false);
    sprite.draw(spriteBatch);
    spriteBatch.end();
    update();
}

@Override
public void resize(int width, int height)
{
    float aspectRatio = (float) width / (float) height;
    camera = new PerspectiveCamera(67, 2f * aspectRatio, 2f);

}

@Override
public void resume()
{
}

public void update()
{

    float delta = Gdx.graphics.getDeltaTime();

    if(Gdx.input.isTouched())
    {
         Vector3 worldCoordinates = new Vector3(sprite.getX(), sprite.getY(), 0);
         camera.unproject(worldCoordinates);
        sprite.setPosition(Gdx.input.getX(), Gdx.input.getY());

        float moveX = 0;
        float moveY = 0;

 }
     }

为了简单起见,我裁剪了这段代码。我还制作了一个演示该错误的视频: http ://www.youtube.com/watch?v=m89LpwMkneI

4

4 回答 4

27

Camera.unproject将屏幕坐标转换为世界坐标。

Vector3 pos = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
camera.unproject(pos);
sprite.setPosition(pos.x, pos.y);
于 2012-07-16T16:35:49.750 回答
1

首先, Gdx.input.getX() 和 Gdx.input.getY() 返回“屏幕坐标”。您想将这些转换为您的“相机坐标”。屏幕坐标通常在窗口的左上角有 (0,0)。我认为您的相机坐标在左下角有 (0,0) (libgdx 或 opengl 都在这样做)。你的视频似乎表明这是真的。因此,您需要将 Y 值乘以 -1。其次,我怀疑屏幕的比例与相机的比例不同。我认为您可以通过乘以(世界/屏幕)来固定比例。

假设您的屏幕宽度=800,高度=600,而您的世界宽度=480,高度=320。那么你的精灵的新 X,Y 应该是:

X = Gdx.input.getX()*(480/800)
Y = Gdx.input.getY()*(320/600)*-1
于 2011-11-14T16:13:24.630 回答
0

我计算出屏幕尺寸/游戏比例并将其乘以当前屏幕尺寸:

rect.x=((((1024/Gdx.graphics.getWidth()))* Gdx.graphics.getWidth())

对于 1024 像素的屏幕宽度

必须有一种更简单的方法,但这对我有用。

于 2014-06-03T13:57:31.163 回答
0

你应该检查你的触摸坐标。

Gdx.app.log("", "hello x"+touchx);
Gdx.app.log("", "hello x"+touchy);

这里 touchx 和 touchy 是您的输入 x 和输入 y 变量然后进行计算触摸应该工作的地方就像您触摸 x=100,y=100

touchx 即将到来 120 并且 touch y 即将到来 120

soo 在您的更新方法中执行此操作

sprite.setPosition(Gdx.input.getX()-20, Gdx.input.getY()-20);

我认为这会有所帮助

于 2013-10-03T15:57:10.620 回答