Learn what Felgo offers to help your business succeed. Start your free evaluation today! Felgo for Your Business

MovementAnimation

Modifies a property with a constant velocity and acceleration. More...

Import Statement: import Felgo 4.0

Properties

Signals

Methods

Detailed Description

This component modifies any property with a constant velocity and acceleration. Compared to the QML Animation elements, this component has a significant difference: You do not need to specify a duration. So the velocity and acceleration are applied endlessly, unless stop() is called or running is set to false.

You can use this component for any task where you do not know the target value and duration beforehand. For example when you want to move the parallax background layer of a level with a constant velocity. If this velocity should change over time, also an acceleration can be set.

For convenience, there are border properties which limit the calculations of the property value and the velocity. However, these are all optional. The only required parameters of this component is the target, the property and the running property.

Moving a Game Entity with MovementAnimation

With the MovementAnimation, you can animate any property of an object over time, but it is especially useful for positioning entities. You can move an entity from position A to B either with the physics-based components like BoxCollider and setting the ColliderBase::linearVelocity and ColliderBase::angularVelocity, or by using the MovementAnimation.

This example shows how to move an entity with a velocity of 100px per second to the right, and 50px per second to the bottom. So with this example, after 1 second the x position of the entity will be 100, and the y position 50:

 GameEntity {

   MovementAnimation {
     target: parent
     property: "pos"

     velocity: Qt.point(100, 50)

     // start running from the beginning
     running: true
   }
 }

Rotating a Game Entity with MovementAnimation

Similar to the movement, also the rotation can be changed over time. With the following example, the rotation is modified by an acceleration of 50. This means that the velocity will start with the initial setting 100 (so 100 degrees per second to the right as it is a positive value, a negative would rotate it counter-clockwise). And after 1 second, the velocity will be 150, because the acceleration defines how much the velocity changes each second. Consider this example:

 EntityBase {

   MovementAnimation {
     target: parent
     property: "rotation"

     velocity: 100
     acceleration: 50

     // start running from the beginning
     running: true
   }
 }

Advanced Usage

The following example combines all possible options. Usually, you will not need to set all of them, but for demonstration purposes:

 Rectangle {
   id: entity
   x: 30; y: 20
   width: 30; height: 20
   color: "blue"

   MovementAnimation {
     target: entity
     property: "x"
     running: true

     // the starting velocity
     velocity: 960

     // this forces the rectangle to move to the left (against the velocity direction), but it doesnt get faster than -20 px/second!
     acceleration: -260
     minVelocity: -20
     // limits the initial velocity set to 960, now to 500
     maxVelocity: 500

     // limits the x property between a border of 10 and 100
     minPropertyValue: 10
     maxPropertyValue: 100

     // never change the x value by more than 50 pixels in one step
     // this is useful for example to limit the rotation from MoveToPointHelper
     maxPropertyValueDifference: 50


     // this is the same as setting running to true, only for demonstration purpose
     //Component.onCompleted: movement.start()
   }
 }

Note: The API, i.e the properties and functions of this component are similar to the QML Animation elements, but they do not share the same base class. So some functions are different and some properties are not supported which do not make sense for this kind of animation. It also cannot be used in SequentialAnimation or ParallelAnimation containers, because the base class is not the same. However, adding MovementAnimations to these containers is not intended, because by its nature it has no fixed duration but runs endlessly. So any animations put after it in a SequentialAnimation would never be started.

Property Documentation

maxPropertyValue : variant

minPropertyValue : variant

These properties limit the property value of the target to be within the [minPropertyValue maxPropertyValue] range. By default, they are not set and thus the property is modified by the velocity setting. The property type is either real or point.

You can use this property for example when modifying the position property: you can set borders with them, which the entity can not exceed. For example, the following code keeps the GameEntity inside the Scene:

 GameWindow {

   Scene {

     EntityBase {

       MovementAnimation {
         target: parent
         property: "pos"
         velocity: Qt.point(30, 10)
         running: true

         // limit the entity within the scene size
         minPropertyValue: scene.pos
         maxPropertyValue: Qt.point(scene.width, scene.height)
       }
     }
   }
 }

maxVelocity : variant

minVelocity : variant

These properties limit the velocity value to be within the [minVelocity maxVelocity] range. By default, they are not set and thus the velocity can change freely. The property type is either real or point.


acceleration : variant

This property specifies how much the velocity changes per second. Can either be of type real or point (when the position should be modified).

If the acceleration is set to 10 for example, and the initial velocity is set to 20, it will be 30 after one second. Here is the source code for this example:

 Item {

   MovementAnimation {
     target: parent
     property: "rotation"
     running: true

     velocity: 20
     acceleration: 10
   }
 }

maxPropertyValueDifference : variant

This property holds the maximum difference the propertyValue can have within one time step. You can use it for example, to prevent the rotation exceeding a maximum rotation value. This value might come from the MoveToPointHelper component, which provides the exact rotation towards a target point.

Consider this example, where the followerEntity rotates towards the targetPoint, and does not over-rotate thanks to the maxPropertyValueDifference setting:

 GameEntity {
   id: followerEntity

   MoveToPointHelper {
     id: moveToPointHelper
     // the target point the follerEntity should rotate to
     // its rotation will then be 45 degrees, because it starts at position 0/0 with 0 rotation
     targetPoint: Qt.point(100, 100)
   }

   MovementAnimation {
     target: followerEntity
     property: "rotation"
     running: true
     velocity: 300*moveToPointHelper.outputXAxis

     // this avoids over-rotating, so rotating further than allowed
     maxPropertyValueDifference: moveToPointHelper.absoluteRotationDifference
   }
 }

See also MoveToPointHelper::outputXAxis.


property : string

This string defines the property of the target that should be animated. For example, it can be set to x, y, rotation, or pos (when the position should be animated).

The other properties including velocity or acceleration, must be the same type like this property. So when the property is set to "x", the velocity must be of type real. When it is set to "pos", the type must be point.


running : bool

This property holds whether the animation is running. By default, animations are not running.

This is an example of an animation that is toggled with every mouse click:

 Scene {

   MouseArea {
     anchors.fill: parent

     // toggles the running property
     // the velocity does not have with the value 40 when toggled, but it has the last value when running changed to false
     onClicked: movementAnimation.running = !movementAnimation.running
   }

   EntityBase {

     MovementAnimation {
       id: movementAnimation
       target: parent
       property: "x"
       velocity: 40
     }
   }
 }

target : Object

The target object whose property should be animated.


velocity : variant

The speed how much the property changes per second. Can either be of type real or point (when the position should be modified).

If the velocity is set to 10 for example, and the initial rotation is set to 50, it will be 60 after one second. Here is the source code for this example:

 Item {
   rotation: 50

   MovementAnimation {
     target: parent
     property: "rotation"
     velocity: 10
     running: true
   }
 }

Signal Documentation

limitReached()

This handler is called when minPropertyValue or maxPropertyValue was reached. In case the minPropertyValue or maxPropertyValue are of type point, the handler is called when either x or y is reaching its borders. The signal will be emitted until the boundaries or the target property have been reset. In wrapping mode it is called just before the target property is set to the new min/max value. It can be used for looping the movement manually, for instance, an endless background can be created using the signal by setting a new max value when the signal is reached.

Note: The corresponding handler is onLimitReached.


started()

This handler is called when start() was called or running changed from false tu true.

Note: The corresponding handler is onStarted.


stopped()

This handler is called when stop() was called or running changed from true to false.

Note: The corresponding handler is onStopped.


Method Documentation

void start()

Call this function to start the animation.

If the animation is already running, calling this method has no effect. The running property will be true following a call to start().

When started, the last velocity value will be used. If you want to reset the velocity to the value at first start, you must reset it manually be changing the velocity property.

See also started and running.


void stop()

Call this function to stop the animation.

If the animation is not running, calling this method has no effect. The running property will be false following a call to stop().

The velocity value is not changed automatically after a call to stop.

See also stopped and running.


Qt_Technology_Partner_RGB_475 Qt_Service_Partner_RGB_475_padded