gpt4 book ai didi

java - 使用 libgdx 缩放和绘制纹理以触摸坐标

转载 作者:行者123 更新时间:2023-12-01 23:40:24 35 4
gpt4 key购买 nike

我正在尝试从屏幕中心到触摸坐标(时钟针)绘制纹理。为了说明这一点,我创建了一个 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. 计算从中心到触摸点的 vector 。
  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) {


}

}

关于java - 使用 libgdx 缩放和绘制纹理以触摸坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18042830/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com