|
11
|
1 |
package com.eclecticdesignstudio.motion { |
|
|
2 |
|
|
|
3 |
|
|
|
4 |
import com.eclecticdesignstudio.motion.actuators.MotionInternal; |
|
|
5 |
|
|
|
6 |
use namespace MotionInternal; |
|
|
7 |
|
|
|
8 |
|
|
|
9 |
/** |
|
|
10 |
* @author Joshua Granick |
|
|
11 |
* @version 1.2 |
|
|
12 |
*/ |
|
|
13 |
public class MotionPath { |
|
|
14 |
|
|
|
15 |
|
|
|
16 |
public var start:Number; |
|
|
17 |
|
|
|
18 |
protected var paths:Array; |
|
|
19 |
protected var totalStrength:Number; |
|
|
20 |
|
|
|
21 |
|
|
|
22 |
public function MotionPath () { |
|
|
23 |
|
|
|
24 |
paths = new Array (); |
|
|
25 |
start = 0; |
|
|
26 |
totalStrength = 0; |
|
|
27 |
|
|
|
28 |
} |
|
|
29 |
|
|
|
30 |
|
|
|
31 |
protected function addPath (path:BezierPath):void { |
|
|
32 |
|
|
|
33 |
paths.push (path); |
|
|
34 |
totalStrength += path.strength; |
|
|
35 |
|
|
|
36 |
} |
|
|
37 |
|
|
|
38 |
|
|
|
39 |
/** |
|
|
40 |
* Creates a motion path using a bezier curve |
|
|
41 |
* @param end The position of the end point for the curve |
|
|
42 |
* @param control The position of the control point for the curve, which affects its angle and midpoint |
|
|
43 |
* @param strength The degree of emphasis that should be placed on this segment . If a motion path contains multiple segments with the same strength, they all receive equal emphasis (Default is 1) |
|
|
44 |
* @return The new motion path instance |
|
|
45 |
*/ |
|
|
46 |
public static function bezier (end:Number, control:Number, strength:Number = 1):MotionPath { |
|
|
47 |
|
|
|
48 |
return new MotionPath ().bezier (end, control, strength); |
|
|
49 |
|
|
|
50 |
} |
|
|
51 |
|
|
|
52 |
|
|
|
53 |
/** |
|
|
54 |
* Adds a bezier curve to the current motion path |
|
|
55 |
* @param end The position of the end point for the curve |
|
|
56 |
* @param control The position of the control point for the curve, which affects its angle and midpoint |
|
|
57 |
* @param strength The degree of emphasis that should be placed on this segment . If a motion path contains multiple segments with the same strength, they all receive equal emphasis (Default is 1) |
|
|
58 |
* @return The current motion path instance |
|
|
59 |
*/ |
|
|
60 |
public function bezier (end:Number, control:Number, strength:Number = 1):MotionPath { |
|
|
61 |
|
|
|
62 |
addPath (new BezierPath (end, control, strength)); |
|
|
63 |
|
|
|
64 |
return this; |
|
|
65 |
|
|
|
66 |
} |
|
|
67 |
|
|
|
68 |
|
|
|
69 |
MotionInternal function calculate (k:Number):Number { |
|
|
70 |
|
|
|
71 |
if (paths.length === 1) { |
|
|
72 |
|
|
|
73 |
return (paths[0] as Object).calculate (start, k); |
|
|
74 |
|
|
|
75 |
} else { |
|
|
76 |
|
|
|
77 |
var ratio:Number = k * totalStrength; |
|
|
78 |
var lastEnd:Number = start; |
|
|
79 |
|
|
|
80 |
for each (var path:BezierPath in paths) { |
|
|
81 |
|
|
|
82 |
if (ratio > path.strength) { |
|
|
83 |
|
|
|
84 |
ratio -= path.strength; |
|
|
85 |
lastEnd = path.end; |
|
|
86 |
|
|
|
87 |
} else { |
|
|
88 |
|
|
|
89 |
return path.calculate (lastEnd, ratio / path.strength); |
|
|
90 |
|
|
|
91 |
} |
|
|
92 |
|
|
|
93 |
} |
|
|
94 |
|
|
|
95 |
} |
|
|
96 |
|
|
|
97 |
return 0; |
|
|
98 |
|
|
|
99 |
} |
|
|
100 |
|
|
|
101 |
|
|
|
102 |
/** |
|
|
103 |
* Creates a motion path using a line |
|
|
104 |
* @param end The position of the end point for the line |
|
|
105 |
* @param strength The degree of emphasis that should be placed on this segment . If a motion path contains multiple segments with the same strength, they all receive equal emphasis (Default is 1) |
|
|
106 |
* @return The new motion path instance |
|
|
107 |
*/ |
|
|
108 |
public static function line (end:Number, strength:Number = 1):MotionPath { |
|
|
109 |
|
|
|
110 |
return new MotionPath ().line (end, strength); |
|
|
111 |
|
|
|
112 |
} |
|
|
113 |
|
|
|
114 |
|
|
|
115 |
/** |
|
|
116 |
* Adds a line to the current motion path |
|
|
117 |
* @param end The position of the end point for the line |
|
|
118 |
* @param strength The degree of emphasis that should be placed on this segment . If a motion path contains multiple segments with the same strength, they all receive equal emphasis (Default is 1) |
|
|
119 |
* @return The current motion path instance |
|
|
120 |
*/ |
|
|
121 |
public function line (end:Number, strength:Number = 1):MotionPath { |
|
|
122 |
|
|
|
123 |
addPath (new LinearPath (end, strength)); |
|
|
124 |
|
|
|
125 |
return this; |
|
|
126 |
|
|
|
127 |
} |
|
|
128 |
|
|
|
129 |
|
|
|
130 |
|
|
|
131 |
|
|
|
132 |
// Get & Set Methods |
|
|
133 |
|
|
|
134 |
|
|
|
135 |
|
|
|
136 |
|
|
|
137 |
MotionInternal function get end ():Number { |
|
|
138 |
|
|
|
139 |
if (paths.length > 0) { |
|
|
140 |
|
|
|
141 |
var path:BezierPath = paths[paths.length - 1]; |
|
|
142 |
return path.end; |
|
|
143 |
|
|
|
144 |
} else { |
|
|
145 |
|
|
|
146 |
return NaN; |
|
|
147 |
|
|
|
148 |
} |
|
|
149 |
|
|
|
150 |
} |
|
|
151 |
|
|
|
152 |
} |
|
|
153 |
|
|
|
154 |
|
|
|
155 |
} |
|
|
156 |
|
|
|
157 |
|
|
|
158 |
|
|
|
159 |
|
|
|
160 |
class BezierPath { |
|
|
161 |
|
|
|
162 |
|
|
|
163 |
public var control:Number; |
|
|
164 |
public var end:Number; |
|
|
165 |
public var strength:Number; |
|
|
166 |
|
|
|
167 |
|
|
|
168 |
public function BezierPath (end:Number, control:Number, strength:Number) { |
|
|
169 |
|
|
|
170 |
this.end = end; |
|
|
171 |
this.control = control; |
|
|
172 |
this.strength = strength; |
|
|
173 |
|
|
|
174 |
} |
|
|
175 |
|
|
|
176 |
|
|
|
177 |
public function calculate (start:Number, k:Number):Number { |
|
|
178 |
|
|
|
179 |
return (1 - k) * (1 - k) * start + 2 * (1 - k) * k * control + k * k * end; |
|
|
180 |
|
|
|
181 |
} |
|
|
182 |
|
|
|
183 |
|
|
|
184 |
} |
|
|
185 |
|
|
|
186 |
|
|
|
187 |
class LinearPath extends BezierPath { |
|
|
188 |
|
|
|
189 |
|
|
|
190 |
public function LinearPath (end:Number, strength:Number) { |
|
|
191 |
|
|
|
192 |
super (end, 0, strength); |
|
|
193 |
|
|
|
194 |
} |
|
|
195 |
|
|
|
196 |
|
|
|
197 |
public override function calculate (start:Number, k:Number):Number { |
|
|
198 |
|
|
|
199 |
return start + k * (end - start); |
|
|
200 |
|
|
|
201 |
} |
|
|
202 |
|
|
|
203 |
|
|
|
204 |
} |