Browse Source

More vector docs;

bjorn 1 month ago
parent
commit
4b9a73af1c

+ 163 - 8
api/init.lua

@@ -42957,13 +42957,114 @@ return {
       enums = {},
       enums = {},
       external = true,
       external = true,
       functions = {
       functions = {
+        {
+          name = "cross",
+          summary = "Get the cross product of two vectors.",
+          description = "Returns the cross product of two vectors.  This is a vector that is perpendicular to both vectors.",
+          key = "vector.cross",
+          module = "vector",
+          examples = {
+            {
+              code = "vector.cross(vector(1, 0, 0), vector(0, 1, 0)) --> vector(0, 0, 1)"
+            }
+          },
+          variants = {
+            {
+              arguments = {
+                {
+                  name = "v1",
+                  type = "vector",
+                  description = "The first vector."
+                },
+                {
+                  name = "v2",
+                  type = "vector",
+                  description = "The second vector."
+                }
+              },
+              returns = {
+                {
+                  name = "cross",
+                  type = "vector",
+                  description = "A vector perpendicular to both `v1` and `v2`."
+                }
+              }
+            }
+          }
+        },
+        {
+          name = "distance",
+          summary = "Get the distance between two vectors.",
+          description = "Returns the distance between two vectors.",
+          key = "vector.distance",
+          module = "vector",
+          examples = {
+            {
+              code = "vector.distance(vector(0, 0, 0), vector(10, 0, 0)) --> 10"
+            }
+          },
+          notes = "This is short for `vector.length(v1 - v2)",
+          variants = {
+            {
+              arguments = {
+                {
+                  name = "v1",
+                  type = "vector",
+                  description = "The first vector."
+                },
+                {
+                  name = "v2",
+                  type = "vector",
+                  description = "The second vector."
+                }
+              },
+              returns = {
+                {
+                  name = "distance",
+                  type = "number",
+                  description = "The distance between the two vectors."
+                }
+              }
+            }
+          }
+        },
+        {
+          name = "length",
+          summary = "Get the length of a vector.",
+          description = "Returns the length of the vector.",
+          key = "vector.length",
+          module = "vector",
+          examples = {
+            {
+              code = "vector(0, 5, 0):length() --> 5\nvector(3, 4):length() --> 5"
+            }
+          },
+          notes = "The length of a vector is computed as:\n\n    math.sqrt(x ^ 2 + y ^ 2 + z ^ 2)",
+          related = {
+            "vector.normalize"
+          },
+          variants = {
+            {
+              arguments = {},
+              returns = {
+                {
+                  name = "length",
+                  type = "number",
+                  description = "The length of the vector."
+                }
+              }
+            }
+          }
+        },
         {
         {
           name = "normalize",
           name = "normalize",
           summary = "Get a normalized vector.",
           summary = "Get a normalized vector.",
-          description = "Returns the normalized version of the input vector (a vector that points in the same direction, but has a length of 1).",
+          description = "Returns a normalized version of the input vector (a vector that points in the same direction, but has a length of 1).",
           key = "vector.normalize",
           key = "vector.normalize",
           module = "vector",
           module = "vector",
-          notes = "This can be called as a method, e.g. `v:normalize()`.",
+          related = {
+            "vector.length"
+          },
           variants = {
           variants = {
             {
             {
               arguments = {
               arguments = {
@@ -42994,27 +43095,28 @@ return {
               code = "local a = vector.pack(1, 2, 3)\nlocal b = vector.pack(5)\nprint(a + b) --> 6, 7, 8\n\n-- put the 3 numbers from lovr.headset.getPosition into a vector!\nlocal position = vector(lovr.headset.getPosition())"
               code = "local a = vector.pack(1, 2, 3)\nlocal b = vector.pack(5)\nprint(a + b) --> 6, 7, 8\n\n-- put the 3 numbers from lovr.headset.getPosition into a vector!\nlocal position = vector(lovr.headset.getPosition())"
             }
             }
           },
           },
-          notes = "As a shortcut, the `vector` library can be called like a function, which calls `vector.pack`:\n\n    vector(x, y, z) -- same as vector.pack(x, y, z)",
+          notes = "The `vector` library can be called like a function, which is shorthand for `vector.pack`:\n\n    vector(x, y, z) -- same as vector.pack(x, y, z)",
+          related = {
+            "vector.unpack"
+          },
           variants = {
           variants = {
             {
             {
               arguments = {
               arguments = {
                 {
                 {
                   name = "x",
                   name = "x",
                   type = "number",
                   type = "number",
-                  description = "The x component of the vector.",
-                  default = "0"
+                  description = "The x component of the vector."
                 },
                 },
                 {
                 {
                   name = "y",
                   name = "y",
                   type = "number",
                   type = "number",
-                  description = "The y component of the vector.",
-                  default = "x"
+                  description = "The y component of the vector."
                 },
                 },
                 {
                 {
                   name = "z",
                   name = "z",
                   type = "number",
                   type = "number",
                   description = "The z component of the vector.",
                   description = "The z component of the vector.",
-                  default = "x"
+                  default = "0"
                 }
                 }
               },
               },
               returns = {
               returns = {
@@ -43024,6 +43126,59 @@ return {
                   description = "The new vector."
                   description = "The new vector."
                 }
                 }
               }
               }
+            },
+            {
+              arguments = {
+                {
+                  name = "n",
+                  type = "number",
+                  description = "A number to assign to the x, y, and z components of the vector."
+                }
+              },
+              returns = {
+                {
+                  name = "v",
+                  type = "vector",
+                  description = "The new vector."
+                }
+              }
+            }
+          }
+        },
+        {
+          name = "unpack",
+          summary = "Get the components of a vector as numbers.",
+          description = "Returns the components of the vector as numbers.",
+          key = "vector.unpack",
+          module = "vector",
+          examples = {
+            {
+              code = "local x, y, z = vector(1, 2, 3):unpack()"
+            }
+          },
+          related = {
+            "vector.pack"
+          },
+          variants = {
+            {
+              arguments = {},
+              returns = {
+                {
+                  name = "x",
+                  type = "number",
+                  description = "The x component of the vector."
+                },
+                {
+                  name = "y",
+                  type = "number",
+                  description = "The y component of the vector."
+                },
+                {
+                  name = "z",
+                  type = "number",
+                  description = "The z component of the vector."
+                }
+              }
             }
             }
           }
           }
         }
         }

+ 32 - 0
api/lovr/vector/cross.lua

@@ -0,0 +1,32 @@
+return {
+  summary = 'Get the cross product of two vectors.',
+  description = [[
+    Returns the cross product of two vectors.  This is a vector that is perpendicular to both
+    vectors.
+  ]],
+  arguments = {
+    v1 = {
+      type = 'vector',
+      description = 'The first vector.'
+    },
+    v2 = {
+      type = 'vector',
+      description = 'The second vector.'
+    }
+  },
+  returns = {
+    cross = {
+      type = 'vector',
+      description = 'A vector perpendicular to both `v1` and `v2`.'
+    }
+  },
+  variants = {
+    {
+      arguments = { 'v1', 'v2' },
+      returns = { 'cross' }
+    }
+  },
+  example = [[
+    vector.cross(vector(1, 0, 0), vector(0, 1, 0)) --> vector(0, 0, 1)
+  ]]
+}

+ 30 - 0
api/lovr/vector/distance.lua

@@ -0,0 +1,30 @@
+return {
+  summary = 'Get the distance between two vectors.',
+  description = 'Returns the distance between two vectors.',
+  arguments = {
+    v1 = {
+      type = 'vector',
+      description = 'The first vector.'
+    },
+    v2 = {
+      type = 'vector',
+      description = 'The second vector.'
+    }
+  },
+  returns = {
+    distance = {
+      type = 'number',
+      description = 'The distance between the two vectors.'
+    }
+  },
+  variants = {
+    {
+      arguments = { 'v1', 'v2' },
+      returns = { 'distance' }
+    }
+  },
+  notes = 'This is short for `vector.length(v1 - v2)',
+  example = [[
+    vector.distance(vector(0, 0, 0), vector(10, 0, 0)) --> 10
+  ]]
+}

+ 34 - 0
api/lovr/vector/length.lua

@@ -0,0 +1,34 @@
+return {
+  summary = 'Get the length of a vector.',
+  description = 'Returns the length of the vector.',
+  arguments = {
+    v = {
+      type = 'vector',
+      description = 'The vector to get the length of.'
+    }
+  },
+  returns = {
+    length = {
+      type = 'number',
+      description = 'The length of the vector.'
+    }
+  },
+  variants = {
+    {
+      arguments = {},
+      returns = { 'length' }
+    }
+  },
+  notes = [[
+    The length of a vector is computed as:
+
+        math.sqrt(x ^ 2 + y ^ 2 + z ^ 2)
+  ]],
+  example = [[
+    vector(0, 5, 0):length() --> 5
+    vector(3, 4):length() --> 5
+  ]],
+  related = {
+    'vector.normalize'
+  }
+}

+ 4 - 2
api/lovr/vector/normalize.lua

@@ -1,7 +1,7 @@
 return {
 return {
   summary = 'Get a normalized vector.',
   summary = 'Get a normalized vector.',
   description = [[
   description = [[
-    Returns the normalized version of the input vector (a vector that points in the same direction,
+    Returns a normalized version of the input vector (a vector that points in the same direction,
     but has a length of 1).
     but has a length of 1).
   ]],
   ]],
   arguments = {
   arguments = {
@@ -22,5 +22,7 @@ return {
       returns = { 'normalized' }
       returns = { 'normalized' }
     }
     }
   },
   },
-  notes = 'This can be called as a method, e.g. `v:normalize()`.'
+  related = {
+    'vector.length'
+  }
 }
 }

+ 15 - 8
api/lovr/vector/pack.lua

@@ -1,23 +1,23 @@
 return {
 return {
   summary = 'Pack numbers into a vector.',
   summary = 'Pack numbers into a vector.',
-  description = [[
-    Packs numbers into a vector.
-  ]],
+  description = 'Packs numbers into a vector.',
   arguments = {
   arguments = {
     x = {
     x = {
       type = 'number',
       type = 'number',
-      default = '0',
       description = 'The x component of the vector.'
       description = 'The x component of the vector.'
     },
     },
     y = {
     y = {
       type = 'number',
       type = 'number',
-      default = 'x',
       description = 'The y component of the vector.'
       description = 'The y component of the vector.'
     },
     },
     z = {
     z = {
       type = 'number',
       type = 'number',
-      default = 'x',
+      default = '0',
       description = 'The z component of the vector.'
       description = 'The z component of the vector.'
+    },
+    n = {
+      type = 'number',
+      description = 'A number to assign to the x, y, and z components of the vector.'
     }
     }
   },
   },
   returns = {
   returns = {
@@ -30,10 +30,14 @@ return {
     {
     {
       arguments = { 'x', 'y', 'z' },
       arguments = { 'x', 'y', 'z' },
       returns = { 'v' }
       returns = { 'v' }
+    },
+    {
+      arguments = { 'n' },
+      returns = { 'v' }
     }
     }
   },
   },
   notes = [[
   notes = [[
-    As a shortcut, the `vector` library can be called like a function, which calls `vector.pack`:
+    The `vector` library can be called like a function, which is shorthand for `vector.pack`:
 
 
         vector(x, y, z) -- same as vector.pack(x, y, z)
         vector(x, y, z) -- same as vector.pack(x, y, z)
   ]],
   ]],
@@ -44,5 +48,8 @@ return {
 
 
     -- put the 3 numbers from lovr.headset.getPosition into a vector!
     -- put the 3 numbers from lovr.headset.getPosition into a vector!
     local position = vector(lovr.headset.getPosition())
     local position = vector(lovr.headset.getPosition())
-  ]]
+  ]],
+  related = {
+    'vector.unpack'
+  }
 }
 }

+ 34 - 0
api/lovr/vector/unpack.lua

@@ -0,0 +1,34 @@
+return {
+  summary = 'Get the components of a vector as numbers.',
+  description = 'Returns the components of the vector as numbers.',
+  arguments = {
+    v = {
+      type = 'vector',
+      description = 'The vector to unpack.'
+    }
+  },
+  returns = {
+    x = {
+      type = 'number',
+      description = 'The x component of the vector.'
+    },
+    y = {
+      type = 'number',
+      description = 'The y component of the vector.'
+    },
+    z = {
+      type = 'number',
+      description = 'The z component of the vector.'
+    },
+  },
+  variants = {
+    {
+      arguments = {},
+      returns = { 'x', 'y', 'z' }
+    }
+  },
+  example = 'local x, y, z = vector(1, 2, 3):unpack()',
+  related = {
+    'vector.pack'
+  }
+}