main.lua 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. --[[ A zipline demo combining several joint types
  2. Capsule is suspended from trolley using distance joint. Trolley is attached to hanger
  3. using slider joint. Beware that slider joint loses its accuracy/stability when attached
  4. objects are too far away. Increasing object mass of helps with stability. --]]
  5. local world
  6. function lovr.load()
  7. world = lovr.physics.newWorld(0, -3, 0, false)
  8. local hanger = world:newBoxCollider(vector(1, 1.9, -1), vector(0.1, 0.1, 0.3))
  9. hanger:setKinematic(true)
  10. local trolley = world:newBoxCollider(vector(-1, 2, -1), vector(0.2, 0.2, 0.5))
  11. trolley:setRestitution(0.7)
  12. -- calculate axis that passes through centers of hanger and trolley
  13. local sliderAxis = vector(hanger:getPosition()) - vector(trolley:getPosition())
  14. -- constraint the trolley so that it can only slide along specified axis without any rotation
  15. joint = lovr.physics.newSliderJoint(hanger, trolley, sliderAxis)
  16. -- hang a weight from trolley
  17. local weight = world:newCapsuleCollider(vector(-1, 1.5, -1), 0.1, 0.4)
  18. weight:setOrientation(math.pi/2, 1,0,0)
  19. weight:setLinearDamping(0.005)
  20. weight:setAngularDamping(0.01)
  21. local joint = lovr.physics.newDistanceJoint(trolley, weight, vector(trolley:getPosition()), vector(weight:getPosition()) + vector(0, 0.3, 0))
  22. lovr.graphics.setBackgroundColor(0.1, 0.1, 0.1)
  23. end
  24. function lovr.update(dt)
  25. world:update(dt)
  26. end
  27. function lovr.draw(pass)
  28. for i, collider in ipairs(world:getColliders()) do
  29. pass:setColor(0.6, 0.6, 0.6)
  30. local shape = collider:getShapes()[1]
  31. local shapeType = shape:getType()
  32. local x,y,z, angle, ax,ay,az = collider:getPose()
  33. if shapeType == 'box' then
  34. local sx, sy, sz = shape:getDimensions()
  35. pass:box(x,y,z, sx,sy,sz, angle, ax,ay,az)
  36. elseif shapeType == 'capsule' then
  37. pass:setColor(0.4, 0, 0)
  38. local l, r = shape:getLength(), shape:getRadius()
  39. local x,y,z, angle, ax,ay,az = collider:getPose()
  40. pass:capsule(x,y,z, r, l, angle, ax,ay,az)
  41. end
  42. end
  43. end