vector.lua 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. ---@meta
  2. ---Spatial Vectors
  3. ------------------
  4. -- Minetest stores 3-dimensional spatial vectors in Lua as tables of 3 coordinates,
  5. -- and has a class to represent them (`vector.*`), which this chapter is about.
  6. -- For details on what a spatial vectors is, please refer to Wikipedia:
  7. -- https://en.wikipedia.org/wiki/Euclidean_vector.
  8. --
  9. -- Spatial vectors are used for various things, including, but not limited to:
  10. --
  11. -- * any 3D spatial vector (x/y/z-directions)
  12. -- * Euler angles (pitch/yaw/roll in radians) (Spatial vectors have no real semantic
  13. -- meaning here. Therefore, most vector operations make no sense in this use case.)
  14. --
  15. -- Note that they are *not* used for:
  16. --
  17. -- * n-dimensional vectors where n is not 3 (ie. n=2)
  18. -- * arrays of the form `{num, num, num}`
  19. --
  20. -- The API documentation may refer to spatial vectors, as produced by `vector.new`,
  21. -- by any of the following notations:
  22. --
  23. -- * `(x, y, z)` (Used rarely, and only if it's clear that it's a vector.)
  24. -- * `vector.new(x, y, z)`
  25. -- * `{x=num, y=num, z=num}` (Even here you are still supposed to use `vector.new`.)
  26. --
  27. -- **Compatibility notes**
  28. --
  29. -- Vectors used to be defined as tables of the form `{x = num, y = num, z = num}`.
  30. -- Since Minetest 5.5.0, vectors additionally have a metatable to enable easier use.
  31. -- Note: Those old-style vectors can still be found in old mod code. Hence, mod and
  32. -- engine APIs still need to be able to cope with them in many places.
  33. --
  34. -- Manually constructed tables are deprecated and highly discouraged. This interface
  35. -- should be used to ensure seamless compatibility between mods and the Minetest API.
  36. -- This is especially important to callback function parameters and functions overwritten
  37. -- by mods.
  38. -- Also, though not likely, the internal implementation of a vector might change in
  39. -- the future.
  40. -- In your own code, or if you define your own API, you can, of course, still use
  41. -- other representations of vectors.
  42. --
  43. -- Vectors provided by API functions will provide an instance of this class if not
  44. -- stated otherwise. Mods should adapt this for convenience reasons.
  45. --
  46. -- **Special properties of the class**
  47. --
  48. -- Vectors can be indexed with numbers and allow method and operator syntax.
  49. --
  50. -- All these forms of addressing a vector `v` are valid:
  51. -- `v[1]`, `v[3]`, `v.x`, `v[1] = 42`, `v.y = 13`
  52. -- Note: Prefer letter over number indexing for performance and compatibility reasons.
  53. --
  54. -- Where `v` is a vector and `foo` stands for any function name, `v:foo(...)` does
  55. -- the same as `vector.foo(v, ...)`, apart from deprecated functionality.
  56. --
  57. -- `tostring` is defined for vectors, see `vector.to_string`.
  58. --
  59. -- The metatable that is used for vectors can be accessed via `vector.metatable`.
  60. -- Do not modify it!
  61. --
  62. -- All `vector.*` functions allow vectors `{x = X, y = Y, z = Z}` without metatables.
  63. -- Returned vectors always have a metatable set.
  64. --
  65. -- **Operators**
  66. --
  67. -- Operators can be used if all of the involved vectors have metatables:
  68. --
  69. -- * `v1 == v2`:
  70. -- * Returns whether `v1` and `v2` are identical.
  71. -- * `-v`:
  72. -- * Returns the additive inverse of v.
  73. -- * `v1 + v2`:
  74. -- * Returns the sum of both vectors.
  75. -- * Note: `+` cannot be used together with scalars.
  76. -- * `v1 - v2`:
  77. -- * Returns the difference of `v1` subtracted by `v2`.
  78. -- * Note: `-` cannot be used together with scalars.
  79. -- * `v * s` or `s * v`:
  80. -- * Returns `v` scaled by `s`.
  81. -- * `v / s`:
  82. -- * Returns `v` scaled by `1 / s`.
  83. ---@alias mt.Vector mt.VectorFields|mt.VectorMethods
  84. ---@class mt.VectorFields
  85. ---@field x number
  86. ---@field y number
  87. ---@field z number
  88. ---@field [1] number|nil
  89. ---@field [2] number|nil
  90. ---@field [3] number|nil
  91. ---@operator unm: mt.Vector
  92. ---@operator sub(mt.Vector): mt.Vector
  93. ---@operator add(mt.Vector): mt.Vector
  94. ---@operator mul(number): mt.Vector
  95. ---@operator div(number): mt.Vector
  96. ---@class mt.VectorMethods
  97. vector = {}
  98. ---Create a new vector.
  99. ---
  100. ---Recommendations:
  101. ---* Use `vector.zero()` instead of `vector.new()`
  102. ---* Use `vector.copy(v)` instead of `vector.new(v)`
  103. ---@param x number
  104. ---@param y number
  105. ---@param z number
  106. ---@return mt.Vector
  107. ---@nodiscard
  108. ---@overload fun(v:mt.Vector|nil): mt.Vector
  109. function vector.new(x, y, z) end
  110. ---Create a new vector `(0, 0, 0)`.
  111. ---@return mt.Vector
  112. ---@nodiscard
  113. function vector.zero() end
  114. ---Returns a copy of the vector `v`.
  115. ---@param v mt.Vector
  116. ---@return mt.Vector
  117. function vector.copy(v) end
  118. ---Returns `v, np`, where `v` is a vector read from the given string `s` and `np` is the next position in the string after the vector.
  119. ---Returns `nil` on failure.
  120. ---@param s string Has to begin with a substring of the form `"(x, y, z)"`
  121. ---@param init? integer If given starts looking for the vector at this string index.
  122. ---@return mt.Vector?
  123. ---@return integer?
  124. ---@nodiscard
  125. function vector.from_string(s, init) end
  126. ---Returns a string of the form `"(x, y, z)"`.
  127. ---@param v mt.Vector
  128. ---@return string
  129. ---@nodiscard
  130. function vector.to_string(v) end
  131. ---Returns a vector of length 1 with direction `p1` to `p2`.
  132. ---
  133. ---If `p1` and `p2` are identical, returns `(0, 0, 0)`.
  134. ---@param p1 mt.Vector
  135. ---@param p2 mt.Vector
  136. ---@return mt.Vector
  137. ---@nodiscard
  138. function vector.direction(p1, p2) end
  139. ---Returns zero or a positive number, the distance between `p1` and `p2`.
  140. ---@param p1 mt.Vector
  141. ---@param p2 mt.Vector
  142. ---@return number
  143. ---@nodiscard
  144. function vector.distance(p1, p2) end
  145. ---Returns zero or a positive number, the length of vector `v`.
  146. ---@param v mt.Vector
  147. ---@return number
  148. ---@nodiscard
  149. function vector.length(v) end
  150. ---Returns a vector of length 1 with direction of vector `v`.
  151. ---
  152. ---If `v` has zero length, returns `(0, 0, 0)`.
  153. ---@param v mt.Vector
  154. ---@return mt.Vector
  155. ---@nodiscard
  156. function vector.normalize(v) end
  157. ---Returns a vector, each dimension rounded down.
  158. ---@param v mt.Vector
  159. ---@return mt.Vector
  160. ---@nodiscard
  161. function vector.floor(v) end
  162. ---Returns a vector, each dimension rounded to nearest integer.
  163. ---
  164. ---At a multiple of `0.5`, rounds away from zero.
  165. ---@param v mt.Vector
  166. ---@return mt.Vector
  167. ---@nodiscard
  168. function vector.round(v) end
  169. ---Returns a vector where the function `func` has been applied to each component.
  170. ---@param v mt.Vector
  171. ---@param func fun(i: number):number
  172. ---@return mt.Vector
  173. ---@nodiscard
  174. function vector.apply(v, func) end
  175. ---Returns a boolean, `true` if the vectors are identical.
  176. ---@param a mt.Vector
  177. ---@param b mt.Vector
  178. ---@return boolean
  179. ---@nodiscard
  180. function vector.equals(a, b) end
  181. ---Returns in order `minp`, `maxp` vectors of the cuboid defined by `v1`, `v2`.
  182. ---@param v1 mt.Vector
  183. ---@param v2 mt.Vector
  184. ---@return mt.Vector minp
  185. ---@return mt.Vector maxp
  186. ---@nodiscard
  187. function vector.sort(v1, v2) end
  188. ---Returns the angle between `v1` and `v2` in radians.
  189. ---@param v1 mt.Vector
  190. ---@param v2 mt.Vector
  191. ---@return number
  192. ---@nodiscard
  193. function vector.angle(v1, v2) end
  194. ---Returns the dot product of `v1` and `v2`.
  195. ---@param v1 mt.Vector
  196. ---@param v2 mt.Vector
  197. ---@return number
  198. function vector.dot(v1, v2) end
  199. ---Returns the cross product of `v1` and `v2`.
  200. ---@param v1 mt.Vector
  201. ---@param v2 mt.Vector
  202. ---@return mt.Vector
  203. ---@nodiscard
  204. function vector.cross(v1, v2) end
  205. ---Returns the sum of the vectors `v` and `(x, y, z)`.
  206. ---@param v mt.Vector
  207. ---@param x number
  208. ---@param y number
  209. ---@param z number
  210. ---@return mt.Vector
  211. ---@nodiscard
  212. function vector.offset(v, x, y, z) end
  213. ---Returns a boolean value indicating whether `v` is a real vector, eg. created by a `vector.*` function.
  214. ---
  215. ---Returns `false` for anything else, including tables like `{x=3,y=1,z=4}`.
  216. ---@param v mt.Vector
  217. ---@return boolean
  218. ---@nodiscard
  219. function vector.check(v) end
  220. ---If `x` is a vector: Returns the sum of `v` and `x`.
  221. ---
  222. ---If `x` is a number: Adds `x` to each component of `v`.
  223. ---@param v mt.Vector
  224. ---@param x mt.Vector | number
  225. ---@return mt.Vector
  226. ---@nodiscard
  227. function vector.add(v, x) end
  228. ---If `x` is a vector: Returns the difference of `v` subtracted by `x`.
  229. ---
  230. ---If `x` is a number: Subtracts `x` from each component of `v`.
  231. ---@param v mt.Vector
  232. ---@param x mt.Vector | number
  233. ---@return mt.Vector
  234. ---@nodiscard
  235. function vector.subtract(v, x) end
  236. ---Returns a scaled vector.
  237. ---@param v mt.Vector
  238. ---@param s number
  239. ---@return mt.Vector
  240. ---@nodiscard
  241. function vector.multiply(v, s) end
  242. ---Returns a scaled vector.
  243. ---@param v mt.Vector
  244. ---@param s number
  245. ---@return mt.Vector
  246. ---@nodiscard
  247. function vector.divide(v, s) end
  248. ---Applies the rotation `r` (in radians) to `v` and returns the result.
  249. ---@param v mt.Vector
  250. ---@param r number
  251. ---@return mt.Vector
  252. ---@nodiscard
  253. function vector.rotate(v, r) end
  254. ---Returns `v1` rotated around axis `v2` by `a` radians according to the right hand rule.
  255. ---@param v1 mt.Vector
  256. ---@param v2 mt.Vector
  257. ---@param a number
  258. ---@return mt.Vector
  259. ---@nodiscard
  260. function vector.rotate_around_axis(v1, v2, a) end
  261. ---Returns a rotation vector for `direction` pointing forward using `up` as the up vector.
  262. ---
  263. ---If `up` is omitted, the roll of the returned vector defaults to zero.
  264. ---
  265. ---Otherwise `direction` and `up` need to be vectors in a 90 degree angle to each other.
  266. ---@param direction mt.Vector
  267. ---@param up? mt.Vector
  268. ---@return mt.Vector
  269. ---@nodiscard
  270. function vector.dir_to_rotation(direction, up) end