将像素坐标应用于屏幕坐标

时间:2011-11-12 12:11:23

标签: java libgdx

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

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;

 }
     }

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

4 个答案:

答案 0 :(得分:27)

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

Vector3 pos = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
camera.unproject(pos);
sprite.setPosition(pos.x, pos.y);

答案 1 :(得分: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

答案 2 :(得分:0)

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

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

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

和touchx即将到来120 触摸y即将到来120

更新方法中的

soo执行此操作

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

我认为这会有所帮助

答案 3 :(得分:0)

我弄清了屏幕尺寸/游戏比例并将其乘以当前屏幕尺寸:

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

,屏幕宽度为1024像素

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

相关问题