Three.js WebXR Hand Physics (Grab, Throw, Poke)
Natural hand interactions in Three.js, where you pinch to grab objects, release to throw with the measured hand velocity and poke physical buttons with your fingertip, using a small impulse-based sphere physics engine, real XR hands in VR or a mouse-driven hand on desktop.
Key topics: webxr hand physics, hand interaction, grab and throw, poke buttons, vr physics, hand tracking
Game Over
What this code does
- Pinch: thumb-tip to index-tip distance under 30% of palm length counts as a pinch, and the pinch point is their midpoint.
- Grab: pinching within reach of a body attaches it to the pinch point, kinematically, while held.
- Throw: on release the body gets the average velocity of the pinch point over the last 8 frames, the standard trick for natural throws.
- Physics: sphere-approximated bodies with gravity, table and floor contacts (restitution, friction, rolling spin) and pairwise impulse resolution.
- Poke: the index tip pressing a button cap past its travel triggers it on the press edge (spawn, reset, toggle gravity).
- Controls: bounciness, friction, throw strength, auto demo and reset; on desktop move the mouse, hold a button to pinch and use the wheel for depth.
Related Demos
Continue Learning
JavaScript Implementation
// Hand interactions: pinch to grab, release to throw, fingertip to poke
const pinchPoint = thumbTip.clone().lerp(indexTip, 0.5)
const pinching = thumbTip.distanceTo(indexTip) < 0.3 * palmLength
if (pinching && !held) held = bodies.find(b => b.position.distanceTo(pinchPoint) < b.radius + 0.035)
if (held && pinching) held.position.lerp(pinchPoint, 0.6)
if (held && !pinching) { // throw: average recent velocity
held.velocity = history.at(-1).p.clone().sub(history[0].p).divideScalar(history.at(-1).t - history[0].t)
held = null
}
const pressed = indexTip.y < button.top && horizontalDistance(indexTip, button) < button.radius