1 ! Copyright (C) 2007 Alex Chapman
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays float-arrays kernel locals math math.constants math.functions math.matrices math.vectors math.quaternions random sequences ;
6 ! An oint is a point with three linearly independent unit vectors
7 ! given relative to that point. In jamshred a player's location and
8 ! direction are given by the player's oint. Similarly, a tunnel
9 ! segment's location and orientation are given by an oint.
11 TUPLE: oint location forward up left ;
14 : rotation-quaternion ( theta axis -- quaternion )
15 swap 2 / dup cos swap sin rot n*v first3 rect> >r rect> r> 2array ;
17 : rotate-vector ( q qrecip v -- v )
20 : rotate-oint ( oint theta axis -- )
21 rotation-quaternion dup qrecip pick
22 [ forward>> rotate-vector >>forward ]
23 [ up>> rotate-vector >>up ]
24 [ left>> rotate-vector >>left ] 3tri drop ;
26 : left-pivot ( oint theta -- )
27 over left>> rotate-oint ;
29 : up-pivot ( oint theta -- )
30 over up>> rotate-oint ;
32 : random-float+- ( n -- m )
33 #! find a random float between -n/2 and n/2
34 dup 10000 * >fixnum random 10000 / swap 2 / - ;
36 : random-turn ( oint theta -- )
37 2 / 2dup random-float+- left-pivot random-float+- up-pivot ;
39 : go-forward ( distance oint -- )
40 [ forward>> n*v ] [ location>> v+ ] [ (>>location) ] tri ;
42 : distance-vector ( oint oint -- vector )
43 [ location>> ] bi@ swap v- ;
45 : distance ( oint oint -- distance )
46 distance-vector norm ;
48 : scalar-projection ( v1 v2 -- n )
49 #! the scalar projection of v1 onto v2
52 : proj-perp ( u v -- w )
55 : perpendicular-distance ( oint oint -- distance )
56 tuck distance-vector swap 2dup left>> scalar-projection abs
57 -rot up>> scalar-projection abs + ;
59 :: reflect ( v n -- v' )
60 #! bounce v on a surface with normal n
61 v v n v. n n v. / 2 * n n*v v- ;