main.lua 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. -- Left thumbstick: move and strafe
  2. -- Right thumbstick: rotate the view horizontally
  3. -- Right grip: switch to snap locomotion mode
  4. -- Left grip: enable flying mode
  5. -- Left trigger: switch orientation to left hand controller (can look around while moving)
  6. local motion = {
  7. pose = lovr.math.newMat4(), -- Transformation in VR initialized to origin (0,0,0) looking down -Z
  8. thumbstickDeadzone = 0.4, -- Smaller thumbstick displacements are ignored (too much noise)
  9. directionFrom = 'head', -- Movement can be relative to orientation of head or left controller
  10. flying = false,
  11. -- Snap motion parameters
  12. snapTurnAngle = 2 * math.pi / 12,
  13. dashDistance = 1.5,
  14. thumbstickCooldownTime = 0.3,
  15. thumbstickCooldown = 0,
  16. -- Smooth motion parameters
  17. turningSpeed = 2 * math.pi * 1 / 6,
  18. walkingSpeed = 4,
  19. }
  20. lovr.graphics.setBackgroundColor(0.1, 0.1, 0.1)
  21. function motion.smooth(dt)
  22. if lovr.headset.isTracked('right') then
  23. local x, y = lovr.headset.getAxis('right', 'thumbstick')
  24. -- Smooth horizontal turning
  25. if math.abs(x) > motion.thumbstickDeadzone then
  26. motion.pose:rotate(-x * motion.turningSpeed * dt, 0, 1, 0)
  27. end
  28. end
  29. if lovr.headset.isTracked('left') then
  30. local x, y = lovr.headset.getAxis('left', 'thumbstick')
  31. local direction = quat(lovr.headset.getOrientation(motion.directionFrom)):direction()
  32. if not motion.flying then
  33. direction.y = 0
  34. end
  35. -- Smooth strafe movement
  36. if math.abs(x) > motion.thumbstickDeadzone then
  37. local strafeVector = quat(-math.pi / 2, 0,1,0):mul(vec3(direction))
  38. motion.pose:translate(strafeVector * x * motion.walkingSpeed * dt)
  39. end
  40. -- Smooth Forward/backward movement
  41. if math.abs(y) > motion.thumbstickDeadzone then
  42. motion.pose:translate(direction * y * motion.walkingSpeed * dt)
  43. end
  44. end
  45. end
  46. function motion.snap(dt)
  47. -- Snap horizontal turning
  48. if lovr.headset.isTracked('right') then
  49. local x, y = lovr.headset.getAxis('right', 'thumbstick')
  50. if math.abs(x) > motion.thumbstickDeadzone and motion.thumbstickCooldown < 0 then
  51. local angle = -x / math.abs(x) * motion.snapTurnAngle
  52. motion.pose:rotate(angle, 0, 1, 0)
  53. motion.thumbstickCooldown = motion.thumbstickCooldownTime
  54. end
  55. end
  56. -- Dashing forward/backward
  57. if lovr.headset.isTracked('left') then
  58. local x, y = lovr.headset.getAxis('left', 'thumbstick')
  59. if math.abs(y) > motion.thumbstickDeadzone and motion.thumbstickCooldown < 0 then
  60. local moveVector = quat(lovr.headset.getOrientation('head')):direction()
  61. if not motion.flying then
  62. moveVector.y = 0
  63. end
  64. moveVector:mul(y / math.abs(y) * motion.dashDistance)
  65. motion.pose:translate(moveVector)
  66. motion.thumbstickCooldown = motion.thumbstickCooldownTime
  67. end
  68. end
  69. motion.thumbstickCooldown = motion.thumbstickCooldown - dt
  70. end
  71. function lovr.update(dt)
  72. motion.directionFrom = lovr.headset.isDown('left', 'trigger') and 'left' or 'head'
  73. if lovr.headset.isDown('left', 'grip') then
  74. motion.flying = true
  75. elseif lovr.headset.wasReleased('left', 'grip') then
  76. motion.flying = false
  77. local height = vec3(motion.pose).y
  78. motion.pose:translate(0, -height, 0)
  79. end
  80. if lovr.headset.isDown('right', 'grip') then
  81. motion.snap(dt)
  82. else
  83. motion.smooth(dt)
  84. end
  85. end
  86. function lovr.draw(pass)
  87. pass:transform(mat4(motion.pose):invert())
  88. -- Render hands
  89. pass:setColor(1,1,1)
  90. local radius = 0.05
  91. for _, hand in ipairs(lovr.headset.getHands()) do
  92. -- Whenever pose of hand or head is used, need to account for VR movement
  93. local poseRW = mat4(lovr.headset.getPose(hand))
  94. local poseVR = mat4(motion.pose):mul(poseRW)
  95. poseVR:scale(radius)
  96. pass:sphere(poseVR)
  97. end
  98. -- Some scenery
  99. lovr.math.setRandomSeed(0)
  100. local goldenRatio = (math.sqrt(5) + 1) / 2
  101. local goldenAngle = (2 - goldenRatio) * (2 * math.pi)
  102. local k = 1.8
  103. for i = 1, 500 do
  104. local r = math.sqrt(i) * k
  105. local x = math.cos(goldenAngle * i) * r
  106. local y = math.sin(goldenAngle * i) * r
  107. if lovr.math.random() < 0.05 then
  108. pass:setColor(0.5, 0, 0)
  109. else
  110. local shade = 0.1 + 0.3 * lovr.math.random()
  111. pass:setColor(shade, shade, shade)
  112. end
  113. pass:cylinder(x, 0, y, 1,0.05, math.pi / 2, 1,0,0)
  114. end
  115. end