2
0

main.lua 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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(vec3(1, 1.9, -1), vec3(0.1, 0.1, 0.3))
  9. hanger:setKinematic(true)
  10. local trolley = world:newBoxCollider(vec3(-1, 2, -1), vec3(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 = vec3(hanger:getPosition()) - vec3(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(vec3(-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, vec3(trolley:getPosition()), vec3(weight:getPosition()) + vec3(0, 0.3, 0))
  22. joint:setResponseTime(10) -- make the hanging rope streachable
  23. lovr.graphics.setBackgroundColor(0.1, 0.1, 0.1)
  24. end
  25. function lovr.update(dt)
  26. world:update(dt)
  27. end
  28. function lovr.draw(pass)
  29. for i, collider in ipairs(world:getColliders()) do
  30. pass:setColor(0.6, 0.6, 0.6)
  31. local shape = collider:getShapes()[1]
  32. local shapeType = shape:getType()
  33. local x,y,z, angle, ax,ay,az = collider:getPose()
  34. if shapeType == 'box' then
  35. local sx, sy, sz = shape:getDimensions()
  36. pass:box(x,y,z, sx,sy,sz, angle, ax,ay,az)
  37. elseif shapeType == 'capsule' then
  38. pass:setColor(0.4, 0, 0)
  39. local l, r = shape:getLength(), shape:getRadius()
  40. local x,y,z, angle, ax,ay,az = collider:getPose()
  41. pass:capsule(x,y,z, r, l, angle, ax,ay,az)
  42. end
  43. end
  44. end