Consider that there is a vehicle moving towards a target at a constant speed. At a certain point, it sees a target and should slow down in order to come to a complete stop. How can this be achieved with animations in Qt Quick, keeping in mind that the moment at which it stops is not known, and will be issued by some AI system?
When running the application below, which is an attempt at solving this problem (albeit simplified, as the conditions for the stopping
state are known), the transition from the moving
state to stopping
is not smooth. How can I ease into the stopping
transition by starting it at the same speed the item was travelling at in the moving
state?
import QtQuick 2.3
import QtQuick.Controls 1.2
ApplicationWindow {
id: window
color: "black"
width: 150
height: 600
Timer {
running: true
interval: 1000
onTriggered: {
unit.state = "moving";
unit.y = window.height - unit.height;
}
}
Rectangle {
id: unit
width: 150
height: 150
radius: width
color: "white"
state: "stopped"
readonly property bool shouldStop: unit.y >= 200
onStateChanged: print(state)
states: [
State {
name: "stopped"
},
State {
name: "moving"
},
State {
name: "stopping"
when: unit.shouldStop
}
]
Behavior on y {
enabled: unit.state == "moving"
NumberAnimation {
duration: 1000
}
}
transitions: [
Transition {
from: "moving"
to: "stopping"
NumberAnimation {
target: unit
property: "y"
to: window.height - unit.height
duration: 2000
easing.type: Easing.OutQuad
}
}
]
}
}