簡體   English   中英

使用libgdx縮放並繪制紋理以觸摸坐標

[英]Scale and Draw texture to touch co-ordinates using libgdx

我正在嘗試從屏幕中心到觸摸坐標(時鍾指針)繪制紋理。 為了說明這一點,我創建了一個ShapeRenderer來創建從中心到接觸點的線。 下面的代碼

 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幫助我繪制紋理。 但是它不起作用。 下面的代碼

 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();
     } 

我非常感謝您為解決這一問題提供的幫助。

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

  1. 計算從中心到觸摸點的向量。
  2. 通過Math.atan2(y,x)將其轉換為極角phi
  3. 將phi從[-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) {


    }

}

使用以下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必須是紋理區域的原點。 縮放和旋轉應用於此點。

您需要做的是根據texureRegion將這些值賦予

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

這將為您工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM