ConvexShape
EditA 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, similar to 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 their own scale.
Constructors
lovr.physics.newConvexShape | Create a new ConvexShape. |
World:newConvexCollider | Add a Collider with a ConvexShape to the World. |
Methods
Shape:containsPoint | Check if a point is inside the shape. |
Shape:destroy | Destroy the Shape. |
Shape:getAABB | Get the Shape's axis aligned bounding box. |
Shape:getCenterOfMass | Get the center of mass of the Shape. |
Shape:getCollider | Get the Collider the Shape is attached to. |
Shape:getDensity | Get the density of the Shape. |
Shape:getInertia | Get the inertia of the Shape. |
Shape:getMass | Get the mass of the Shape. |
Shape:getOffset | Get the local offset of the Shape. |
Shape:getOrientation | Get the orientation of the Shape. |
Shape:getPose | Get the pose of the Shape. |
Shape:getPosition | Get the position of the Shape. |
Shape:getType | Get the type of the Shape. |
Shape:getUserData | Get the Lua value associated with the Shape. |
Shape:getVolume | Get the volume of the Shape. |
Shape:isDestroyed | Check if the Shape is destroyed. |
Shape:raycast | Cast a ray against the shape. |
Shape:setDensity | Set the density of the Shape. |
Shape:setOffset | Set the local offset of the Shape. |
Shape:setUserData | Associate a Lua value with the Shape. |
ConvexShape:getFace | Get the point indices of one of the faces of the convex hull. |
ConvexShape:getFaceCount | Get the number of faces in the convex hull. |
ConvexShape:getPoint | Get one of the points in the convex hull. |
ConvexShape:getPointCount | Get the number of points in the convex hull. |
ConvexShape:getScale | Get the scale the ConvexShape was created with. |
Example
Drawing a convex hull.
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