main.lua 1.3 KB

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