• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package aurelienribon.tweenengine.equations;
2 
3 import aurelienribon.tweenengine.TweenEquation;
4 
5 /**
6  * Easing equation based on Robert Penner's work:
7  * http://robertpenner.com/easing/
8  * @author Aurelien Ribon | http://www.aurelienribon.com/
9  */
10 public abstract class Elastic extends TweenEquation {
11 	private static final float PI = 3.14159265f;
12 
13 	public static final Elastic IN = new Elastic() {
14 		@Override
15 		public final float compute(float t) {
16 			float a = param_a;
17 			float p = param_p;
18 			if (t==0) return 0;  if (t==1) return 1; if (!setP) p=.3f;
19 			float s;
20 			if (!setA || a < 1) { a=1; s=p/4; }
21 			else s = p/(2*PI) * (float)Math.asin(1/a);
22 			return -(a*(float)Math.pow(2,10*(t-=1)) * (float)Math.sin( (t-s)*(2*PI)/p ));
23 		}
24 
25 		@Override
26 		public String toString() {
27 			return "Elastic.IN";
28 		}
29 	};
30 
31 	public static final Elastic OUT = new Elastic() {
32 		@Override
33 		public final float compute(float t) {
34 			float a = param_a;
35 			float p = param_p;
36 			if (t==0) return 0;  if (t==1) return 1; if (!setP) p=.3f;
37 			float s;
38 			if (!setA || a < 1) { a=1; s=p/4; }
39 			else s = p/(2*PI) * (float)Math.asin(1/a);
40 			return a*(float)Math.pow(2,-10*t) * (float)Math.sin( (t-s)*(2*PI)/p ) + 1;
41 		}
42 
43 		@Override
44 		public String toString() {
45 			return "Elastic.OUT";
46 		}
47 	};
48 
49 	public static final Elastic INOUT = new Elastic() {
50 		@Override
51 		public final float compute(float t) {
52 			float a = param_a;
53 			float p = param_p;
54 			if (t==0) return 0;  if ((t*=2)==2) return 1; if (!setP) p=.3f*1.5f;
55 			float s;
56 			if (!setA || a < 1) { a=1; s=p/4; }
57 			else s = p/(2*PI) * (float)Math.asin(1/a);
58 			if (t < 1) return -.5f*(a*(float)Math.pow(2,10*(t-=1)) * (float)Math.sin( (t-s)*(2*PI)/p ));
59 			return a*(float)Math.pow(2,-10*(t-=1)) * (float)Math.sin( (t-s)*(2*PI)/p )*.5f + 1;
60 		}
61 
62 		@Override
63 		public String toString() {
64 			return "Elastic.INOUT";
65 		}
66 	};
67 
68 	// -------------------------------------------------------------------------
69 
70 	protected float param_a;
71 	protected float param_p;
72 	protected boolean setA = false;
73 	protected boolean setP = false;
74 
a(float a)75 	public Elastic a(float a) {
76 		param_a = a;
77 		this.setA = true;
78 		return this;
79 	}
80 
p(float p)81 	public Elastic p(float p) {
82 		param_p = p;
83 		this.setP = true;
84 		return this;
85 	}
86 }