@@ -19,26 +19,38 @@ window.onload = function() {
19
19
new Position ( 60 , 60 ) , new Position ( 60 , 260 )
20
20
)
21
21
) ;
22
- myWorld . path . next = new Line (
23
- new Position ( 60 , 260 ) , new Position ( 160 , 360 )
22
+ var path = myWorld . path ;
23
+ path . next = new Circle ( new Position ( 160 , 260 ) , 100 , Math . PI , Math . PI / 2 ) ;
24
+ path . next . previous = path ;
25
+ path = path . next ;
26
+ path . next = new Line (
27
+ new Position ( 160 , 360 ) , new Position ( 360 , 360 )
24
28
) ;
25
- myWorld . path . next . previous = myWorld . path ;
29
+ path . next . previous = path ;
30
+ path = path . next ;
31
+ path . next = new Circle ( new Position ( 560 , 160 ) , 200 , Math . PI / 2 , Math . PI / 2 ) ;
32
+ path . next . previous = path ;
33
+ /*
34
+ path.next = new Circle(new Position(360, 260), 300, Math.PI, Math.PI);
35
+ path.next.previous = path;
36
+ */
37
+
26
38
myWorld . tokens = [ new Token ( 50 , myWorld . path , 0 , 0.1 ) ] ;
27
39
28
40
printPaths ( myWorld , rcontext ) ; // We just have to do it once
29
41
printTokens ( myWorld , tcontext ) ;
30
42
31
- setInterval ( function ( ) {
32
- updateVelocity ( myWorld . tokens [ 0 ] , myWorld , 1 / 60 ) ;
33
- updatePosition ( myWorld . tokens [ 0 ] , 1 / 60 ) ;
34
- printTokens ( myWorld , tcontext ) ;
35
- } , 1000 / 60 ) ;
36
-
43
+ var play = false ;
44
+ var interval ;
37
45
document . getElementById ( 'tokens' ) . onclick = function ( ) {
38
- updateVelocity ( myWorld . tokens [ 0 ] , myWorld , 1 ) ;
39
- updatePosition ( myWorld . tokens [ 0 ] , 1 ) ;
40
- printTokens ( myWorld , tcontext ) ;
41
- console . log ( myWorld . tokens [ 0 ] . velocity ) ;
46
+ if ( play = ! play ) {
47
+ clearInterval ( interval ) ;
48
+ } else {
49
+ interval = setInterval ( function ( ) {
50
+ updateTokens ( myWorld , 1 / 60 ) ;
51
+ printTokens ( myWorld , tcontext ) ;
52
+ } , 1000 / 60 ) ;
53
+ }
42
54
}
43
55
} ;
44
56
@@ -69,11 +81,12 @@ function Line(start, end) {
69
81
this . length = Math . sqrt ( distx * distx + disty * disty ) ;
70
82
}
71
83
72
- function Circle ( center , radius , start , end ) {
84
+ function Circle ( center , radius , start , angle ) {
73
85
this . center = center ;
74
86
this . radius = radius ;
75
87
this . start = start ;
76
- this . end = end ;
88
+ this . angle = angle ;
89
+ this . length = Math . abs ( radius * angle ) ;
77
90
}
78
91
79
92
function Token ( size , path , progression , friction ) {
@@ -85,10 +98,16 @@ function Token(size, path, progression, friction) {
85
98
}
86
99
87
100
function updateVelocity ( token , world , time ) {
88
- var pathDir = Math . atan (
89
- ( token . path . end . y - token . path . start . y ) /
90
- ( token . path . end . x - token . path . start . x )
91
- ) ;
101
+ var pathDir ;
102
+ if ( token . path instanceof Line ) {
103
+ pathDir = Math . atan (
104
+ ( token . path . end . y - token . path . start . y ) /
105
+ ( token . path . end . x - token . path . start . x )
106
+ ) ;
107
+ } else if ( token . path instanceof Circle ) {
108
+ pathDir = - ( token . progression / token . path . radius - token . path . start + Math . PI / 2 ) % Math . PI ;
109
+ }
110
+ console . log ( pathDir ) ;
92
111
token . velocity += time * world . force . value * Math . cos ( world . force . direction - pathDir ) ;
93
112
token . velocity *= ( 1 - time * token . friction ) ;
94
113
}
@@ -118,15 +137,25 @@ function updatePosition(token, time) {
118
137
}
119
138
}
120
139
140
+ function updateTokens ( world , time ) {
141
+ for ( var i = 0 ; i < world . tokens . length ; i ++ ) {
142
+ updateVelocity ( world . tokens [ i ] , world , time ) ;
143
+ updatePosition ( world . tokens [ i ] , time ) ;
144
+ }
145
+ }
121
146
function position ( token ) {
122
147
if ( token . path instanceof Line ) {
123
148
return new Position (
124
149
token . path . start . x +
125
150
( token . path . end . x - token . path . start . x ) * token . progression / token . path . length ,
126
151
token . path . start . y +
127
- ( token . path . end . y - token . path . start . y ) * token . progression / token . path . length ) ;
128
- } else {
129
- return 0 ;
152
+ ( token . path . end . y - token . path . start . y ) * token . progression / token . path . length
153
+ ) ;
154
+ } else if ( token . path instanceof Circle ) {
155
+ return new Position (
156
+ token . path . center . x + token . path . radius * Math . cos ( token . path . start - ( token . progression / token . path . length ) * token . path . angle ) ,
157
+ token . path . center . y + token . path . radius * Math . sin ( token . path . start - ( token . progression / token . path . length ) * token . path . angle )
158
+ ) ;
130
159
}
131
160
}
132
161
@@ -140,10 +169,14 @@ function printPaths(world, context) {
140
169
for ( p = world . path ; hasNext ( p ) ; p = p . next ) {
141
170
if ( p instanceof Line ) {
142
171
printLine ( p , context ) ;
172
+ } else if ( p instanceof Circle ) {
173
+ printCircle ( p , context ) ;
143
174
}
144
175
}
145
176
if ( p instanceof Line ) {
146
177
printLine ( p , context ) ;
178
+ } else if ( p instanceof Circle ) {
179
+ printCircle ( p , context ) ;
147
180
}
148
181
}
149
182
@@ -154,6 +187,16 @@ function printLine(line, context) {
154
187
console . log ( 'Line : ' + line . length ) ;
155
188
}
156
189
190
+ function printCircle ( circle , context ) {
191
+ context . beginPath ( ) ;
192
+ if ( circle . angle >= 0 ) {
193
+ context . arc ( circle . center . x , circle . center . y , circle . radius , circle . start - circle . angle , circle . start ) ;
194
+ } else {
195
+ context . arc ( circle . center . x , circle . center . y , circle . radius , circle . start , circle . start - circle . angle ) ;
196
+ }
197
+ context . stroke ( ) ;
198
+ }
199
+
157
200
function printTokens ( world , context ) {
158
201
context . clearRect ( 0 , 0 , 1000 , 1000 ) ;
159
202
for ( var i = 0 ; i < world . tokens . length ; i ++ ) {
0 commit comments