init.lua 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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()
  24. -- position and size are vec3's, rotation is a quat
  25. lovr.graphics.box('fill', 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 LÖVR objects. They are individual objects that are garbage
  30. collected when no longer needed. They're created using the usual `lovr.math.new<Type>` syntax:
  31. self.position = lovr.math.newVec3(x, y, z)
  32. **Temporary** vectors are created from a shared pool of vector objects. This makes them faster
  33. because they use temporary memory and do not need to be garbage collected. To make a temporary
  34. vector, leave off the `new` prefix:
  35. local position = lovr.math.vec3(x, y, z)
  36. As a further shorthand, these vector constructors are placed on the global scope. If you prefer to
  37. keep the global scope clean, this can be configured using the `t.math.globals` flag in `lovr.conf`.
  38. local position = vec3(x1, y1, z1) + vec3(x2, y2, z2)
  39. Temporary vectors, with all their speed, come with an important restriction: they can only be used
  40. during the frame in which they were created. Saving them into variables and using them later on
  41. will throw an error:
  42. local position = vec3(1, 2, 3)
  43. function lovr.update(dt)
  44. -- Reusing a temporary vector across frames will error:
  45. position:add(vec3(dt))
  46. end
  47. It's possible to overflow the temporary vector pool. If that happens, `lovr.math.drain` can be used
  48. to periodically drain the pool, invalidating any existing temporary vectors.
  49. ### Metamethods
  50. Vectors have metamethods, allowing them to be used using the normal math operators like `+`, `-`,
  51. `*`, `/`, etc.
  52. print(vec3(2, 4, 6) * .5 + vec3(10, 20, 30))
  53. These metamethods will create new temporary vectors.
  54. ### Components and Swizzles
  55. The raw components of a vector can be accessed like normal fields:
  56. print(vec3(1, 2, 3).z) --> 3
  57. print(mat4()[16]) --> 1
  58. Also, multiple fields can be accessed and combined into a new (temporary) vector, called swizzling:
  59. local position = vec3(10, 5, 1)
  60. print(position.xy) --> vec2(10, 5)
  61. print(position.xyy) --> vec3(10, 5, 5)
  62. print(position.zyxz) --> vec4(1, 5, 10, 1)
  63. The following fields are supported for vectors:
  64. - `x`, `y`, `z`, `w`
  65. - `r`, `g`, `b`, `a`
  66. - `s`, `t`, `p`, `q`
  67. Quaternions support `x`, `y`, `z`, and `w`.
  68. Matrices use numbers for accessing individual components in "column-major" order.
  69. All fields can also be assigned to.
  70. -- Swap the components of a 2D vector
  71. v.xy = v.yx
  72. The `unpack` function can be used (on any vector type) to access all of the individual components of
  73. a vector object. For quaternions you can choose whether you want to unpack the angle/axis
  74. representation or the raw quaternion components. Similarly, matrices support raw unpacking as well
  75. as decomposition into translation/scale/rotation values.
  76. ]]
  77. }