Mercurial > hg > Game > Games
comparison Orchestland/Assets/LeapMotion/Scripts/Tools/ToolModel.cs @ 1:f7675884f2a1
Add Orchestland project
author | Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp> |
---|---|
date | Fri, 17 Jul 2015 23:09:20 +0900 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
0:347d21cdfc22 | 1:f7675884f2a1 |
---|---|
1 /******************************************************************************\ | |
2 * Copyright (C) Leap Motion, Inc. 2011-2014. * | |
3 * Leap Motion proprietary. Licensed under Apache 2.0 * | |
4 * Available at http://www.apache.org/licenses/LICENSE-2.0.html * | |
5 \******************************************************************************/ | |
6 | |
7 using UnityEngine; | |
8 using System.Collections; | |
9 using Leap; | |
10 | |
11 // NOTE: This file is a work in progress, changes to come. | |
12 /** | |
13 * Updates a tool model based on tracking data from the Leap Motion service/daemon. | |
14 * In the Leap Motion API, tools represent long, thin objects, like a pencil, which | |
15 * can be tracked by the Leap Motion sensor. Tools are not associated with hands. | |
16 * | |
17 * A GameObject representing the tool graphics and with this script attached can be added | |
18 * to a HandController's ToolModel slot. The HandController will spawn instances of the game object, | |
19 * updates its position during its lifetime, and finally, destroy the spawned instance when | |
20 * tracking of the tool is lost. | |
21 */ | |
22 public class ToolModel : MonoBehaviour { | |
23 | |
24 /** Smoothing factor applied to movement.*/ | |
25 public float filtering = 0.5f; | |
26 | |
27 protected Tool tool_; | |
28 protected HandController controller_; | |
29 protected bool mirror_z_axis_ = false; | |
30 | |
31 /** The Leap Tool object. */ | |
32 public Tool GetLeapTool() { | |
33 return tool_; | |
34 } | |
35 | |
36 /** Sets the Leap Tool for this ToolModel. */ | |
37 public void SetLeapTool(Tool tool) { | |
38 tool_ = tool; | |
39 } | |
40 | |
41 /** Whether to mirror the tool and motion. */ | |
42 public void MirrorZAxis(bool mirror = true) { | |
43 mirror_z_axis_ = mirror; | |
44 } | |
45 | |
46 /** The Leap Controller object providing tracking data. */ | |
47 public HandController GetController() { | |
48 return controller_; | |
49 } | |
50 | |
51 /** Sets the Leap Controller for this ToolModel. */ | |
52 public void SetController(HandController controller) { | |
53 controller_ = controller; | |
54 } | |
55 | |
56 /** The local rotation of this tool based on the tracked tool, the HandController, and the mirror setting.*/ | |
57 public Quaternion GetToolRotation() { | |
58 Quaternion local_rotation = Quaternion.FromToRotation(Vector3.forward, | |
59 tool_.Direction.ToUnity(mirror_z_axis_)); | |
60 return GetController().transform.rotation * local_rotation; | |
61 } | |
62 | |
63 /** Calculates the tip velocity of this tool model within the scene. */ | |
64 public Vector3 GetToolTipVelocity() { | |
65 Vector3 local_velocity = tool_.TipVelocity.ToUnityScaled(mirror_z_axis_); | |
66 Vector3 total_scale = Vector3.Scale(GetController().handMovementScale, | |
67 GetController().transform.localScale); | |
68 Vector3 scaled_velocity = Vector3.Scale(total_scale, local_velocity); | |
69 return GetController().transform.TransformDirection(scaled_velocity); | |
70 } | |
71 | |
72 /** The position of the tip of this tool in the Unity scene. */ | |
73 public Vector3 GetToolTipPosition() { | |
74 Vector3 local_point = tool_.TipPosition.ToUnityScaled(mirror_z_axis_); | |
75 Vector3 scaled_point = Vector3.Scale(GetController().handMovementScale, local_point); | |
76 return GetController().transform.TransformPoint(scaled_point); | |
77 } | |
78 | |
79 /** Initalizes the tool by setting its position and orientation. */ | |
80 public void InitTool() { | |
81 transform.position = GetToolTipPosition(); | |
82 transform.rotation = GetToolRotation(); | |
83 } | |
84 | |
85 /** Updates the tool by setting its position, velocity, and orientation. */ | |
86 public void UpdateTool() { | |
87 Vector3 target_position = GetToolTipPosition(); | |
88 if (Time.deltaTime != 0) { | |
89 GetComponent<Rigidbody>().velocity = (target_position - transform.position) * | |
90 (1 - filtering) / Time.deltaTime; | |
91 } | |
92 | |
93 // Set angular velocity. | |
94 Quaternion target_rotation = GetToolRotation(); | |
95 Quaternion delta_rotation = target_rotation * | |
96 Quaternion.Inverse(transform.rotation); | |
97 float angle = 0.0f; | |
98 Vector3 axis = Vector3.zero; | |
99 delta_rotation.ToAngleAxis(out angle, out axis); | |
100 | |
101 if (angle >= 180) { | |
102 angle = 360 - angle; | |
103 axis = -axis; | |
104 } | |
105 if (angle != 0) | |
106 GetComponent<Rigidbody>().angularVelocity = (1 - filtering) * angle * axis; | |
107 } | |
108 } |