Objective:
- Grab an object when the hand is grabbing and near the object
- Change object's position and rotation according to hand's position and rotation
- Release the object when the hand is not grabbing
Easy solution (also the method in the previous version of the project):
- When hand gesture is grab gesture and the hand center is close to the center of an object (within a threshold), attach the object center to the hand center (so maintain same position and rotation).
- When hand gesture releases from grab gesture, break the attachment.
- Pro: easy to implement; the attachment is stable when moving around (object won't slip)
- Con: not intuitive, at init of grabbing, object is like "flying" towards the hand to jump through the threshold distance, and for some object, the center point is empty, so it looks kind of strange.
New method
Changes:
- Use rigidbody collision to detect whether hand touches object, instead of hard-code distance threshold.
- Create "Contact Point" for grabbing (think of it as a grabbing handle but it can appear at any location on the surface of the object upon grabbing and invisible), instead of only use the center to move.
Implementation Steps:
- Add rigidbody collider to every finger and palm of both hand models.
- When hand gesture is grab gesture, and hand collider has collision with an object (use tag to distinguish with other collider like ground and wall), record contact point position, hand position and rotation.
- Create invisible gameobject as "Contact Point", initialize in the contact point position, update its position and rotation to be the same as hand position and rotation.
- Create invisible gameobect "Object Representation" as a child object to "Contact Point", whose initial position and rotation is the same as those of the design object's center upon grabbing. So during grabbing and moving, position and rotation of "Object Representation" will change according to its parent gameobject "Contact Point" and indicate the supposed updated position and rotation of the design object.
- Update position and rotation of design object to be the same as "Object Representation".
- When hand gesture is released from grab gesture, destroy "Contact Point" and "Object Representation", grabbing ends.
- Tricky bug: During grabbing and moving, at contact point, the hand collider has constant collision with the design object which prevent from grabbing. Bug-fix solution: disable hand collider during grabbing, re-enable after grabbing ends.
- Other existing bug info: Unlike gloves, leap-motion-based hand can disappear from the frame, and drop the grabbed object, when hand re-appear at a different position as before, the grabbing "Contact Point" still record the previous position, so it will become distance-grabbing (look like some super power).
Demo video of grabbing an object
In this demo video, the hand moved the stool to a different location and flipped it over, by grabbing at different position on the surface of the stool.