2013-08-04 77 views
0

我想從屏幕的中心繪製紋理觸摸座標(時鐘手)。爲了說明這一點,我創建了一個ShapeRenderer來創建從中心到接觸點的一條線。下面的代碼規模和繪製紋理觸摸座標使用libgdx

if (Gdx.input.isTouched()) { 
     camera.unproject(touchPoint.set(Gdx.input.getX(), Gdx.input.getY(), 0)); 
     debugRenderer.begin(ShapeType.Line); 
     debugRenderer.setColor(Color.ORANGE); 
     debugRenderer.line(centerObject.x, centerObject.y, touchPoint.x, touchPoint.y); 

     debugRenderer.end() 
} 

這工作正常。現在我需要用實際圖像替換線(我需要根據觸摸位置進行縮放)。我試圖從中心繪製紋理使用的

public void draw (TextureRegion region, float x, float y, float originX, float originY, float width, float height, 
    float scaleX, float scaleY, float rotation) 

SpriteBatch幫我畫紋理point.I想。但它不工作。下面的代碼

private TextureRegion handRegion; 
private Texture handTexture; 

handTexture = new Texture(
       Gdx.files.internal("hand.png")); 
handRegion = new TextureRegion(handTexture); 

//origin_X,origin_Y are the center of the screen 

if (Gdx.input.isTouched()) { 
      camera.unproject(touchPoint.set(Gdx.input.getX(), Gdx.input.getY(), 0)); 
      game.batch.begin(); 
      game.batch.draw(handRegion, touchPoint.x, touchPoint.y, origin_X, origin_Y,10,100,1,1,0);    
      game.batch.end(); 
    } 

我真的很感謝任何幫助解決這一問題。

回答

3

您需要計算以度爲單位的旋轉(0..360)。

  1. 計算從中心到觸點的向量。
  2. 經由Math.atan2將其轉換爲極角披(Y,X)
  3. 量表披從[-PI..PI]至[0..360]
  4. 添加90因爲不同點起源的( ?)
package com.me.mygdxgame.Screens; 

import com.badlogic.gdx.Gdx; 
import com.badlogic.gdx.Screen; 
import com.badlogic.gdx.graphics.GL10; 
import com.badlogic.gdx.graphics.OrthographicCamera; 
import com.badlogic.gdx.graphics.Texture; 
import com.badlogic.gdx.graphics.g2d.SpriteBatch; 
import com.badlogic.gdx.graphics.g2d.TextureRegion; 
import com.badlogic.gdx.math.Vector3; 

public class HandScreen implements Screen{ 
    SpriteBatch batch = new SpriteBatch(); 
    OrthographicCamera camera; 
    TextureRegion handRegion; 
    Texture handTexture; 
    Vector3 touchPoint = new Vector3(0,0,0); 
    double rotation = 0; 
    float width,height; 
    @Override 
    public void render(float delta) { 

     Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); 
     if (Gdx.input.isTouched()) { 
      camera.unproject(touchPoint.set(Gdx.input.getX(), Gdx.input.getY(),0)); 

      // the center of your hand 
      Vector3 center = new Vector3(width/2,height/2,0); 
      // you need a vector from the center to your touchpoint 
      touchPoint.sub(center); 
      // now convert into polar angle        
      rotation = Math.atan2(touchPoint.y, touchPoint.x); 
      // rotation should now be between -PI and PI 
      // so scale to 0..1 
      rotation = (rotation + Math.PI)/(Math.PI * 2); 
      // SpriteBatch.draw needs degrees 
      rotation *= 360; 
      // add Offset because of reasons 
      rotation += 90; 
     } 


     batch.begin(); 
     batch.draw(handRegion, 
       width/2, // x, center of rotation 
       height/2, //y, center of rotation 
       0, // origin x in the texture region 
       0,// origin y in the texture region 
       10, // width 
       100, // height 
       1, // scale x 
       1,// scale y 
       (float)rotation); // rotation !   
     batch.end(); 
    } 


    @Override 
    public void show() { 
     handTexture = new Texture(Gdx.files.internal("img/coin.png")); 
     handRegion = new TextureRegion(handTexture); 
     width = Gdx.graphics.getWidth(); 
     height = Gdx.graphics.getHeight(); 
     camera = new OrthographicCamera();  
     camera.viewportHeight = height; 
     camera.viewportWidth = width; 
     camera.position.set(camera.viewportWidth * .5f, camera.viewportHeight * .5f, 0f); 
     camera.update(); 

    } 
    // rest of screen interface 
    @Override 
    public void hide() { 


    } 

    @Override 
    public void pause() { 


    } 

    @Override 
    public void resume() { 

    } 

    @Override 
    public void dispose() { 


    } 
    @Override 
    public void resize(int width, int height) { 


    } 

} 
+0

謝謝@Tino,它非常完美。 – RameshVel

0

當您使用spriteBatch

以下功能
public void draw (TextureRegion region, float x, float y, float originX, float originY, float width, float height, 
    float scaleX, float scaleY, float rotation) ; 

originX和originY必須是紋理區域的原點。根據這一點應用縮放和旋轉。

你需要做的就是給這些值accoring到texureRegion作爲

origin_x = handRegion.getRegionWidth()/2f; 
origin_y = handRegion.getRegionHeight()/2f; 

這會爲你工作。

+0

謝謝Vikalp。它仍然沒有工作。我在這裏感到困惑。我希望origin_x和origin_y始終居中。所以時鐘手紋理將從中心繪製到觸摸點。基於紋理需要縮放的距離。任何幫助? – RameshVel

+0

@RameshVel:像這樣使用它game.batch.draw(handRegion,touchPoint.x,touchPoint.y,5,50,10,100,1,1,0);這裏5和50分別是寬度和高度的一半,分別爲 –

+0

仍然相同。有沒有完全不同的方式?我認爲我做錯了什麼。 – RameshVel