main.lua 1.3 KB

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