如何在指定方向旋转后移动精灵?Libgdx-Java-Android

如何在指定方向旋转后移动精灵?Libgdx-Java-Android,java,android,libgdx,Java,Android,Libgdx,我有一个视频游戏,其中箭头在旋转箭头后向其指向的一侧移动,例如: 我需要将精灵移动到旋转后箭头指向的同一方向。 我要做的一点代码: int count = 0; @Override protected void handleInput() { if(Gdx.input.justTouched()){ // move to the direction of pointing: arrow.setPosition(x, y); } } p

我有一个视频游戏,其中箭头在旋转箭头后向其指向的一侧移动,例如:

我需要将精灵移动到旋转后箭头指向的同一方向。 我要做的一点代码:

int count = 0;
@Override
protected void handleInput() {
     if(Gdx.input.justTouched()){
         // move to the direction of pointing:
         arrow.setPosition(x, y);
     }

}

public void update(float dt){

    count++;
    // rotate sprite:
    arrow.setRotation(count);

}

最简单的方法是使用旋转量的正弦和余弦来确定平移向量的x和y分量。

在《用LibGDX开始Java游戏开发》一书中,作者制作了一个游戏,我认为它演示了您想要的行为。游戏是第三章的“海星收藏家”。玩家移动一只海龟来收集海星。左箭头键和右箭头键旋转海龟,向上箭头键将海龟朝其当前面对的方向向前移动

游戏的源代码可以从作者的Github帐户下载。(我不知道他为什么把它放在zip文件中。)

相关代码如下所示:

@Override
public void update(float dt) {
    // process input
    turtle.setAccelerationXY(0, 0);

    if (Gdx.input.isKeyPressed(Keys.LEFT)) {
        turtle.rotateBy(90 * dt);
    }
    if (Gdx.input.isKeyPressed(Keys.RIGHT)) {
        turtle.rotateBy(-90 * dt);
    }
    if (Gdx.input.isKeyPressed(Keys.UP)) {
        turtle.accelerateForward(100);
    }
    // ...
public void accelerateForward(float speed) {
    setAccelerationAS(getRotation(), speed);
}
// set acceleration from angle and speed
public void setAccelerationAS(float angleDeg, float speed) {
    acceleration.x = speed * MathUtils.cosDeg(angleDeg);
    acceleration.y = speed * MathUtils.sinDeg(angleDeg);
}
其中
turtle
扩展了一些扩展
Actor
的自定义类

accelerateForward
的代码如下所示:

@Override
public void update(float dt) {
    // process input
    turtle.setAccelerationXY(0, 0);

    if (Gdx.input.isKeyPressed(Keys.LEFT)) {
        turtle.rotateBy(90 * dt);
    }
    if (Gdx.input.isKeyPressed(Keys.RIGHT)) {
        turtle.rotateBy(-90 * dt);
    }
    if (Gdx.input.isKeyPressed(Keys.UP)) {
        turtle.accelerateForward(100);
    }
    // ...
public void accelerateForward(float speed) {
    setAccelerationAS(getRotation(), speed);
}
// set acceleration from angle and speed
public void setAccelerationAS(float angleDeg, float speed) {
    acceleration.x = speed * MathUtils.cosDeg(angleDeg);
    acceleration.y = speed * MathUtils.sinDeg(angleDeg);
}
然后,
setAccelerationAS
的代码如下所示:

@Override
public void update(float dt) {
    // process input
    turtle.setAccelerationXY(0, 0);

    if (Gdx.input.isKeyPressed(Keys.LEFT)) {
        turtle.rotateBy(90 * dt);
    }
    if (Gdx.input.isKeyPressed(Keys.RIGHT)) {
        turtle.rotateBy(-90 * dt);
    }
    if (Gdx.input.isKeyPressed(Keys.UP)) {
        turtle.accelerateForward(100);
    }
    // ...
public void accelerateForward(float speed) {
    setAccelerationAS(getRotation(), speed);
}
// set acceleration from angle and speed
public void setAccelerationAS(float angleDeg, float speed) {
    acceleration.x = speed * MathUtils.cosDeg(angleDeg);
    acceleration.y = speed * MathUtils.sinDeg(angleDeg);
}
请注意,这最后一段代码可能正是user unexistential所指的内容

(如果你正在学习LibGDX和游戏开发,我推荐这本书。它非常好。)

另请参见:


我通过本页解决了以下问题:

}


flecha.setPosition(posAuxX,posAuxY)

你想要实现什么?旋转雪碧时移动雪碧,或者先旋转雪碧,然后移动雪碧?如果解决了问题,则应将答案标记为正确。