Skip to contents

If speeds are constant, returns `speed` column of the `moment`. If speeds are functions, evaluates the speed functions and returns their results.

Usage

cur_speed(moment, cur_time = NULL)

Arguments

moment

Position tibble with extra columns `direction` and `speed`

cur_time

Current time

Speeds can be defined as functions and thus it is possible to make trajectories with speeds sensitive to time (harmonic movements or accelerations) or location (slower speeds in the center).

If there is only single speed function, all speeds are evaluated in one call. If there is more than one speed function, speeds are evaluated individually, one object at time.

Speed functions follow the template `function(time, moment)` and return a vector of speeds. They should be written to handle both vector calculations (moment is a tibble with multiple rows) and individual values (moment is a one-row-tibble).

Speed functions could be introduced via `settings$speed` or added directly into the `speed` column of the initial position tibble. When passing functions they should be encapsulated in a list (`settings$speed <- c(my_fun)`).

Value

numerical vector of speed

Examples

f2 <- function(time, moment) {
  5 + cos(time) + (moment$object %% 2) * 2
}
sett_move <-
  new_settings(
    speed = c(f2), xlim = c(-9, 9), ylim = c(-9, 9),
    bounce_off_square = FALSE,
    bounce_off_circle = TRUE, circle_bounce_jitter = pi / 6
  )
moment <- position8c %>% add_random_direction()
tt <- make_random_trajectory(
   moment, seq(0, 8, by = 0.1), sett_move, step_direct
)