main.lua 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. pointer = require 'pointer'
  2. local function drawBox(box, color)
  3. local x, y, z = box:getPosition()
  4. local dx, dy, dz = box:getShapeList()[1]:getDimensions()
  5. lovr.graphics.setColor(color)
  6. lovr.graphics.box('fill', x, y, z, dx, dy, dz, box:getOrientation())
  7. end
  8. local function refreshSource()
  9. pointer:setSource(lovr.headset.getControllers()[1] or lovr.headset)
  10. end
  11. function lovr.load()
  12. -- Make a little physics scene
  13. world = lovr.physics.newWorld()
  14. ground = world:newBoxCollider(0, 0, 0, 5, .001, 5)
  15. ground:setKinematic(true)
  16. box = world:newBoxCollider(0, 1, 0, .5)
  17. -- Initialize the pointer
  18. pointer:init({ source = lovr.headset, world = world })
  19. end
  20. function lovr.update(dt)
  21. pointer:update()
  22. world:update(dt)
  23. end
  24. function lovr.draw()
  25. local hit = pointer:getHit()
  26. drawBox(ground, { 30, 30, 30 })
  27. -- Highlight that darn box if we're pointing at it
  28. local boxColor = (hit and hit.collider == box) and { 50, 100, 200 } or { 20, 70, 170 }
  29. drawBox(box, boxColor)
  30. lovr.graphics.setColor(255, 255, 255)
  31. -- If we're pointing with a controller, draw a line showing the path
  32. if pointer:getSource() ~= lovr.headset then
  33. lovr.graphics.line(pointer:getPath())
  34. end
  35. -- If we're pointing at something, draw the intersection point and normal vector
  36. if hit then
  37. lovr.graphics.cube('fill', hit.x, hit.y, hit.z, .04)
  38. lovr.graphics.setColor(0, 255, 0)
  39. lovr.graphics.line(hit.x, hit.y, hit.z, hit.x + hit.nx * .1, hit.y + hit.ny * .1, hit.z + hit.nz * .1)
  40. end
  41. end
  42. lovr.controlleradded = refreshSource
  43. lovr.controllerremoved = refreshSource