Skip to content

Commit

Permalink
Revise Drag & drop in VR with LÖVR
Browse files Browse the repository at this point in the history
  • Loading branch information
micouy committed Dec 2, 2024
1 parent 9750d44 commit 6ca44f7
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions content/drag-and-drop-in-vr-with-lovr.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ LÖVR exposes a very simple API for tracking both hands and controllers. The who
We can start by copying the [Tracked Hands](https://lovr.org/docs/Intro/Tracked_Hands) example:


# TODO: check if getHands() works with controllers

```lua
function lovr.draw(pass)
for i, hand in ipairs(lovr.headset.getHands()) do
Expand All @@ -54,7 +52,7 @@ end
If you have trouble with hand tracking, you can switch to controllers without changing the code.
</small>

After launching it on your headset, you'll see two 10 centimeters white spheres following your hands positions.
After launching it on your headset, you'll see two white spheres following your hands positions.

To study what exactly the code does, you can check out these pages in the docs:

Expand Down Expand Up @@ -126,7 +124,7 @@ If the vector is used only within one frame (it isn't assigned to a variable use
If you use a temporary vector during another frame, you'll get an error saying `'Attempt to use a temporary vector from a previous frame'`.
</details>

No we need to detect whether the hand was touching the cube while the grab event fired.
Now we need to detect whether the hand was touching the cube while the grab event fired.

We'll add an `isActive` property to each box...

Expand Down Expand Up @@ -172,7 +170,8 @@ function lovr.draw(pass)
end
end

-- ...
pass:setColor(0xffffff)
pass:sphere(handPosition, .1)
end
end
```
Expand Down Expand Up @@ -336,7 +335,13 @@ end
```lua
function lovr.draw(pass)
for i, hand in ipairs(lovr.headset.getHands()) do
-- ...
local handPosition = vec3(lovr.headset.getPosition(hand))

local wasPressed = lovr.headset.wasPressed(hand, 'trigger')

if wasPressed then
-- ...
end

local wasReleased = lovr.headset.wasReleased(hand, 'trigger')

Expand All @@ -358,7 +363,17 @@ function lovr.draw(pass)
for i, hand in ipairs(lovr.headset.getHands()) do
local handPosition = vec3(lovr.headset.getPosition(hand))

-- ...
local wasPressed = lovr.headset.wasPressed(hand, 'trigger')

if wasPressed then
-- ...
end

local wasReleased = lovr.headset.wasReleased(hand, 'trigger')

if wasReleased then
grabbedBoxes[hand] = nil
end

local grabbedBox = grabbedBoxes[hand]

Expand All @@ -377,7 +392,9 @@ To highlight a grabbed box, we'll change its color:

```lua
function lovr.draw(pass)
-- ...
for i, hand in ipairs(lovr.headset.getHands()) do
-- ...
end

for _, box in ipairs(boxes) do
local isGrabbed = (
Expand Down

0 comments on commit 6ca44f7

Please sign in to comment.