QML - 如何将动画移动改变为面向旋转方向?

时间:2016-04-05 07:45:38

标签: qt animation rotation qml

我是初学者,试图写一款驾驶游戏。它涉及一个坦克向前移动并在按下转动按钮时转动。 我能够使坦克朝着它面向的方向移动。但是如果移动动画正在播放时方向发生变化,我就不知道如何使坦克运动动态变化,以保持运动与朝向的方向一致。

我目前的解决方案是为移动动画设置一个非常短的距离,这样在每个动画开始时它都可以根据任何旋转变化改变方向。

这个问题是通过使用这种方法我只能使运动动画的长度大约为10个像素 - 否则会产生明显的侧向运动。只需将它移动10个像素,当我希望汽车快速移动时,它意味着每秒动画很多......在速度的高端,这会在我的坦克中产生闪烁效果。

我确定必须有更好的方法来做到这一点......任何人都可以帮忙吗?我已经复制了下面的代码示例,以说明我目前是如何做的。

我很感激任何回答,记住我是初学者 - 保持简单我是愚蠢的!

谢谢。

RotationAnimation  {
    id: tankTurnRightAnimator
    running: false
    target: tank
    property: "rotation"
    from: tank.rotation
    to: rotation + 360
    loops: Animation.Infinite
    duration: 3600
}
ParallelAnimation{
    id: tankMoveAnimation
    NumberAnimation {
        target: tank
        property: "y"
        to: y + ((tank.direction * pixelTravel) * Math.sin(tank.rotation * (Math.PI / 180)))
        duration: tank.curSpeed
    }
    NumberAnimation {
        target: tank
        property: "x"
        to: x + ((tank.direction * pixelTravel) * Math.cos(tank.rotation * (Math.PI /180)))
        duration: tank.curSpeed
    }

}

0 个答案:

没有答案
相关问题