main.lua 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. shader = require 'shader'
  2. function lovr.load()
  3. world = lovr.physics.newWorld()
  4. world:setLinearDamping(.01)
  5. world:setAngularDamping(.005)
  6. -- Create the ground
  7. world:newBoxCollider(0, 0, 0, 50, .05, 50):setKinematic(true)
  8. -- Create boxes!
  9. boxes = {}
  10. for x = -1, 1, .25 do
  11. for y = .125, 2, .24999 do
  12. local box = world:newBoxCollider(x, y, -2 - y / 5, .25)
  13. table.insert(boxes, box)
  14. end
  15. end
  16. controllerBoxes = {}
  17. lovr.timer.step() -- Reset the timer before the first update
  18. end
  19. function lovr.update(dt)
  20. -- Update the physics simulation
  21. world:update(dt)
  22. -- Place boxes on controllers
  23. for i, controller in ipairs(lovr.headset.getControllers()) do
  24. if not controllerBoxes[i] then
  25. controllerBoxes[i] = world:newBoxCollider(0, 0, 0, .25)
  26. controllerBoxes[i]:setKinematic(true)
  27. controllerBoxes[i]:setMass(10)
  28. end
  29. controllerBoxes[i]:setPosition(controller:getPosition())
  30. controllerBoxes[i]:setOrientation(controller:getOrientation())
  31. end
  32. end
  33. -- A helper function for drawing boxes
  34. function drawBox(box)
  35. local x, y, z = box:getPosition()
  36. lovr.graphics.cube('fill', x, y, z, .25, box:getOrientation())
  37. end
  38. function lovr.draw()
  39. lovr.graphics.setBackgroundColor(.8, .8, .8)
  40. lovr.graphics.setShader(shader)
  41. lovr.graphics.setColor(1, 0, 0)
  42. for i, box in ipairs(boxes) do
  43. drawBox(box)
  44. end
  45. lovr.graphics.setColor(0, 0, 1)
  46. for i, box in ipairs(controllerBoxes) do
  47. drawBox(box)
  48. end
  49. end