public class VelocityTracker1D internal constructor(
// whether the data points added to the tracker represent differential values
// (i.e. change in the tracked object's displacement since the previous data point).
// If false, it means that the data points added to the tracker will be considered as absolute
// values (e.g. positional values).
public val isDataDifferential: Boolean = false,
// The velocity tracking strategy that this instance uses for all velocity calculations.
private val strategy: Strategy = Strategy.Lsq2,
)
A velocity tracker calculating velocity in 1 dimension.
Add displacement data points using addDataPoint, and obtain velocity using calculateVelocity.
Note: for calculating touch-related or other 2 dimensional/planar velocities, please use VelocityTracker, which handles velocity tracking across both X and Y dimensions at once.
Functions
addDataPoint
public fun addDataPoint(timeMillis: Long, dataPoint: Float)
Adds a data point for velocity calculation at a given time, timeMillis. The data ponit represents an amount of a change in position (for differential data points), or an absolute position (for non-differential data points). Whether or not the tracker handles differential data points is decided by isDataDifferential, which is set once and finally during the construction of the tracker.
Use the same units for the data points provided. For example, having some data points in cm and some in m will result in incorrect velocity calculations, as this method (and the tracker) has no knowledge of the units used.
calculateVelocity
public fun calculateVelocity(): Float
Computes the estimated velocity at the time of the last provided data point.
The units of velocity will be units/second, where units is the units of the data points provided via addDataPoint.
This can be expensive. Only call this when you need the velocity.
calculateVelocity
public fun calculateVelocity(maximumVelocity: Float): Float
Computes the estimated velocity at the time of the last provided data point.
The method allows specifying the maximum absolute value for the calculated velocity. If the absolute value of the calculated velocity exceeds the specified maximum, the return value will be clamped down to the maximum. For example, if the absolute maximum velocity is specified as "20", a calculated velocity of "25" will be returned as "20", and a velocity of "-30" will be returned as "-20".
Parameters
| maximumVelocity | the absolute value of the maximum velocity to be returned in units/second, where units is the units of the positions provided to this VelocityTracker. |
resetTracking
public fun resetTracking()
Clears data points added by addDataPoint.