1 ! Copyright (C) 2007, 2008 Alex Chapman
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays colors combinators fry jamshred.oint
4 kernel literals locals math math.constants math.matrices
5 math.order math.quadratic math.ranges math.vectors random
6 sequences specialized-arrays vectors ;
7 FROM: jamshred.oint => distance ;
8 FROM: alien.c-types => float ;
9 SPECIALIZED-ARRAY: float
12 CONSTANT: n-segments 5000
14 TUPLE: segment < oint number color radius ;
17 : segment-number++ ( segment -- )
18 [ number>> 1 + ] keep number<< ;
20 : clamp-length ( n seq -- n' )
23 : random-color ( -- color )
24 { 100 100 100 } [ random 100 / >float ] map first3 1.0 <rgba> ;
26 CONSTANT: tunnel-segment-distance 0.4
27 CONSTANT: random-rotation-angle $[ pi 20 / ]
29 : random-segment ( previous-segment -- segment )
30 clone dup random-rotation-angle random-turn
31 tunnel-segment-distance over go-forward
32 random-color >>color dup segment-number++ ;
34 : (random-segments) ( segments n -- segments )
35 [ dup last random-segment suffix! ] times ;
37 CONSTANT: default-segment-radius 1
39 : initial-segment ( -- segment )
40 float-array{ 0 0 0 } float-array{ 0 0 -1 } float-array{ 0 1 0 } float-array{ -1 0 0 }
41 0 random-color default-segment-radius <segment> ;
43 : random-segments ( n -- segments )
44 initial-segment 1vector swap (random-segments) ;
46 : simple-segment ( n -- segment )
47 [ float-array{ 0 0 -1 } n*v float-array{ 0 0 -1 } float-array{ 0 1 0 } float-array{ -1 0 0 } ] keep
48 random-color default-segment-radius <segment> ;
50 : simple-segments ( n -- segments )
51 [ simple-segment ] map ;
53 : <random-tunnel> ( -- segments )
54 n-segments random-segments ;
56 : <straight-tunnel> ( -- segments )
57 n-segments simple-segments ;
59 : sub-tunnel ( from to segments -- segments )
60 #! return segments between from and to, after clamping from and to to
62 [ '[ _ clamp-length ] bi@ ] keep <slice> ;
64 : get-segment ( segments n -- segment )
65 over clamp-length swap nth ;
67 : next-segment ( segments current-segment -- segment )
68 number>> 1 + get-segment ;
70 : previous-segment ( segments current-segment -- segment )
71 number>> 1 - get-segment ;
73 : heading-segment ( segments current-segment heading -- segment )
74 #! the next segment on the given heading
75 over forward>> v. 0 <=> {
76 { +gt+ [ next-segment ] }
77 { +lt+ [ previous-segment ] }
78 { +eq+ [ nip ] } ! current segment
81 :: distance-to-next-segment ( current next location heading -- distance )
82 current forward>> :> cf
83 cf next location>> v. cf location v. - cf heading v. / ;
85 :: distance-to-next-segment-area ( current next location heading -- distance )
86 current forward>> :> cf
87 next current half-way-between-oints :> h
88 cf h v. cf location v. - cf heading v. / ;
90 : vector-to-centre ( seg loc -- v )
91 over location>> swap v- swap forward>> proj-perp ;
93 : distance-from-centre ( seg loc -- distance )
94 vector-to-centre norm ;
96 : wall-normal ( seg oint -- n )
97 location>> vector-to-centre normalize ;
99 CONSTANT: distant 1000
101 : max-real ( a b -- c )
102 #! sometimes collision-coefficient yields complex roots, so we ignore these (hack)
104 over real? [ max ] [ nip ] if
106 drop dup real? [ drop distant ] unless
109 :: collision-coefficient ( v w r -- c )
116 c b a quadratic max-real
119 : sideways-heading ( oint segment -- v )
120 [ forward>> ] bi@ proj-perp ;
122 : sideways-relative-location ( oint segment -- loc )
123 [ [ location>> ] bi@ v- ] keep forward>> proj-perp ;
125 : (distance-to-collision) ( oint segment -- distance )
126 [ sideways-heading ] [ sideways-relative-location ]
127 [ nip radius>> ] 2tri collision-coefficient ;
129 : collision-vector ( oint segment -- v )
130 dupd (distance-to-collision) swap forward>> n*v ;
132 : bounce-forward ( segment oint -- )
133 [ wall-normal ] [ forward>> swap reflect ] [ forward<< ] tri ;
135 : bounce-left ( segment oint -- )
136 #! must be done after forward
137 [ forward>> vneg ] dip [ left>> swap reflect ]
138 [ forward>> proj-perp normalize ] [ left<< ] tri ;
140 : bounce-up ( segment oint -- )
141 #! must be done after forward and left!
142 nip [ forward>> ] [ left>> cross ] [ up<< ] tri ;
144 : bounce-off-wall ( oint segment -- )
145 swap [ bounce-forward ] [ bounce-left ] [ bounce-up ] 2tri ;