Mercurial > hg > Game > Games
comparison Orchestland/Assets/LeapMotion/Scripts/Hands/RigidHand.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 // The model for our rigid hand made out of various polyhedra. | |
12 public class RigidHand : SkeletalHand { | |
13 | |
14 public float filtering = 0.5f; | |
15 | |
16 public override void InitHand() { | |
17 base.InitHand(); | |
18 } | |
19 | |
20 public override void UpdateHand() { | |
21 for (int f = 0; f < fingers.Length; ++f) { | |
22 if (fingers[f] != null) | |
23 fingers[f].UpdateFinger(); | |
24 } | |
25 | |
26 if (palm != null) { | |
27 bool useVelocity = false; | |
28 Rigidbody palmBody = palm.GetComponent<Rigidbody>(); | |
29 if (palmBody) { | |
30 if (!palmBody.isKinematic) { | |
31 useVelocity = true; | |
32 | |
33 // Set palm velocity. | |
34 Vector3 target_position = GetPalmCenter(); | |
35 palmBody.velocity = (target_position - palm.position) * (1 - filtering) / Time.deltaTime; | |
36 | |
37 // Set palm angular velocity. | |
38 Quaternion target_rotation = GetPalmRotation(); | |
39 Quaternion delta_rotation = target_rotation * Quaternion.Inverse(palm.rotation); | |
40 float angle = 0.0f; | |
41 Vector3 axis = Vector3.zero; | |
42 delta_rotation.ToAngleAxis(out angle, out axis); | |
43 | |
44 if (angle >= 180) { | |
45 angle = 360 - angle; | |
46 axis = -axis; | |
47 } | |
48 if (angle != 0) { | |
49 float delta_radians = (1 - filtering) * angle * Mathf.Deg2Rad; | |
50 palmBody.angularVelocity = delta_radians * axis / Time.deltaTime; | |
51 } | |
52 } | |
53 } | |
54 if (!useVelocity) { | |
55 palm.position = GetPalmCenter(); | |
56 palm.rotation = GetPalmRotation(); | |
57 } | |
58 } | |
59 | |
60 if (forearm != null) { | |
61 // Set arm dimensions. | |
62 CapsuleCollider capsule = forearm.GetComponent<CapsuleCollider> (); | |
63 if (capsule != null) { | |
64 // Initialization | |
65 capsule.direction = 2; | |
66 forearm.localScale = new Vector3 (1f, 1f, 1f); | |
67 | |
68 // Update | |
69 capsule.radius = GetArmWidth () / 2f; | |
70 capsule.height = GetArmLength () + GetArmWidth (); | |
71 } | |
72 | |
73 bool useVelocity = false; | |
74 Rigidbody forearmBody = forearm.GetComponent<Rigidbody> (); | |
75 if (forearmBody) { | |
76 if (!forearmBody.isKinematic) { | |
77 useVelocity = true; | |
78 | |
79 // Set arm velocity. | |
80 Vector3 target_position = GetArmCenter (); | |
81 forearmBody.velocity = (target_position - forearm.position) * (1 - filtering) / Time.deltaTime; | |
82 | |
83 // Set arm velocity. | |
84 Quaternion target_rotation = GetArmRotation (); | |
85 Quaternion delta_rotation = target_rotation * Quaternion.Inverse (forearm.rotation); | |
86 float angle = 0.0f; | |
87 Vector3 axis = Vector3.zero; | |
88 delta_rotation.ToAngleAxis (out angle, out axis); | |
89 | |
90 if (angle >= 180) { | |
91 angle = 360 - angle; | |
92 axis = -axis; | |
93 } | |
94 if (angle != 0) { | |
95 float delta_radians = (1 - filtering) * angle * Mathf.Deg2Rad; | |
96 forearmBody.angularVelocity = delta_radians * axis / Time.deltaTime; | |
97 } | |
98 } | |
99 } | |
100 if (!useVelocity) { | |
101 forearm.position = GetArmCenter(); | |
102 forearm.rotation = GetArmRotation(); | |
103 } | |
104 } | |
105 } | |
106 } |