TurtleBrains  0.3.1
High quality, portable, C++ framework for rapid 2D game development.
tb_interpolation.h
1 
9 #ifndef _TurtleBrains_Interpolation_h_
10 #define _TurtleBrains_Interpolation_h_
11 
12 namespace TurtleBrains
13 {
14  namespace Math
15  {
16  namespace Interpolation // Tweening / Easing
17  {
18 
19  enum class InterpolationMode
20  {
21  Linear,
22  InSquared,
23  OutSquared,
24  InOutSquared,
25 
26  InCubic,
27  OutCubic,
28  InOutCubic,
29 
30  InQuartic,
31  OutQuartic,
32  InOutQuartic,
33 
34  InQuintic,
35  OutQuintic,
36  InOutQuintic,
37 
38  InExponential,
39  OutExponential,
40  InOutExponential,
41 
42  InSine,
43  OutSine,
44  InOutSine,
45 
46  //Circular,
47  //Back,
48  InElastic,
49  OutElastic,
50  InOutElastic,
51 
52  InBounce,
53  OutBounce,
54  InOutBounce,
55  };
56 
64  inline constexpr float Linear(float percentage)
65  {
66  return percentage;
67  }
68 
82  template <typename Type> constexpr Type Linear(float percentage, const Type& start, const Type& end)
83  {
84  return ((end - start) * percentage) + start;
85  }
86 
95  inline constexpr float Squared(float percentage)
96  {
97  return percentage * percentage;
98  }
99 
112  template <typename Type> Type Squared(float percentage, const Type& start, const Type& end)
113  {
114  return ((end - start) * Squared(percentage)) + start;
115  }
116 
123  inline constexpr float Cubic(float percentage)
124  {
125  return percentage * percentage * percentage;
126  }
127 
140  template <typename Type> Type Cubic(float percentage, const Type& start, const Type& end)
141  {
142  return ((end - start) * Cubic(percentage)) + start;
143  }
144 
151  inline constexpr float Quartic(float percentage)
152  {
153  return percentage * percentage * percentage * percentage;
154  }
155 
165  template <typename Type> Type Quartic(float percentage, const Type& start, const Type& end)
166  {
167  return ((end - start) * Quartic(percentage)) + start;
168  }
169 
176  inline constexpr float Quintic(float percentage)
177  {
178  return percentage * percentage * percentage * percentage * percentage;
179  }
180 
190  template <typename Type> Type Quintic(float percentage, const Type& start, const Type& end)
191  {
192  return ((end - start) * Quintic(percentage)) + start;
193  }
194 
201  inline float Exponential(float percentage)
202  {
203  return pow(2.0f, 10.0f * (percentage - 1.0f));
204  }
205 
215  template <typename Type> Type Exponential(float percentage, const Type& start, const Type& end)
216  {
217  return ((end - start) * Exponential(percentage)) + start;
218  }
219 
226  inline float Sine(float percentage)
227  { //Sin in from http://gizma.com/easing/#sin1 where t/d = percentage, c = 1, b = 0 (time, begin, change, distance)
228  return 1.0f - cos(percentage * tbMath::kPi / 2.0f);
229  }
230 
243  template <typename Type> Type Sine(float percentage, const Type& start, const Type& end)
244  {
245  return ((end - start) * Sine(percentage)) + start;
246  }
247 
256  inline float Elastic(float percentage)
257  { //https://github.com/vrld/hump/blob/master/timer.lua#L128L131
258  const float amplitude = 1.0f;
259  const float period = 0.3f;
260  return (-amplitude * sin(tbMath::kTwoPi / period * (percentage - 1.0f) - asin(1.0f / amplitude))) * pow(2.0f, (10.0f * (percentage - 1.0f)));
261  }
262 
263  inline float Elastic(float percentage, float amplitude, float period)
264  { //https://github.com/vrld/hump/blob/master/timer.lua#L128L131
265  return (-amplitude * sin(tbMath::kTwoPi / period * (percentage - 1.0f) - asin(1.0f / amplitude))) * pow(2.0f, (10.0f * (percentage - 1.0f)));
266  }
267 
279  template <typename Type> Type Elastic(float percentage, const Type& start, const Type& end)
280  {
281  return ((end - start) * Elastic(percentage)) + start;
282  }
283 
290  inline float Bounce(const float percentage)
291  { //https://github.com/vrld/hump/blob/master/timer.lua#L123-L126
292  const float a(7.5625f);
293  const float b(1.0f / 2.75f);
294 
295  return tbMath::Minimum(a * percentage * percentage,
296  tbMath::Minimum(a * (percentage - 1.5f * b) * (percentage - 1.5f * b) + 0.75f,
297  tbMath::Minimum(a * (percentage - 2.25f * b) * (percentage - 2.25f * b) + 0.9375f,
298  a * (percentage - 2.625f * b) * (percentage - 2.625f * b) + 0.984375f)));
299  }
300 
313  template <typename Type> Type Bounce(float percentage, const Type& start, const Type& end)
314  {
315  return ((end - start) * Bounce(percentage)) + start;
316  }
317 
318 
319  typedef float(*InterpolationFunction)(float);
320 
321  inline float Out(float percentage, InterpolationFunction interpolation)
322  {
323  return 1.0f - interpolation(1.0f - percentage);
324  }
325 
326  template <typename Type> float Out(float percentage, const Type& start,
327  const Type& end, InterpolationFunction interpolation)
328  {
329  return ((end - start) * Out(percentage, interpolation)) + start;
330  }
331 
332 
333  inline float InOut(float percentage, InterpolationFunction interpolation)
334  {
335  return 0.5f * ((percentage < 0.5f) ? interpolation(2.0f * percentage) :
336  1.0f + Out(2.0f * percentage - 1.0f, interpolation));
337  }
338 
339  template <typename Type> float InOut(float percentage, const Type& start,
340  const Type& end, InterpolationFunction interpolation)
341  {
342  return ((end - start) * InOut(percentage, interpolation)) + start;
343  }
344 
345  inline float Interpolate(float percentage, const InterpolationMode& mode)
346  {
347  switch (mode)
348  {
349  case InterpolationMode::Linear: return Linear(percentage);
350  case InterpolationMode::InSquared: return Squared(percentage);
351  case InterpolationMode::OutSquared: return Out(percentage, Squared);
352  case InterpolationMode::InOutSquared: return InOut(percentage, &Squared);
353 
354  case InterpolationMode::InCubic: return Cubic(percentage);
355  case InterpolationMode::OutCubic: return Out(percentage, Cubic);
356  case InterpolationMode::InOutCubic: return InOut(percentage, &Cubic);
357 
358  case InterpolationMode::InQuartic: return Quartic(percentage);
359  case InterpolationMode::OutQuartic: return Out(percentage, Quartic);
360  case InterpolationMode::InOutQuartic: return InOut(percentage, &Quartic);
361 
362  case InterpolationMode::InQuintic: return Quintic(percentage);
363  case InterpolationMode::OutQuintic: return Out(percentage, Quintic);
364  case InterpolationMode::InOutQuintic: return InOut(percentage, &Quintic);
365 
366  case InterpolationMode::InExponential: return Exponential(percentage);
367  case InterpolationMode::OutExponential: return Out(percentage, Exponential);
368  case InterpolationMode::InOutExponential: return InOut(percentage, &Exponential);
369 
370  case InterpolationMode::InSine: return Sine(percentage);
371  case InterpolationMode::OutSine: return Out(percentage, Sine);
372  case InterpolationMode::InOutSine: return InOut(percentage, &Sine);
373 
374  case InterpolationMode::InElastic: return Elastic(percentage);
375  case InterpolationMode::OutElastic: return Out(percentage, Elastic);
376  case InterpolationMode::InOutElastic: return InOut(percentage, &Elastic);
377 
378  case InterpolationMode::InBounce: return Bounce(percentage);
379  case InterpolationMode::OutBounce: return Out(percentage, Bounce);
380  case InterpolationMode::InOutBounce: return InOut(percentage, &Bounce);
381  }
382 
383  tb_error("tbInternalError: Unhandled case for InterpolationMode: %d\n", mode);
384  return 0.0;
385  }
386 
387  template<typename Type> Type Interpolate(float percentage, const Type& start, const Type& end, const InterpolationMode& mode)
388  {
389  return ((end - start) * Interpolate(percentage, mode)) + start;
390  }
391 
392 
401  inline float SmoothStep(float percentage)
402  {
403  return (percentage * percentage * (3.0f - 2.0f * percentage));
404  }
405 
419  template <typename Type> Type SmoothStep(float percentage, const Type& start, const Type& end)
420  {
421  const float smoothPercentage = SmoothStep(percentage);
422  return (start * smoothPercentage) + (end * (1.0f - smoothPercentage));
423  }
424 
443  template <typename Type> Type CubicBezier(float percentage, const Type& a, const Type& b, const Type& c, const Type& d)
444  {
445  const Type tempA = Linear(percentage, a, c);
446  const Type tempB = Linear(percentage, c, d);
447  const Type tempC = Linear(percentage, d, b);
448 
449  const Type tempAA = Linear(percentage, tempA, tempB);
450  const Type tempBB = Linear(percentage, tempB, tempC);
451 
452  return Linear(percentage, tempAA, tempBB);
453  }
454 
469  template <typename Type> Type CubicBezierTangent(float percentage, const Type& a, const Type& b, const Type& c, const Type& d)
470  { //Source: http://stackoverflow.com/questions/4089443/find-the-tangent-of-a-point-on-a-cubic-bezier-curve-on-an-iphone
471 
472  const Type c1(d - (3.0f * c) + (3.0f * b) - a);
473  const Type c2((3.0f * c) - (6.0f * b) + (3.0f * a));
474  const Type c3((3.0f * b) - (3.0f * a));
475  //const float c4(a);
476 
477  return ((c1 * (3.0f * percentage * percentage)) + (c2 * (2.0f * percentage)) + c3);
478  }
479 
480  }; /* namespace Interpolation */
481  }; /* namespace Math */
482 }; /* namespace TurtleBrains */
483 
484 namespace tbMath = TurtleBrains::Math;
485 
486 #endif /* _TurtleBrains_Interpolation_h_ */
Contains objects and functions for dealing with Vector and Matrix math.
#define tb_error(message,...)
Definition: tb_error.h:23
Here is some information about the primary namespace.
Definition: tb_application_dialog.h:21
constexpr const T & Minimum(const T &leftValue, const T &rightValue) noexcept
Definition: tb_math.h:81