main.lua 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. --[[ Hand interaction with physics world: use trigger to solidify hand, grip to grab objects
  2. To manipulate objects in world, we create box collider (palm) for each hand controller. This box
  3. is updated to track location of controller.
  4. The naive approach would be to set exact location and orientation of physical collider with values
  5. from hand controller. This results in lousy and unconvincing collisions with other objects, as
  6. physics engine doesn't know the speed of hand colliders at the moment of collision.
  7. An improvement is to set linear and angular speed of kinematic hand colliders so that they
  8. approach the target (actual location/orientation of hand controller). This works well for one
  9. hand, however physics will start to glitch when you try to squeeze an object between two hands.
  10. This is because kinematic hand controllers can never be affected by collision forces, so the
  11. squeezed collider cannot push back against them and the collision cannot be resolved.
  12. The approach taken here is to have hand controllers behave as normal dynamic colliders that can be
  13. affected by other collisions. To track hand controllers, we apply force and torque on collider
  14. objects that's proportional to distance from correct position.
  15. This means hand colliders won't have 1:1 mapping with actual hand controllers, they will actually
  16. 'bend' under large force. Also the colliders can become stuck and buried beneath other objects.
  17. This is frustrating to users, so in this example hand colliders can ghost through objects or
  18. become solid, using the trigger button.
  19. Grabbing objects is done by creating two joints between hand collider and object, to hold them
  20. together. This enables pulling, stacking and throwing. --]]
  21. local hands = { -- palms that can push and grab objects
  22. colliders = {nil, nil}, -- physical objects for palms
  23. touching = {nil, nil}, -- the collider currently touched by each hand
  24. holding = {nil, nil}, -- the collider attached to palm
  25. solid = {false, false}, -- hand can either pass through objects or be solid
  26. } -- to be filled with as many hands as there are active controllers
  27. local world
  28. local collisionCallbacks = {}
  29. local boxes = {}
  30. local hand_torque = 20
  31. local hand_force = 30000
  32. function lovr.load()
  33. world = lovr.physics.newWorld(0, -2, 0, false) -- low gravity and no collider sleeping
  34. -- ground plane
  35. local box = world:newBoxCollider(vec3(0, 0, 0), vec3(20, 0.1, 20))
  36. box:setKinematic(true)
  37. table.insert(boxes, box)
  38. -- create a fort of boxes
  39. lovr.math.setRandomSeed(0)
  40. for angle = 0, 2 * math.pi, 2 * math.pi / 12 do
  41. for height = 0.3, 1.5, 0.4 do
  42. local pose = mat4():rotate(angle, 0,1,0):translate(0, height, -1)
  43. local size = vec3(0.3, 0.4, 0.2)
  44. local box = world:newBoxCollider(vec3(pose), size)
  45. box:setOrientation(quat(pose))
  46. table.insert(boxes, box)
  47. end
  48. end
  49. -- make colliders for two hands
  50. for i = 1, 2 do
  51. hands.colliders[i] = world:newBoxCollider(vec3(0,2,0), vec3(0.04, 0.08, 0.08))
  52. hands.colliders[i]:setLinearDamping(0.7)
  53. hands.colliders[i]:setAngularDamping(0.9)
  54. hands.colliders[i]:setMass(0.5)
  55. registerCollisionCallback(hands.colliders[i],
  56. function(collider, world)
  57. -- store collider that was last touched by hand
  58. hands.touching[i] = collider
  59. end)
  60. end
  61. end
  62. function lovr.update(dt)
  63. -- override collision resolver to notify all colliders that have registered their callbacks
  64. world:update(dt, function(world)
  65. world:computeOverlaps()
  66. for shapeA, shapeB in world:overlaps() do
  67. local areColliding = world:collide(shapeA, shapeB)
  68. if areColliding then
  69. cbA = collisionCallbacks[shapeA]
  70. if cbA then cbA(shapeB:getCollider(), world) end
  71. cbB = collisionCallbacks[shapeB]
  72. if cbB then cbB(shapeA:getCollider(), world) end
  73. end
  74. end
  75. end)
  76. -- hand updates - location, orientation, solidify on trigger button, grab on grip button
  77. for i, hand in pairs(lovr.headset.getHands()) do
  78. -- align collider with controller by applying force (position) and torque (orientation)
  79. local rw = mat4(lovr.headset.getPose(hand)) -- real world pose of controllers
  80. local vr = mat4(hands.colliders[i]:getPose()) -- vr pose of palm colliders
  81. local angle, ax,ay,az = quat(rw):mul(quat(vr):conjugate()):unpack()
  82. angle = ((angle + math.pi) % (2 * math.pi) - math.pi) -- for minimal motion wrap to (-pi, +pi) range
  83. hands.colliders[i]:applyTorque(vec3(ax, ay, az):mul(angle * dt * hand_torque))
  84. hands.colliders[i]:applyForce((vec3(rw) - vec3(vr)):mul(dt * hand_force))
  85. -- solidify when trigger touched
  86. hands.solid[i] = lovr.headset.isDown(hand, 'trigger')
  87. hands.colliders[i]:getShapes()[1]:setSensor(not hands.solid[i])
  88. -- hold/release colliders
  89. if lovr.headset.isDown(hand, 'grip') and hands.touching[i] and not hands.holding[i] then
  90. hands.holding[i] = hands.touching[i]
  91. -- grab object with ball joint to drag it, and slider joint to also match the orientation
  92. lovr.physics.newBallJoint(hands.colliders[i], hands.holding[i], vr:mul(0, 0, 0))
  93. lovr.physics.newSliderJoint(hands.colliders[i], hands.holding[i], quat(vr):direction())
  94. end
  95. if lovr.headset.wasReleased(hand, 'grip') and hands.holding[i] then
  96. for _,joint in ipairs(hands.colliders[i]:getJoints()) do
  97. joint:destroy()
  98. end
  99. hands.holding[i] = nil
  100. end
  101. end
  102. hands.touching = {nil, nil} -- to be set again in collision resolver
  103. end
  104. function lovr.draw(pass)
  105. for i, collider in ipairs(hands.colliders) do
  106. pass:setColor(0.75, 0.56, 0.44)
  107. drawBoxCollider(pass, collider, not hands.solid[i])
  108. end
  109. lovr.math.setRandomSeed(0)
  110. for i, collider in ipairs(boxes) do
  111. local shade = 0.2 + 0.6 * lovr.math.random()
  112. pass:setColor(shade, shade, shade)
  113. drawBoxCollider(pass, collider)
  114. end
  115. end
  116. function drawBoxCollider(pass, collider, is_sensor)
  117. -- query current pose (location and orientation)
  118. local pose = mat4(collider:getPose())
  119. -- query dimensions of box
  120. local shape = collider:getShapes()[1]
  121. local size = vec3(shape:getDimensions())
  122. -- draw box
  123. pose:scale(size)
  124. pass:box(pose, is_sensor and 'line' or 'fill')
  125. end
  126. function registerCollisionCallback(collider, callback)
  127. collisionCallbacks = collisionCallbacks or {}
  128. for _, shape in ipairs(collider:getShapes()) do
  129. collisionCallbacks[shape] = callback
  130. end
  131. -- to be called with arguments callback(otherCollider, world) from update function
  132. end