1

我有以下旋转功能:

public void rotate( Actor a ) {
    float rotateBy = 30.0f,
          newX = ( a.getX() * ( float ) Math.cos( rotateBy ) ) - ( a.getY() * ( float ) Math.sin( rotateBy ) ),
          newY = ( a.getX() * ( float ) Math.sin( rotateBy ) ) + ( a.getY() * ( float ) Math.cos( rotateBy ) ),
          moveByX = a.getX() - newX, // delta
          moveByY = a.getY() - newY, // delta
          duration = 1.0f; // 1 second

    a.addAction(
        Actions.sequence(
            Actions.parallel(
                Actions.rotateBy( rotateBy, duration ),
                Actions.moveBy( moveByX, moveByY, duration )
            )
        )
    );
}

但似乎moveByXmoveByY值不准确。

目标是弄清楚旋转后新的 x,y 是什么,并向上移动这么多,以便它仍然保持与旋转前相同的 y。

期望的结果

另外,我的 Actor 的绘制方法:

    @Override
    public void draw( Batch batch, float parentAlpha ) {
        batch.draw( texture, getX(), getY(), getWidth()/2, getHeight()/2, getWidth(), getHeight(), getScaleX(), getScaleY(), getRotation() );
    }
4

1 回答 1

1

这就是我要做的方式:首先演员的原点必须是正方形的中心,然后在其中心旋转演员,你会看到这样的东西

问题

红线表示演员需要向上移动以跟随地板的 y 偏移量,您可以看到这个 y 偏移量增加和减少(所以也许您不能使用 Actions.moveBy 因为这会移动它线性方式,您的路径不是线性的,请参见蓝线)所以

Vector2 pos = new Vector2();
public void rotate(float delta, Actor a) {
    a.rotateBy(angularSpeed*delta);
    pos.x += linearSpeed*delta;
    a.setPosition(pos.x,pos.y+yOffset(a.getRotation());
}

yOffset 是根据正方形旋转角度描述红线长度的函数:

public float yOffset(float angle) {
    angle %= Math.PI/2;
    return (float) Math.abs(squareSize*(Math.sin((Math.PI/4+angle))-Math.sin(Math.PI/4)));
}

所有角度都以弧度为单位。

于 2014-05-14T00:19:55.193 回答