1 ! Copyright (C) 2007, 2008 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 : forward-pivot ( oint theta -- )
33 over forward>> rotate-oint ;
35 : random-float+- ( n -- m )
36 #! find a random float between -n/2 and n/2
37 dup 10000 * >fixnum random 10000 / swap 2 / - ;
39 : random-turn ( oint theta -- )
40 2 / 2dup random-float+- left-pivot random-float+- up-pivot ;
42 : location+ ( v oint -- )
43 [ location>> v+ ] [ (>>location) ] bi ;
45 : go-forward ( distance oint -- )
46 [ forward>> n*v ] [ location+ ] bi ;
48 : distance-vector ( oint oint -- vector )
49 [ location>> ] bi@ swap v- ;
51 : distance ( oint oint -- distance )
52 distance-vector norm ;
54 : scalar-projection ( v1 v2 -- n )
55 #! the scalar projection of v1 onto v2
58 : proj-perp ( u v -- w )
61 : perpendicular-distance ( oint oint -- distance )
62 tuck distance-vector swap 2dup left>> scalar-projection abs
63 -rot up>> scalar-projection abs + ;
65 :: reflect ( v n -- v' )
66 #! bounce v on a surface with normal n
67 v v n v. n n v. / 2 * n n*v v- ;
69 : half-way ( p1 p2 -- p3 )
72 : half-way-between-oints ( o1 o2 -- p )
73 [ location>> ] bi@ half-way ;