Mercurial > hg > Game > Games
comparison Orchestland/Assets/LeapMotion/Scripts/Hands/RigidFinger.cs @ 3:0030a1b971fb default tip
merge
author | Yuta ANSE <e135745@ie.u-ryukyu.ac.jp> |
---|---|
date | Fri, 17 Jul 2015 23:23:43 +0900 |
parents | f7675884f2a1 |
children |
comparison
equal
deleted
inserted
replaced
2:fdab88fc2cb9 | 3:0030a1b971fb |
---|---|
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 finger model for our rigid hand made out of various cubes. | |
12 public class RigidFinger : SkeletalFinger { | |
13 | |
14 public float filtering = 0.5f; | |
15 | |
16 void Start() { | |
17 for (int i = 0; i < bones.Length; ++i) { | |
18 if (bones[i] != null) { | |
19 bones[i].GetComponent<Rigidbody>().maxAngularVelocity = Mathf.Infinity; | |
20 } | |
21 } | |
22 } | |
23 | |
24 public override void UpdateFinger() { | |
25 for (int i = 0; i < bones.Length; ++i) { | |
26 if (bones[i] != null) { | |
27 // Set bone dimensions. | |
28 CapsuleCollider capsule = bones[i].GetComponent<CapsuleCollider>(); | |
29 if (capsule != null) | |
30 { | |
31 // Initialization | |
32 capsule.direction = 2; | |
33 bones[i].localScale = new Vector3(1f, 1f, 1f); | |
34 | |
35 // Update | |
36 capsule.radius = GetBoneWidth(i) / 2f; | |
37 capsule.height = GetBoneLength(i) + GetBoneWidth(i); | |
38 } | |
39 | |
40 bool useVelocity = false; | |
41 Rigidbody boneBody = bones[i].GetComponent<Rigidbody>(); | |
42 if (boneBody) { | |
43 if (!boneBody.isKinematic) { | |
44 useVelocity = true; | |
45 | |
46 // Set velocity. | |
47 Vector3 target_bone_position = GetBoneCenter(i); | |
48 | |
49 bones[i].GetComponent<Rigidbody>().velocity = (target_bone_position - bones[i].position) * ((1 - filtering) / Time.deltaTime); | |
50 | |
51 // Set angular velocity. | |
52 Quaternion target_rotation = GetBoneRotation(i); | |
53 Quaternion delta_rotation = target_rotation * Quaternion.Inverse(bones[i].rotation); | |
54 float angle = 0.0f; | |
55 Vector3 axis = Vector3.zero; | |
56 delta_rotation.ToAngleAxis(out angle, out axis); | |
57 | |
58 if (angle >= 180) { | |
59 angle = 360 - angle; | |
60 axis = -axis; | |
61 } | |
62 | |
63 if (angle != 0) { | |
64 float delta_radians = (1 - filtering) * angle * Mathf.Deg2Rad; | |
65 bones[i].GetComponent<Rigidbody>().angularVelocity = delta_radians * axis / Time.deltaTime; | |
66 } | |
67 } | |
68 } | |
69 if (!useVelocity) { | |
70 bones[i].position = GetBoneCenter(i); | |
71 bones[i].rotation = GetBoneRotation(i); | |
72 } | |
73 } | |
74 } | |
75 } | |
76 } |