Browse Source

Add Dragging example;

bjorn 5 years ago
parent
commit
fbf12b7e1e
2 changed files with 47 additions and 0 deletions
  1. 46 0
      examples/Interaction/Dragging/main.lua
  2. 1 0
      examples/init.lua

+ 46 - 0
examples/Interaction/Dragging/main.lua

@@ -0,0 +1,46 @@
+box = {
+  position = lovr.math.newVec3(0, 1, -.25),
+  size = .25
+}
+
+function lovr.load()
+  drag = {
+    active = false,
+    hand = nil,
+    offset = lovr.math.newVec3()
+  }
+end
+
+function lovr.update(dt)
+  for i, hand in ipairs(lovr.headset.getHands()) do
+    if lovr.headset.wasPressed(hand, 'trigger') then
+      local offset = box.position - vec3(lovr.headset.getPosition(hand))
+      local halfSize = box.size / 2
+      local x, y, z = offset:unpack()
+      if math.abs(x) < halfSize and math.abs(y) < halfSize and math.abs(z) < halfSize then
+        drag.active = true
+        drag.hand = hand
+        drag.offset:set(offset)
+      end
+    end
+  end
+
+  if drag.active then
+    local handPosition = vec3(lovr.headset.getPosition(drag.hand))
+    box.position:set(handPosition + drag.offset)
+
+    if lovr.headset.wasReleased(drag.hand, 'trigger') then
+      drag.active = false
+    end
+  end
+end
+
+function lovr.draw()
+  lovr.graphics.setColor(drag.active and 0x80ee80 or 0xee8080)
+  lovr.graphics.cube('line', box.position, box.size)
+
+  for i, hand in ipairs(lovr.headset.getHands()) do
+    lovr.graphics.setColor(0xffffff)
+    lovr.graphics.cube('fill', mat4(lovr.headset.getPose(hand)):scale(.01))
+  end
+end

+ 1 - 0
examples/init.lua

@@ -7,6 +7,7 @@ return {
   'Interaction/Pointer',
   'Interaction/Pointer',
   'Interaction/Pointer_UI',
   'Interaction/Pointer_UI',
   'Interaction/Physics_Pointer',
   'Interaction/Physics_Pointer',
+  'Interaction/Dragging',
   'Interaction/Controller_Models',
   'Interaction/Controller_Models',
   'Interaction/Hand_Tracking',
   'Interaction/Hand_Tracking',
   'Environment/Grid',
   'Environment/Grid',