LibGDX 使用 touchDragged() 事件旋转 2D 图像

LibGDX Rotate 2D image with touchDragged() event

Objective: 在屏幕中央旋转图像,移动等于向左或向右的 touchDragged 事件。

现在我创建了一个基本的舞台,并在舞台上添加了一个演员 (centerMass.png)。它是这样创建和呈现的:

public class Application extends ApplicationAdapter {
Stage stageGamePlay;

@Override
public void create () {
    //setup game stage variables
    stageGamePlay = new Stage(new ScreenViewport());
    stageGamePlay.addActor(new CenterMass(new Texture(Gdx.files.internal("centerMass.png"))));

    Gdx.input.setInputProcessor(stageGamePlay);

}

@Override
public void render () {
    Gdx.gl.glClearColor(255f/255, 249f/255, 236f/255, 1f);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    //before drawing, updating actions that have changed
    stageGamePlay.act(Gdx.graphics.getDeltaTime());
    stageGamePlay.draw();

    }
}

然后我有一个单独的 class 文件,其中包含 CenterMass class,扩展图像。我足够熟悉,知道我可以扩展 Actor,但我不确定使用 Actor 与 Image 可以获得什么好处。

在 CenterMass 中 class 我创建纹理、设置边界、设置可触摸并将其置于屏幕中心。

在 CenterMass 内部 class 我还有一个 InputListener 监听事件。我为 touchDragged 设置了覆盖设置,我试图获取拖动的 X 和 Y,并使用它来相应地设置旋转动作。 class 看起来像这样:

//extend Image vs Actor classes
public class CenterMass extends Image {
public CenterMass(Texture centerMassSprite) {
    //let parent be aware
    super(centerMassSprite);
    setBounds(getX(), getY(), getWidth(), getHeight());
    setTouchable(Touchable.enabled);
    setPosition(Gdx.graphics.getWidth()/2, Gdx.graphics.getHeight()/2);
    setRotation(90f);


    addListener(new InputListener(){
        private int dragX, dragY;
        private float duration;
        private float rotateBy = 30f;

        @Override
        public void touchDragged(InputEvent event, float x, float y, int pointer) {
            //get
            float dX = (float)(x-dragX)/(float)Gdx.graphics.getWidth();
            float dY = (float)(dragY-y)/(float)Gdx.graphics.getHeight();

            duration = 1.0f; // 1 second

            Actions.sequence(
                    Actions.parallel(
                            Actions.rotateBy(rotateBy, duration),
                            Actions.moveBy( dX, dY, duration)
                    )
            );


        }
    });
}

@Override
protected void positionChanged() {
    //super.positionChanged();
}

@Override
public void draw(Batch batch, float parentAlpha) {
    //draw needs to be available for changing color and rotation, I think
    batch.setColor(this.getColor());
    //cast back to texture because we use Image vs Actor and want to rotate and change color safely
    ((TextureRegionDrawable)getDrawable()).draw(batch, getX(), getY(),
            getOriginX(), getOriginY(),
            getWidth(), getHeight(),
            getScaleX(), getScaleY(),
            getRotation());
}

@Override
public void act(float delta) {
    super.act(delta);
    }
}

问题: 我无法让它按照我想要的方式旋转。我已经能够让它以不可预知的方式移动。任何指导将不胜感激。

从你的代码来看,似乎一切都很好。除非您不设置图像的任何来源。在不设置原点的情况下,它默认设置为 0,0。(图像的左下角) 因此,如果您想要以原点为中心旋转图像,则必须将原点设置为 imageWidth/2。 imageHeight/2.

 setOrigin(imageWidth/2,imageHeight/2)// something like this