Merge 'remotes/trunk'
[0ad.git] / source / maths / Ease.h
blob5ba9546a15f40a7e065224a616178d610c08f44e
1 /* Copyright (C) 2012 Wildfire Games.
2 * This file is part of 0 A.D.
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
18 #ifndef INCLUDED_EASE
19 #define INCLUDED_EASE
22 * Straightforward C++ port of Robert Penner's easing equations
23 * http://www.robertpenner.com/easing/
25 * Copyright (c) 2001 Robert Penner
26 * All rights reserved.
28 * Redistribution and use in source and binary forms, with or without modification, are permitted
29 * provided that the following conditions are met:
30 * - Redistributions of source code must retain the above copyright notice, this list of
31 * conditions and the following disclaimer.
32 * - Redistributions in binary form must reproduce the above copyright notice, this list of
33 * conditions and the following disclaimer in the documentation and/or other materials provided
34 * with the distribution.
35 * - Neither the name of the author nor the names of contributors may be used to endorse or
36 * promote products derived from this software without specific prior written permission.
38 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
39 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
40 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
41 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
42 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
43 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
44 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48 #include <math.h>
50 /**
51 * Generic easing functions. In each function, the parameters are:
53 * @param t Current time in seconds, as a float between 0 and d (inclusive).
54 * @param d Total duration of the ease, in seconds. Must be strictly positive.
55 * @param b Baseline value (at t = 0).
56 * @param c Delta from baseline value to reach the target value (at t = d). I.e., target = b + c.
58 * Each function outputs the eased value between 'b' and 'b+c' at time 't'.
60 class Ease
62 public:
63 static float QuadIn(float t, const float b, const float c, const float d)
65 t /= d;
66 return c*t*t + b;
69 static float QuadOut(float t, const float b, const float c, const float d)
71 t /= d;
72 return -c * t*(t-2) + b;
75 static float QuadInOut(float t, const float b, const float c, const float d)
77 t /= d/2;
78 if (t < 1)
79 return c/2*t*t + b;
80 --t;
81 return -c/2 * (t*(t-2) - 1) + b;
84 static float CubicIn(float t, const float b, const float c, const float d)
86 t /= d;
87 return c*t*t*t + b;
90 static float CubicOut(float t, const float b, const float c, const float d)
92 t = t/d - 1;
93 return c*(t*t*t + 1) + b;
96 static float CubicInOut(float t, const float b, const float c, const float d)
98 t /= d/2;
99 if (t < 1)
100 return c/2*t*t*t + b;
101 t -= 2;
102 return c/2*(t*t*t + 2) + b;
105 static float QuartIn(float t, const float b, const float c, const float d)
107 t /= d;
108 return c*t*t*t*t + b;
111 static float QuartOut(float t, const float b, const float c, const float d)
113 t = t/d - 1;
114 return -c*(t*t*t*t - 1) + b;
117 static float QuartInOut(float t, const float b, const float c, const float d)
119 t /= d/2;
120 if (t < 1)
121 return c/2*t*t*t*t + b;
122 t -= 2;
123 return -c/2 * (t*t*t*t - 2) + b;
126 static float QuintIn(float t, const float b, const float c, const float d)
128 t /= d;
129 return c*t*t*t*t*t + b;
132 static float QuintOut(float t, const float b, const float c, const float d)
134 t = t/d - 1;
135 return c*(t*t*t*t*t + 1) + b;
138 static float QuintInOut(float t, const float b, const float c, const float d)
140 t /= d/2;
141 if (t < 1)
142 return c/2*t*t*t*t*t + b;
143 t -= 2;
144 return c/2*(t*t*t*t*t + 2) + b;
148 #endif // INCLUDED_EASE