init.lua 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. return {
  2. summary = 'What is your vector victor.',
  3. description = [[
  4. LÖVR has math objects for vectors, matrices, and quaternions, collectively called "vector
  5. objects". Vectors are useful because they can represent a multidimensional quantity (like a 3D
  6. position) using just a single value.
  7. ]],
  8. constructors = {
  9. 'lovr.math.vec2',
  10. 'lovr.math.vec3',
  11. 'lovr.math.vec4',
  12. 'lovr.math.quat',
  13. 'lovr.math.mat4',
  14. 'lovr.math.newVec2',
  15. 'lovr.math.newVec3',
  16. 'lovr.math.newVec4',
  17. 'lovr.math.newQuat',
  18. 'lovr.math.newMat4'
  19. },
  20. notes = [[
  21. Most LÖVR functions that accept positions, orientations, transforms, velocities, etc. also accept
  22. vector objects, so they can be used interchangeably with numbers:
  23. function lovr.draw(pass)
  24. -- position and size are vec3's, rotation is a quat
  25. pass:box(position, size, rotation)
  26. end
  27. ### Temporary vs. Permanent
  28. Vectors can be created in two different ways: **permanent** and **temporary**.
  29. **Permanent** vectors behave like normal Lua values. They are individual objects that are
  30. garbage collected when no longer needed. They're created using the usual `lovr.math.new<Type>`
  31. syntax:
  32. self.position = lovr.math.newVec3(x, y, z)
  33. **Temporary** vectors are created from a shared pool of vector objects. This makes them faster
  34. because they use temporary memory and do not need to be garbage collected. To make a temporary
  35. vector, leave off the `new` prefix:
  36. local position = lovr.math.vec3(x, y, z)
  37. As a shortcut, vector constructors are placed on the global scope. The uppercase name of the
  38. vector is a function that will create a permanent vector, and the lowercase name will create a
  39. temporary vector. This can be disabled using the `t.math.globals` option in `lovr.conf`.
  40. local position = vec3(x1, y1, z1) + vec3(x2, y2, z2)
  41. local transform = Mat4()
  42. Temporary vectors, with all their speed, come with an important restriction: they can only be
  43. used during the frame in which they were created. Saving them into variables and using them
  44. later on will throw an error:
  45. local position = vec3(1, 2, 3)
  46. function lovr.update(dt)
  47. -- Reusing the temporary 'position' vector across frames will error:
  48. position:add(vec3(dt))
  49. end
  50. It's possible to overflow the temporary vector pool. If that happens, `lovr.math.drain` can be
  51. used to periodically drain the pool, invalidating any existing temporary vectors.
  52. ### Metamethods
  53. Vectors have metamethods, allowing them to be used using the normal math operators like `+`,
  54. `-`, `*`, `/`, etc.
  55. print(vec3(2, 4, 6) * .5 + vec3(10, 20, 30))
  56. These metamethods will create new temporary vectors.
  57. ### Components and Swizzles
  58. The raw components of a vector can be accessed like normal fields:
  59. print(vec3(1, 2, 3).z) --> 3
  60. print(mat4()[16]) --> 1
  61. Also, multiple fields can be accessed and combined into a new (temporary) vector, called swizzling:
  62. local position = vec3(10, 5, 1)
  63. print(position.xy) --> vec2(10, 5)
  64. print(position.xyy) --> vec3(10, 5, 5)
  65. print(position.zyxz) --> vec4(1, 5, 10, 1)
  66. The following fields are supported for vectors:
  67. - `x`, `y`, `z`, `w`
  68. - `r`, `g`, `b`, `a`
  69. - `s`, `t`, `p`, `q`
  70. Quaternions support `x`, `y`, `z`, and `w`.
  71. Matrices use numbers for accessing individual components in "column-major" order.
  72. All fields can also be assigned to.
  73. -- Swap the components of a 2D vector
  74. v.xy = v.yx
  75. The `unpack` function can be used (on any vector type) to access all of the individual components of
  76. a vector object. For quaternions you can choose whether you want to unpack the angle/axis
  77. representation or the raw quaternion components. Similarly, matrices support raw unpacking as well
  78. as decomposition into translation/scale/rotation values.
  79. ### Vector Constants
  80. The following vector constants are available. They return new temporary vectors each time they
  81. are used:
  82. - `vec2.zero` (0, 0)
  83. - `vec2.one` (1, 1)
  84. - `vec3.zero` (0, 0, 0)
  85. - `vec3.one` (1, 1, 1)
  86. - `vec3.left` (-1, 0, 0)
  87. - `vec3.right` (1, 0, 0)
  88. - `vec3.up` (0, 1, 0)
  89. - `vec3.down` (0, -1, 0)
  90. - `vec3.back` (0, 0, 1)
  91. - `vec3.forward` (0, 0, -1)
  92. - `vec4.zero` (0, 0, 0, 0)
  93. - `vec4.one` (1, 1, 1, 1)
  94. - `quat.identity` (0, 0, 0, 1)
  95. ]]
  96. }