main.lua 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. local random = lovr.math.random
  2. local boxes = {}
  3. local selectedBox = nil
  4. local hitpoint = nil
  5. local red = { 1, .5, .5 }
  6. local green = { .5, 1, .5 }
  7. local device = 'hand/left/point'
  8. function lovr.load()
  9. lovr.graphics.setBackgroundColor(.2, .2, .22)
  10. world = lovr.physics.newWorld(0, 0, 0)
  11. -- Make a bunch of random spinning boxes XD
  12. for x = -3, 3 do
  13. for z = 1, 10 do
  14. local y = .5 + lovr.math.randomNormal(.1)
  15. local box = world:newBoxCollider(x, y, -z, .28)
  16. box:setOrientation(random(2 * math.pi), random(), random(), random())
  17. box:setAngularVelocity(random(), random(), random())
  18. table.insert(boxes, box)
  19. end
  20. end
  21. end
  22. function lovr.update(dt)
  23. selectedBox = nil
  24. world:update(dt)
  25. local origin = vector(lovr.headset.getPosition(device))
  26. local direction = vector(lovr.headset.getDirection(device))
  27. local collider, shape, x, y, z = world:raycast(origin, origin + direction * 50)
  28. if collider then
  29. selectedBox = collider
  30. hitpoint = vector(x, y, z)
  31. end
  32. end
  33. function lovr.draw(pass)
  34. -- Boxes
  35. for i, box in ipairs(boxes) do
  36. pass:setColor(box == selectedBox and green or red)
  37. pass:cube(vector(box:getPosition()), .28, box:getOrientation())
  38. end
  39. -- Dot
  40. if selectedBox then
  41. pass:setColor(0, 0, 1)
  42. pass:sphere(hitpoint, .01)
  43. end
  44. -- Laser pointer
  45. local hand = vector(lovr.headset.getPosition(device))
  46. local direction = vector(lovr.headset.getDirection(device))
  47. pass:setColor(1, 1, 1)
  48. pass:line(hand, selectedBox and hitpoint or (hand + direction * 50))
  49. end