2 USING: kernel combinators sequences arrays math math.vectors
3 generalizations vars accessors math.physics.vel ;
7 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
9 : scalar-projection ( a b -- n ) [ v. ] [ nip norm ] 2bi / ;
11 : vector-projection ( a b -- vec )
12 [ nip normalize ] [ scalar-projection ] 2bi v*n ;
14 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
21 : world-width ( -- width ) world-size> first ;
23 : world-height ( -- height ) world-size> second ;
27 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
29 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
31 TUPLE: node < vel mass elas force ;
35 : node-vel ( node -- vel ) vel>> ;
37 : set-node-vel ( vel node -- ) swap >>vel drop ;
39 : pos-x ( node -- x ) pos>> first ;
40 : pos-y ( node -- y ) pos>> second ;
41 : vel-x ( node -- y ) vel>> first ;
42 : vel-y ( node -- y ) vel>> second ;
44 : >>pos-x ( node x -- node ) over pos>> set-first ;
45 : >>pos-y ( node y -- node ) over pos>> set-second ;
46 : >>vel-x ( node x -- node ) over vel>> set-first ;
47 : >>vel-y ( node y -- node ) over vel>> set-second ;
49 : apply-force ( node vec -- ) over force>> v+ >>force drop ;
51 : reset-force ( node -- node ) 0 0 2array >>force ;
53 : node-id ( id -- node ) 1- nodes> nth ;
55 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
57 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
59 TUPLE: spring rest-length k damp node-a node-b ;
63 : end-points ( spring -- b-pos a-pos )
64 [ node-b>> pos>> ] [ node-a>> pos>> ] bi ;
66 : spring-length ( spring -- length ) end-points v- norm ;
68 : stretch-length ( spring -- length )
69 [ spring-length ] [ rest-length>> ] bi - ;
71 : dir ( spring -- vec ) end-points v- normalize ;
73 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
75 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
79 ! k :: spring constant
80 ! x :: distance stretched beyond rest length
82 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
84 : hooke-force-mag ( spring -- mag ) [ k>> ] [ stretch-length ] bi * ;
86 : hooke-force ( spring -- force ) [ dir ] [ hooke-force-mag ] bi v*n ;
88 : hooke-forces ( spring -- a b ) hooke-force dup vneg ;
90 : act-on-nodes-hooke ( spring -- )
91 [ node-a>> ] [ node-b>> ] [ ] tri hooke-forces swapd
95 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
97 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
101 ! b :: Damping constant
104 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
106 ! : damping-force-a ( spring -- vec )
107 ! [ spring-node-a node-vel ] [ spring-damp ] bi v*n vneg ;
109 ! : damping-force-b ( spring -- vec )
110 ! [ spring-node-b node-vel ] [ spring-damp ] bi v*n vneg ;
112 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
114 : relative-velocity-a ( spring -- vel )
115 [ node-a>> vel>> ] [ node-b>> vel>> ] bi v- ;
117 : unit-vec-b->a ( spring -- vec )
118 [ node-a>> pos>> ] [ node-b>> pos>> ] bi v- ;
120 : relative-velocity-along-spring-a ( spring -- vel )
121 [ relative-velocity-a ] [ unit-vec-b->a ] bi vector-projection ;
123 : damping-force-a ( spring -- vec )
124 [ relative-velocity-along-spring-a ] [ damp>> ] bi v*n vneg ;
126 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
128 : relative-velocity-b ( spring -- vel )
129 [ node-b>> vel>> ] [ node-a>> vel>> ] bi v- ;
131 : unit-vec-a->b ( spring -- vec )
132 [ node-b>> pos>> ] [ node-a>> pos>> ] bi v- ;
134 : relative-velocity-along-spring-b ( spring -- vel )
135 [ relative-velocity-b ] [ unit-vec-a->b ] bi vector-projection ;
137 : damping-force-b ( spring -- vec )
138 [ relative-velocity-along-spring-b ] [ damp>> ] bi v*n vneg ;
140 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
142 : act-on-nodes-damping ( spring -- )
144 [ node-a>> ] [ damping-force-a ] bi apply-force
145 [ node-b>> ] [ damping-force-b ] bi apply-force ;
147 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
149 : below? ( node -- ? ) pos-y 0 < ;
151 : above? ( node -- ? ) pos-y world-height >= ;
153 : beyond-left? ( node -- ? ) pos-x 0 < ;
155 : beyond-right? ( node -- ? ) pos-x world-width >= ;
157 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
159 : bounce-top ( node -- )
160 world-height 1- >>pos-y
161 dup [ vel-y ] [ elas>> ] bi * neg >>vel-y
164 : bounce-bottom ( node -- )
166 dup [ vel-y ] [ elas>> ] bi * neg >>vel-y
169 : bounce-left ( node -- )
171 dup [ vel-x ] [ elas>> ] bi * neg >>vel-x
174 : bounce-right ( node -- )
175 world-width 1- >>pos-x
176 dup [ vel-x ] [ elas>> ] bi * neg >>vel-x
179 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
181 : handle-bounce ( node -- )
182 { { [ dup above? ] [ bounce-top ] }
183 { [ dup below? ] [ bounce-bottom ] }
184 { [ dup beyond-left? ] [ bounce-left ] }
185 { [ dup beyond-right? ] [ bounce-right ] }
189 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
191 : act-on-nodes ( spring -- )
194 act-on-nodes-damping ;
196 ! : act-on-nodes ( spring -- ) act-on-nodes-hooke ;
198 : loop-over-springs ( -- ) springs> [ act-on-nodes ] each ;
200 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
202 : apply-gravity ( node -- ) { 0 -9.8 } apply-force ;
204 : do-gravity ( -- ) gravity> [ nodes> [ apply-gravity ] each ] when ;
206 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
210 : calc-acceleration ( node -- vec ) [ force>> ] [ mass>> ] bi v/n ;
212 : new-vel ( node -- vel )
213 [ vel>> ] [ calc-acceleration time-slice> v*n ] bi v+ ;
215 : new-pos ( node -- pos ) [ pos>> ] [ vel>> time-slice> v*n ] bi v+ ;
217 : iterate-node ( node -- )
223 : iterate-nodes ( -- ) nodes> [ iterate-node ] each ;
225 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
227 : iterate-system ( -- ) do-gravity loop-over-springs iterate-nodes ;
229 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
230 ! Reading xspringies data files
231 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
233 : mass ( id x y x-vel y-vel mass elas -- )
240 nodes> swap suffix >nodes
243 : spng ( id id-a id-b k damp rest-length -- )
248 swap node-id >>node-b
249 swap node-id >>node-a
250 springs> swap suffix >springs