vector.lua 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. vector = {}
  2. function vector.new(a, b, c)
  3. if type(a) == "table" then
  4. assert(a.x and a.y and a.z, "Invalid vector passed to vector.new()")
  5. return {x=a.x, y=a.y, z=a.z}
  6. elseif a then
  7. assert(b and c, "Invalid arguments for vector.new()")
  8. return {x=a, y=b, z=c}
  9. end
  10. return {x=0, y=0, z=0}
  11. end
  12. function vector.equals(a, b)
  13. return a.x == b.x and
  14. a.y == b.y and
  15. a.z == b.z
  16. end
  17. function vector.length(v)
  18. return math.hypot(v.x, math.hypot(v.y, v.z))
  19. end
  20. function vector.normalize(v)
  21. local len = vector.length(v)
  22. if len == 0 then
  23. return {x=0, y=0, z=0}
  24. else
  25. return vector.divide(v, len)
  26. end
  27. end
  28. function vector.floor(v)
  29. return {
  30. x = math.floor(v.x),
  31. y = math.floor(v.y),
  32. z = math.floor(v.z)
  33. }
  34. end
  35. function vector.round(v)
  36. return {
  37. x = math.floor(v.x + 0.5),
  38. y = math.floor(v.y + 0.5),
  39. z = math.floor(v.z + 0.5)
  40. }
  41. end
  42. function vector.apply(v, func)
  43. return {
  44. x = func(v.x),
  45. y = func(v.y),
  46. z = func(v.z)
  47. }
  48. end
  49. function vector.distance(a, b)
  50. local x = a.x - b.x
  51. local y = a.y - b.y
  52. local z = a.z - b.z
  53. return math.hypot(x, math.hypot(y, z))
  54. end
  55. function vector.direction(pos1, pos2)
  56. return vector.normalize({
  57. x = pos2.x - pos1.x,
  58. y = pos2.y - pos1.y,
  59. z = pos2.z - pos1.z
  60. })
  61. end
  62. function vector.angle(a, b)
  63. local dotp = vector.dot(a, b)
  64. local cp = vector.cross(a, b)
  65. local crossplen = vector.length(cp)
  66. return math.atan2(crossplen, dotp)
  67. end
  68. function vector.dot(a, b)
  69. return a.x * b.x + a.y * b.y + a.z * b.z
  70. end
  71. function vector.cross(a, b)
  72. return {
  73. x = a.y * b.z - a.z * b.y,
  74. y = a.z * b.x - a.x * b.z,
  75. z = a.x * b.y - a.y * b.x
  76. }
  77. end
  78. function vector.add(a, b)
  79. if type(b) == "table" then
  80. return {x = a.x + b.x,
  81. y = a.y + b.y,
  82. z = a.z + b.z}
  83. else
  84. return {x = a.x + b,
  85. y = a.y + b,
  86. z = a.z + b}
  87. end
  88. end
  89. function vector.subtract(a, b)
  90. if type(b) == "table" then
  91. return {x = a.x - b.x,
  92. y = a.y - b.y,
  93. z = a.z - b.z}
  94. else
  95. return {x = a.x - b,
  96. y = a.y - b,
  97. z = a.z - b}
  98. end
  99. end
  100. function vector.multiply(a, b)
  101. if type(b) == "table" then
  102. return {x = a.x * b.x,
  103. y = a.y * b.y,
  104. z = a.z * b.z}
  105. else
  106. return {x = a.x * b,
  107. y = a.y * b,
  108. z = a.z * b}
  109. end
  110. end
  111. function vector.divide(a, b)
  112. if type(b) == "table" then
  113. return {x = a.x / b.x,
  114. y = a.y / b.y,
  115. z = a.z / b.z}
  116. else
  117. return {x = a.x / b,
  118. y = a.y / b,
  119. z = a.z / b}
  120. end
  121. end
  122. function vector.offset(v, x, y, z)
  123. return {x = v.x + x,
  124. y = v.y + y,
  125. z = v.z + z}
  126. end
  127. function vector.sort(a, b)
  128. return {x = math.min(a.x, b.x), y = math.min(a.y, b.y), z = math.min(a.z, b.z)},
  129. {x = math.max(a.x, b.x), y = math.max(a.y, b.y), z = math.max(a.z, b.z)}
  130. end
  131. local function sin(x)
  132. if x % math.pi == 0 then
  133. return 0
  134. else
  135. return math.sin(x)
  136. end
  137. end
  138. local function cos(x)
  139. if x % math.pi == math.pi / 2 then
  140. return 0
  141. else
  142. return math.cos(x)
  143. end
  144. end
  145. function vector.rotate_around_axis(v, axis, angle)
  146. local cosangle = cos(angle)
  147. local sinangle = sin(angle)
  148. axis = vector.normalize(axis)
  149. -- https://en.wikipedia.org/wiki/Rodrigues%27_rotation_formula
  150. local dot_axis = vector.multiply(axis, vector.dot(axis, v))
  151. local cross = vector.cross(v, axis)
  152. return vector.new(
  153. cross.x * sinangle + (v.x - dot_axis.x) * cosangle + dot_axis.x,
  154. cross.y * sinangle + (v.y - dot_axis.y) * cosangle + dot_axis.y,
  155. cross.z * sinangle + (v.z - dot_axis.z) * cosangle + dot_axis.z
  156. )
  157. end
  158. function vector.rotate(v, rot)
  159. local sinpitch = sin(-rot.x)
  160. local sinyaw = sin(-rot.y)
  161. local sinroll = sin(-rot.z)
  162. local cospitch = cos(rot.x)
  163. local cosyaw = cos(rot.y)
  164. local cosroll = math.cos(rot.z)
  165. -- Rotation matrix that applies yaw, pitch and roll
  166. local matrix = {
  167. {
  168. sinyaw * sinpitch * sinroll + cosyaw * cosroll,
  169. sinyaw * sinpitch * cosroll - cosyaw * sinroll,
  170. sinyaw * cospitch,
  171. },
  172. {
  173. cospitch * sinroll,
  174. cospitch * cosroll,
  175. -sinpitch,
  176. },
  177. {
  178. cosyaw * sinpitch * sinroll - sinyaw * cosroll,
  179. cosyaw * sinpitch * cosroll + sinyaw * sinroll,
  180. cosyaw * cospitch,
  181. },
  182. }
  183. -- Compute matrix multiplication: `matrix` * `v`
  184. return vector.new(
  185. matrix[1][1] * v.x + matrix[1][2] * v.y + matrix[1][3] * v.z,
  186. matrix[2][1] * v.x + matrix[2][2] * v.y + matrix[2][3] * v.z,
  187. matrix[3][1] * v.x + matrix[3][2] * v.y + matrix[3][3] * v.z
  188. )
  189. end
  190. function vector.dir_to_rotation(forward, up)
  191. forward = vector.normalize(forward)
  192. local rot = {x = math.asin(forward.y), y = -math.atan2(forward.x, forward.z), z = 0}
  193. if not up then
  194. return rot
  195. end
  196. assert(vector.dot(forward, up) < 0.000001,
  197. "Invalid vectors passed to vector.dir_to_rotation().")
  198. up = vector.normalize(up)
  199. -- Calculate vector pointing up with roll = 0, just based on forward vector.
  200. local forwup = vector.rotate({x = 0, y = 1, z = 0}, rot)
  201. -- 'forwup' and 'up' are now in a plane with 'forward' as normal.
  202. -- The angle between them is the absolute of the roll value we're looking for.
  203. rot.z = vector.angle(forwup, up)
  204. -- Since vector.angle never returns a negative value or a value greater
  205. -- than math.pi, rot.z has to be inverted sometimes.
  206. -- To determine wether this is the case, we rotate the up vector back around
  207. -- the forward vector and check if it worked out.
  208. local back = vector.rotate_around_axis(up, forward, -rot.z)
  209. -- We don't use vector.equals for this because of floating point imprecision.
  210. if (back.x - forwup.x) * (back.x - forwup.x) +
  211. (back.y - forwup.y) * (back.y - forwup.y) +
  212. (back.z - forwup.z) * (back.z - forwup.z) > 0.0000001 then
  213. rot.z = -rot.z
  214. end
  215. return rot
  216. end