15 #ifndef MOTIVE_TARGET_H
16 #define MOTIVE_TARGET_H
18 #include "motive/math/range.h"
19 #include "motive/math/vector_converter.h"
77 motive::ModularDirection
direction = motive::kDirectionClosest)
100 static const int kMaxNodes = 3;
140 assert(0 <= node_index && node_index < num_nodes_);
141 return nodes_[node_index];
148 assert(num_nodes_ > 0);
149 float min = start_value;
150 float max = start_value;
151 for (
int i = 0; i < num_nodes_; ++i) {
152 min = std::min(nodes_[i].value, min);
153 max = std::max(nodes_[i].value, max);
160 assert(num_nodes_ > 0);
161 return nodes_[num_nodes_ - 1].
time;
164 int num_nodes()
const {
return num_nodes_; }
165 const MotiveTarget1f* targets()
const {
return this; }
173 MotiveNode1f nodes_[kMaxNodes];
179 template <
int kDimensionsT>
182 static const int kDimensions = kDimensionsT;
185 assert(0 <= i && i < kDimensions);
190 assert(0 <= i && i < kDimensions);
235 float current_velocity = 0.0f) {
242 inline MotiveTarget1f Target1f(
float target_value,
float target_velocity,
243 MotiveTime target_time,
244 motive::ModularDirection direction =
245 motive::kDirectionClosest) {
246 assert(target_time >= 0);
247 return MotiveTarget1f(
248 MotiveNode1f(target_value, target_velocity, target_time, direction));
252 inline MotiveTarget1f CurrentToTarget1f(
253 float current_value,
float current_velocity,
float target_value,
254 float target_velocity, MotiveTime target_time,
255 motive::ModularDirection direction = motive::kDirectionClosest) {
256 return MotiveTarget1f(
257 MotiveNode1f(current_value, current_velocity, 0),
258 MotiveNode1f(target_value, target_velocity, target_time, direction));
262 inline MotiveTarget1f CurrentToTargetConstVelocity1f(
float current_value,
264 MotiveTime target_time) {
265 assert(target_time > 0);
266 const float velocity = (target_value - current_value) / target_time;
267 return MotiveTarget1f(MotiveNode1f(current_value, velocity, 0),
268 MotiveNode1f(target_value, velocity, target_time,
269 motive::kDirectionDirect));
274 inline MotiveTarget1f TargetToTarget1f(
float target_value,
275 float target_velocity,
276 MotiveTime target_time,
277 float third_value,
float third_velocity,
278 MotiveTime third_time) {
279 return MotiveTarget1f(
280 MotiveNode1f(target_value, target_velocity, target_time),
281 MotiveNode1f(third_value, third_velocity, third_time));
285 inline MotiveTarget1f CurrentToTargetToTarget1f(
286 float current_value,
float current_velocity,
float target_value,
287 float target_velocity, MotiveTime target_time,
float third_value,
288 float third_velocity, MotiveTime third_time) {
289 return MotiveTarget1f(
290 MotiveNode1f(current_value, current_velocity, 0),
291 MotiveNode1f(target_value, target_velocity, target_time),
292 MotiveNode1f(third_value, third_velocity, third_time));
296 template <
class VectorConverter,
int kDimensionsT>
298 typedef VectorConverter C;
301 static const int kDimensions = kDimensionsT;
302 typedef typename VectorT<C, kDimensionsT>::type Vec;
304 typedef const Vec& VecIn;
309 VecIn current_velocity = Vec(0.0f)) {
310 const float* current_value_in = C::ToPtr(current_value);
311 const float* current_velocity_in = C::ToPtr(current_velocity);
314 for (
int i = 0; i < kDimensions; ++i) {
315 t[i] = Current1f(current_value_in[i], current_velocity_in[i]);
324 VecIn target_value, VecIn target_velocity, MotiveTime target_time,
325 motive::ModularDirection direction = motive::kDirectionClosest) {
326 const float* target_value_in = C::ToPtr(target_value);
327 const float* target_velocity_in = C::ToPtr(target_velocity);
330 for (
int i = 0; i < kDimensions; ++i) {
331 t[i] = Target1f(target_value_in[i], target_velocity_in[i], target_time,
339 VecIn current_value, VecIn current_velocity, VecIn target_value,
340 VecIn target_velocity, MotiveTime target_time,
341 motive::ModularDirection direction = motive::kDirectionClosest) {
342 const float* current_value_in = C::ToPtr(current_value);
343 const float* current_velocity_in = C::ToPtr(current_velocity);
344 const float* target_value_in = C::ToPtr(target_value);
345 const float* target_velocity_in = C::ToPtr(target_velocity);
348 for (
int i = 0; i < kDimensions; ++i) {
349 t[i] = CurrentToTarget1f(current_value_in[i], current_velocity_in[i],
350 target_value_in[i], target_velocity_in[i],
351 target_time, direction);
359 MotiveTime target_time) {
360 const float* current_value_in = C::ToPtr(current_value);
361 const float* target_value_in = C::ToPtr(target_value);
365 for (
int i = 0; i < kDimensions; ++i) {
366 t[i] = CurrentToTargetConstVelocity1f(current_value_in[i],
367 target_value_in[i], target_time);
374 template <
class VectorConverter>
377 static const int kDimensions = 1;
383 VecIn current_velocity = Vec(0.0f)) {
384 return Current1f(current_value, current_velocity);
388 VecIn target_value, VecIn target_velocity, MotiveTime target_time,
389 motive::ModularDirection direction = motive::kDirectionClosest) {
390 return Target1f(target_value, target_velocity, target_time, direction);
394 VecIn current_value, VecIn current_velocity, VecIn target_value,
395 VecIn target_velocity, MotiveTime target_time,
396 motive::ModularDirection direction = motive::kDirectionClosest) {
397 return CurrentToTarget1f(current_value, current_velocity, target_value,
398 target_velocity, target_time, direction);
403 MotiveTime target_time) {
404 return CurrentToTargetConstVelocity1f(current_value, target_value,
426 #endif // MOTIVE_TARGET_H
motive::Range ValueRange(float start_value) const
Definition: target.h:147
ModularDirection direction
When using modular arithmetic, which of two directions to go.
Definition: target.h:69
Utility functions to construct MotiveTargets of dimension >= 2.
Definition: target.h:297
MotiveTime time
Time to achieve this key point.
Definition: target.h:66
MotiveTarget1f(const MotiveNode1f &n0, const MotiveNode1f &n1, const MotiveNode1f &n2)
Definition: target.h:125
float typical_delta_value
The typical difference between the start and end values.
Definition: target.h:39
static MotiveTarget Current(VecIn current_value, VecIn current_velocity=Vec(0.0f))
Definition: target.h:308
const MotiveNode1f & Node(int node_index) const
Definition: target.h:139
static MotiveTarget Target(VecIn target_value, VecIn target_velocity, MotiveTime target_time, motive::ModularDirection direction=motive::kDirectionClosest)
Definition: target.h:323
float bias
Definition: target.h:52
float velocity
Speed when at time.
Definition: target.h:63
MotiveTime EndTime() const
Return time of the last waypoint.
Definition: target.h:159
A target curve shape for the motivator.
Definition: target.h:29
MotiveTarget1f(const MotiveNode1f &n0)
Definition: target.h:108
float value
Desired value to animate to at time.
Definition: target.h:60
float typical_total_time
The typical time it takes to go the typical distance.
Definition: target.h:42
N-dimensional MotiveTargets are simply arrays of one dimensional MotiveTargets.
Definition: target.h:180
MotiveTarget1f(const MotiveNode1f &n0, const MotiveNode1f &n1)
Definition: target.h:116
static MotiveTarget CurrentToTarget(VecIn current_value, VecIn current_velocity, VecIn target_value, VecIn target_velocity, MotiveTime target_time, motive::ModularDirection direction=motive::kDirectionClosest)
Set both the current and target values for an Motivator.
Definition: target.h:338
A waypoint in MotiveTarget1f. Describes one key point through which a value is animated.
Definition: target.h:58
static MotiveTarget CurrentToTargetConstVelocity(VecIn current_value, VecIn target_value, MotiveTime target_time)
Move from the current value to the target value at a constant speed.
Definition: target.h:357
void Reset()
Empty the target of all waypoints.
Definition: target.h:135
Set the current and/or target state for a one-dimensional Motivator.
Definition: target.h:98
Represent an interval on a number line.
Definition: range.h:37