/unmaintained/jamshred/oint/oint.factor

http://github.com/abeaumont/factor · Factor · 73 lines · 47 code · 20 blank · 6 comment · 0 complexity · ebed444cc70fd2cc11652abf030f5d9c MD5 · raw file

  1. ! Copyright (C) 2007, 2008 Alex Chapman
  2. ! See http://factorcode.org/license.txt for BSD license.
  3. USING: accessors arrays kernel locals math math.constants math.functions math.matrices math.vectors math.quaternions random sequences ;
  4. IN: jamshred.oint
  5. ! An oint is a point with three linearly independent unit vectors
  6. ! given relative to that point. In jamshred a player's location and
  7. ! direction are given by the player's oint. Similarly, a tunnel
  8. ! segment's location and orientation are given by an oint.
  9. TUPLE: oint location forward up left ;
  10. C: <oint> oint
  11. : rotation-quaternion ( theta axis -- quaternion )
  12. swap 2 / dup cos swap sin rot n*v first3 rect> [ rect> ] dip 2array ;
  13. : rotate-vector ( q qrecip v -- v )
  14. v>q swap q* q* q>v ;
  15. : rotate-oint ( oint theta axis -- )
  16. rotation-quaternion dup qrecip pick
  17. [ forward>> rotate-vector >>forward ]
  18. [ up>> rotate-vector >>up ]
  19. [ left>> rotate-vector >>left ] 3tri drop ;
  20. : left-pivot ( oint theta -- )
  21. over left>> rotate-oint ;
  22. : up-pivot ( oint theta -- )
  23. over up>> rotate-oint ;
  24. : forward-pivot ( oint theta -- )
  25. over forward>> rotate-oint ;
  26. : random-float+- ( n -- m )
  27. #! find a random float between -n/2 and n/2
  28. dup 10000 * >integer random 10000 / swap 2 / - ;
  29. : random-turn ( oint theta -- )
  30. 2 / 2dup random-float+- left-pivot random-float+- up-pivot ;
  31. : location+ ( v oint -- )
  32. [ location>> v+ ] [ (>>location) ] bi ;
  33. : go-forward ( distance oint -- )
  34. [ forward>> n*v ] [ location+ ] bi ;
  35. : distance-vector ( oint oint -- vector )
  36. [ location>> ] bi@ swap v- ;
  37. : distance ( oint oint -- distance )
  38. distance-vector norm ;
  39. : scalar-projection ( v1 v2 -- n )
  40. #! the scalar projection of v1 onto v2
  41. [ v. ] [ norm ] bi / ;
  42. : proj-perp ( u v -- w )
  43. dupd proj v- ;
  44. : perpendicular-distance ( oint oint -- distance )
  45. [ distance-vector ] keep 2dup left>> scalar-projection abs
  46. -rot up>> scalar-projection abs + ;
  47. :: reflect ( v n -- v' )
  48. #! bounce v on a surface with normal n
  49. v v n v. n n v. / 2 * n n*v v- ;
  50. : half-way ( p1 p2 -- p3 )
  51. over v- 2 v/n v+ ;
  52. : half-way-between-oints ( o1 o2 -- p )
  53. [ location>> ] bi@ half-way ;