Browse Source

Document ConvexShape better;

bjorn 5 months ago
parent
commit
ed2cb133ec
2 changed files with 79 additions and 5 deletions
  1. 12 2
      api/init.lua
  2. 67 3
      api/lovr/physics/ConvexShape/init.lua

+ 12 - 2
api/init.lua

@@ -38282,10 +38282,20 @@ return {
         },
         {
           name = "ConvexShape",
-          summary = "TODO",
-          description = "TODO",
+          summary = "A convex hull shape.",
+          description = "A type of `Shape` that is a convex hull of a collection of points, allowing for custom collision shapes.  It is similar to a `MeshShape`, but it is not required to be kinematic, and it will use the convex hull of the mesh instead of using the exact triangles of the object.\n\nConvex shapes can be created from a `Model`, `ModelData`, `Mesh`, or a table of point positions, just like `MeshShape`.\n\nConvex shapes can be cloned by passing in an existing ConvexShape to clone:\n\n    model = lovr.data.newModelData('rock.glb')\n    parent = lovr.physics.newConvexShape(model)\n    clone = lovr.physics.newConvexShape(parent, scale)\n\nThe clone will reuse all of the data from the parent, which speeds things up a lot.\n\nConvex shapes can have a custom scale applied to their points, and clones can have a different scale than their parents.",
           key = "ConvexShape",
           module = "lovr.physics",
+          constructors = {
+            "lovr.physics.newConvexShape",
+            "World:newConvexCollider"
+          },
+          examples = {
+            {
+              description = "Drawing a convex hull.",
+              code = "function lovr.load()\n  model = lovr.graphics.newModel('eggplant.glb')\n  hull = lovr.physics.newConvexShape(model)\n\n  -- Each face will be a list of points to draw a line through\n  faces = {}\n\n  for f = 1, hull:getFaceCount() do\n    local face = {}\n\n    for _, pointindex in ipairs(hull:getFace(f)) do\n      local x, y, z = hull:getPoint(pointindex)\n      table.insert(face, x)\n      table.insert(face, y)\n      table.insert(face, z)\n    end\n\n    -- Connect the last point back to the first point\n    table.insert(face, face[1])\n    table.insert(face, face[2])\n    table.insert(face, face[3])\n\n    table.insert(faces, face)\n  end\nend\n\nfunction lovr.draw(pass)\n  pass:push()\n  pass:translate(0, 0, -5)\n  pass:draw(model)\n\n  pass:setColor(1, 0, 0)\n  for i, points in pairs(faces) do\n    pass:line(points)\n  end\n  pass:pop()\nend"
+            }
+          },
           extends = "Shape",
           methods = {
             {

+ 67 - 3
api/lovr/physics/ConvexShape/init.lua

@@ -1,5 +1,69 @@
 return {
-  summary = 'TODO',
-  description = 'TODO',
-  extends = 'Shape'
+  summary = 'A convex hull shape.',
+  description = [[
+    A type of `Shape` that is a convex hull of a collection of points, allowing for custom collision
+    shapes.  It is similar to a `MeshShape`, but it is not required to be kinematic, and it will use
+    the convex hull of the mesh instead of using the exact triangles of the object.
+
+    Convex shapes can be created from a `Model`, `ModelData`, `Mesh`, or a table of point positions,
+    just like `MeshShape`.
+
+    Convex shapes can be cloned by passing in an existing ConvexShape to clone:
+
+        model = lovr.data.newModelData('rock.glb')
+        parent = lovr.physics.newConvexShape(model)
+        clone = lovr.physics.newConvexShape(parent, scale)
+
+    The clone will reuse all of the data from the parent, which speeds things up a lot.
+
+    Convex shapes can have a custom scale applied to their points, and clones can have a different
+    scale than their parents.
+  ]],
+  extends = 'Shape',
+  constructors = {
+    'lovr.physics.newConvexShape',
+    'World:newConvexCollider'
+  },
+  example = {
+    description = 'Drawing a convex hull.',
+    code = [[
+      function lovr.load()
+        model = lovr.graphics.newModel('eggplant.glb')
+        hull = lovr.physics.newConvexShape(model)
+
+        -- Each face will be a list of points to draw a line through
+        faces = {}
+
+        for f = 1, hull:getFaceCount() do
+          local face = {}
+
+          for _, pointindex in ipairs(hull:getFace(f)) do
+            local x, y, z = hull:getPoint(pointindex)
+            table.insert(face, x)
+            table.insert(face, y)
+            table.insert(face, z)
+          end
+
+          -- Connect the last point back to the first point
+          table.insert(face, face[1])
+          table.insert(face, face[2])
+          table.insert(face, face[3])
+
+          table.insert(faces, face)
+        end
+      end
+
+      function lovr.draw(pass)
+        pass:push()
+        pass:translate(0, 0, -5)
+        pass:draw(model)
+
+        pass:setColor(1, 0, 0)
+        for i, points in pairs(faces) do
+          pass:line(points)
+        end
+        pass:pop()
+      end
+    ]]
+  }
 }