Tanoda
TransformTranslationHandle.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
10using System.Collections;
11using System.Collections.Generic;
12using UnityEngine;
13
14namespace Leap.Unity.Examples {
15
16 public enum TranslationAxis { X, Y, Z }
17
18 [AddComponentMenu("")]
20
22
23 protected override void Start() {
24 // Populates _intObj with the InteractionBehaviour, and _tool with the
25 // TransformTool.
26 base.Start();
27
28 // Subscribe to OnGraspedMovement; all of the logic will happen when the handle is
29 // moved via grasping.
30 _intObj.OnGraspedMovement += onGraspedMovement;
31 }
32
33 private void onGraspedMovement(Vector3 presolvePos, Quaternion presolveRot,
34 Vector3 solvedPos, Quaternion solvedRot,
35 List<InteractionController> controllers) {
36 /*
37 * OnGraspedMovement provides the position and rotation of the Interaction object
38 * before and after it was moved by its grasping hand. This callback only occurs
39 * when one or more hands is grasping the Interaction object. In this case, we
40 * don't care about how many or which hands are grasping the object, only where
41 * the object is moved.
42 *
43 * The Translation Handle uses the pre- and post-solve movement information to
44 * calculate how the user is trying to move the object along this handle's forward
45 * direction. Then the Translation Handle will simply override the movement caused
46 * by the grasping hand and reset itself back to its original position.
47 *
48 * The movement calculated by the Handle in this method is reported to the Transform
49 * Tool, which accumulates movement caused by all Handles over the course of a frame
50 * and then moves the target object and all of its child Handles appropriately at
51 * the end of the frame.
52 */
53
54 // Calculate the constrained movement of the handle along its forward axis only.
55 Vector3 deltaPos = solvedPos - presolvePos;
56 Vector3 handleForwardDirection = presolveRot * Vector3.forward;
57 Vector3 deltaAxisPos = handleForwardDirection * Vector3.Dot(handleForwardDirection, deltaPos);
58
59 // Notify the tool about the calculated movement.
60 _tool.NotifyHandleMovement(deltaAxisPos);
61
62 // In this case, the tool itself will accumulate delta positions and delta rotations
63 // from all handles, and will then synchronize handles to the appropriate positions and
64 // rotations at the end of the frame.
65
66 // Because the Tool will be the one to actually move this Handle, all we have left to do
67 // is to undo all of the motion caused by the grasping hand.
68 _intObj.rigidbody.position = presolvePos;
69 _intObj.rigidbody.rotation = presolveRot;
70 }
71
72 }
73
74}
void NotifyHandleMovement(Vector3 deltaPosition)
Transform handles call this method to notify the tool that they were used to move the target object.
Rigidbody rigidbody
The Rigidbody associated with this interaction object.
GraspedMovementEvent OnGraspedMovement
Called directly after this grasped object's Rigidbody has had its position and rotation set by its cu...