Tanoda
TransformRotationHandle.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 [AddComponentMenu("")]
18
19 protected override void Start() {
20 // Populates _intObj with the InteractionBehaviour, and _tool with the TransformTool.
21 base.Start();
22
23 // Subscribe to OnGraspedMovement; all of the logic will happen when the handle is moved via grasping.
24 _intObj.OnGraspedMovement += onGraspedMovement;
25 }
26
27 private void onGraspedMovement(Vector3 presolvePos, Quaternion presolveRot, Vector3 solvedPos, Quaternion solvedRot, List<InteractionController> controllers) {
28 /*
29 * The RotationHandle works very similarly to the TranslationHandle.
30 *
31 * We use OnGraspedMovement to get the position and rotation of this object
32 * before and after it was moved by its grapsing hand. We calculate how the handle
33 * would have rotated and report that to the Transform Tool, and then we move
34 * the handle back where it was before it was moved, because the Tool will
35 * actually move all of its handles at the end of the frame.
36 */
37
38 // Constrain the position of the handle and determine the resulting rotation required to get there.
39 Vector3 presolveToolToHandle = presolvePos - _tool.transform.position;
40 Vector3 solvedToolToHandleDirection = (solvedPos - _tool.transform.position).normalized;
41 Vector3 constrainedToolToHandle = Vector3.ProjectOnPlane(solvedToolToHandleDirection, (presolveRot * Vector3.up)).normalized * presolveToolToHandle.magnitude;
42 Quaternion deltaRotation = Quaternion.FromToRotation(presolveToolToHandle, constrainedToolToHandle);
43
44 // Notify the tool about the calculated rotation.
45 _tool.NotifyHandleRotation(deltaRotation);
46
47 // Move the object back to its original position, to be moved correctly later on by the Transform Tool.
48 _intObj.rigidbody.position = presolvePos;
49 _intObj.rigidbody.rotation = presolveRot;
50 }
51
52 }
53
54}
void NotifyHandleRotation(Quaternion deltaRotation)
Transform handles call this method to notify the tool that they were used to rotate 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...