@@ -84,6 +84,136 @@ game.createClass('Circle', {
84
84
}
85
85
} ) ;
86
86
87
+ /**
88
+ @class Curve
89
+ @constructor
90
+ @param {Number } sx
91
+ @param {Number } sy
92
+ @param {Number } ex
93
+ @param {Number } ey
94
+ @param {Number } h1x
95
+ @param {Number } h1y
96
+ @param {Number } h2x
97
+ @param {Number } h2y
98
+ **/
99
+ game . createClass ( 'Curve' , {
100
+ /**
101
+ End position of curve.
102
+ @property {Vector } end
103
+ **/
104
+ end : null ,
105
+ /**
106
+ Position of first control point.
107
+ @property {Vector } handle1
108
+ **/
109
+ handle1 : null ,
110
+ /**
111
+ Position of second control point.
112
+ @property {Vector } handle2
113
+ **/
114
+ handle2 : null ,
115
+ /**
116
+ Start position of curve.
117
+ @property {Vector } start
118
+ **/
119
+ start : null ,
120
+
121
+ staticInit : function ( sx , sy , ex , ey , h1x , h1y , h2x , h2y ) {
122
+ this . start = new game . Vector ( sx , sy ) ;
123
+ if ( typeof ex !== 'number' ) ex = sx ;
124
+ if ( typeof ey !== 'number' ) ey = sy ;
125
+ this . end = new game . Vector ( ex , ey ) ;
126
+ if ( typeof h1x !== 'number' ) h1x = sx ;
127
+ if ( typeof h1y !== 'number' ) h1y = sy ;
128
+ if ( typeof h2x !== 'number' ) h2x = ex ;
129
+ if ( typeof h2y !== 'number' ) h2y = ey ;
130
+ this . handle1 = new game . Vector ( h1x , h1y ) ;
131
+ this . handle2 = new game . Vector ( h2x , h2y ) ;
132
+ } ,
133
+
134
+ /**
135
+ Get point from curve.
136
+ @method point
137
+ @param {Number } percent Location of the point. 0 is start and 1 is the end of the curve.
138
+ @param {Vector } [out] Optional vector, where the values are set.
139
+ @return {Vector }
140
+ **/
141
+ point : function ( percent , out ) {
142
+ out = out || new game . Vector ( ) ;
143
+
144
+ var x = this . _interpolate ( percent , this . start . x , this . handle1 . x , this . handle2 . x , this . end . x ) ;
145
+ var y = this . _interpolate ( percent , this . start . y , this . handle1 . y , this . handle2 . y , this . end . y ) ;
146
+
147
+ out . set ( x , y ) ;
148
+ return out ;
149
+ } ,
150
+
151
+ /**
152
+ @method _calcHandle1
153
+ @param {Number } t
154
+ @param {Number } p
155
+ @return {Number }
156
+ @private
157
+ **/
158
+ _calcHandle1 : function ( t , p ) {
159
+ var k = 1 - t ;
160
+ return 3 * k * k * t * p ;
161
+ } ,
162
+
163
+ /**
164
+ @method _calcHandle2
165
+ @param {Number } t
166
+ @param {Number } p
167
+ @return {Number }
168
+ @private
169
+ **/
170
+ _calcHandle2 : function ( t , p ) {
171
+ return 3 * ( 1 - t ) * t * t * p ;
172
+ } ,
173
+
174
+ /**
175
+ @method _calcEnd
176
+ @param {Number } t
177
+ @param {Number } p
178
+ @return {Number }
179
+ @private
180
+ **/
181
+ _calcEnd : function ( t , p ) {
182
+ return t * t * t * p ;
183
+ } ,
184
+
185
+ /**
186
+ @method _calcStart
187
+ @param {Number } t
188
+ @param {Number } p
189
+ @return {Number }
190
+ @private
191
+ **/
192
+ _calcStart : function ( t , p ) {
193
+ var k = 1 - t ;
194
+ return k * k * k * p ;
195
+ } ,
196
+
197
+ /**
198
+ Get point from curve.
199
+ @method _interpolate
200
+ @param {Number } percent
201
+ @param {Number } s
202
+ @param {Number } h1
203
+ @param {Number } h2
204
+ @param {Number } e
205
+ @return {Number }
206
+ @private
207
+ **/
208
+ _interpolate : function ( percent , s , h1 , h2 , e ) {
209
+ s = this . _calcStart ( percent , s ) ;
210
+ h1 = this . _calcHandle1 ( percent , h1 ) ;
211
+ h2 = this . _calcHandle2 ( percent , h2 ) ;
212
+ e = this . _calcEnd ( percent , e ) ;
213
+ return s + h1 + h2 + e ;
214
+ }
215
+ } ) ;
216
+
87
217
/**
88
218
@class Polygon
89
219
@constructor
0 commit comments