VelocityTracker1D
class VelocityTracker1D
internal constructor(
val isDataDifferential: Boolean = false,
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.
Secondary Constructors
constructor(isDataDifferential: Boolean) : this(isDataDifferential, Strategy.Impulse)
Constructor to create a new velocity tracker. It allows to specify whether or not the tracker
should consider the data ponits provided via addDataPoint
as differential or
non-differential.
Differential data ponits represent change in displacement. For instance, differential data
points of 2, -1, 5
represent: the object moved by "2" units, then by "-1" units, then by
"5" units. An example use case for differential data points is when tracking velocity for an
object whose displacements (or change in positions) over time are known.
Non-differential data ponits represent position of the object whose velocity is tracked. For
instance, non-differential data points of 2, -1, 5
represent: the object was at position
"2", then at position "-1", then at position "5". An example use case for non-differential
data points is when tracking velocity for an object whose positions on a geometrical axis
over different instances of time are known.
Parameters
isDataDifferential | true if the data ponits provided to the constructed tracker are differential. false otherwise. |
Functions
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.
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.
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. |
fun resetTracking()
Clears data points added by addDataPoint
.