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

タッチ座標を確認する必要があります。

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

ここで、touchxとtouchyは入力x変数と入力y変数であり、uがx = 100、y=100に触れた場合のようにtouchが機能する計算を行います。

touchxは120になり、touchyは120になります

updateメソッドのsooはこれを行います

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

私はこれが役立つと思います

于 2013-10-03T15:57:10.620 に答える
0

画面サイズ/ゲームの比率を計算し、それを現在の画面サイズに掛けました。

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

画面幅が1024ピクセルの場合

もっと簡単な方法があるはずですが、これは私にとってはうまくいきます。

于 2014-06-03T13:57:31.163 に答える