2011-11-12 5 views
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; 

} 
    } 

私はsimplictyのためにこのコードをトリミング。 http://www.youtube.com/watch?v=m89LpwMkneI

答えて

27

Camera.unprojectは、画面座標をワールド座標に変換します。

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

これは正解でなければなりません。 – HAL9000

+0

受け入れられた解決策を試しましたが、画面の解像度が変更されると面倒です。これは、必要な計算をすべて書き直したくない場合に行く方法です。 – MxyL

+0

私は受け入れられた答えを変更しました。これは私が最初の答えを受け入れた後に掲載されました。 – Derek

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 
+0

なぜスターの前にバックスラッシュを追加しましたか? – HAL9000

0

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

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

touchxと微妙なタッチが を動作するはずここで、uは、Y、X = 100に触れ= 100

とtouchxは120 に来ているかのように、計算を行い、あなたの入力xと入力yの変数が されており、 yは、あなたの更新方法では、秀120

を来ているのタッチがこの

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

iは番目思います

rect.x =((((1024/Gdx.graphics.getWidth()))* GDX:私は画面サイズ/ゲーム比を考え出したし、現在の画面サイズにそれを掛け

0

役立ちますです.graphics.getWidth())

1024個のピクセル

の画面幅のためしかし、これは私の作品もっと簡単な方法があるに違いありません。

関連する問題