How to change animation movement to face rotation direction?



  • Hi,
    My driving game involves a tank moving forwards and turning when the turn button is pressed.
    I am able to make the tank move in the direction it is facing. But if that direction changes whilst the move animation is playing, I don't know how to make the tank movement change dynamically, to keep the movement in line with the direction if is facing.

    The solution I currently have is by setting a very short distance for the movement animation, so that at the beginning of each animation it is able to change the direction based on any rotation change.
    The problem with this is that by using this method I can only make the movement animation length about 10 pixels - otherwise it produces a distinct sideways motion. And by only moving it 10 pixels, when I want the car to move FAST it means a lot of animations per second... at the high end of speed, this produces a flickering effect in my tank.

    I'm sure there must be a better way of doing this... can anyone help please? I've copied a sample of my code below to illustrate how I'm currently doing it.

    Thank you.

    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
    }
    }


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.