Mercurial > hg > Game > Games
comparison Orchestland/Assets/LeapMotion/Widgets/Scripts/Physics/LeapPhysicsSpring.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 using UnityEngine; | |
2 | |
3 namespace LMWidgets | |
4 { | |
5 /// <summary> | |
6 /// Base class for spring. Restrains the widget in its local z-axis. | |
7 /// It will apply spring physics in ApplyPhysics and translate the button with hand in ApplyInteractions | |
8 /// </summary> | |
9 public abstract class LeapPhysicsSpring : LeapPhysicsBase | |
10 { | |
11 /// <summary> | |
12 /// Spring constant is separated to xyz-axis for more flexible configuration | |
13 /// </summary> | |
14 public Vector3 springCoefficient = Vector3.one * 10.0f; | |
15 public bool applyCriticalDamping = true; | |
16 | |
17 private Vector3 m_dampingCoefficient = Vector3.zero; | |
18 private Vector3 m_interactionConstraints = Vector3.one; | |
19 | |
20 /// <summary> | |
21 /// Applies Interaction constraints. Takes in a Vector3. If an axis has value > 0.5 then it's allowed to move. Otherwise it won't be | |
22 /// </summary> | |
23 /// <param name="interactionConstraints"></param> | |
24 protected void ApplyInteractionConstraints(Vector3 interactionConstraints) | |
25 { | |
26 interactionConstraints.x = (interactionConstraints.x > 0.5f) ? 1.0f : 0.0f; | |
27 interactionConstraints.y = (interactionConstraints.y > 0.5f) ? 1.0f : 0.0f; | |
28 interactionConstraints.z = (interactionConstraints.z > 0.5f) ? 1.0f : 0.0f; | |
29 m_interactionConstraints = interactionConstraints; | |
30 ResetPivots(); | |
31 } | |
32 | |
33 /// <summary> | |
34 /// Apply spring physics | |
35 /// </summary> | |
36 protected override void ApplyPhysics() | |
37 { | |
38 Vector3 springForce = Vector3.Scale(-springCoefficient, transform.localPosition); | |
39 Vector3 dampingForce = Vector3.zero; | |
40 | |
41 if (applyCriticalDamping) | |
42 { | |
43 Vector3 instantVelocity = springForce * Time.deltaTime; | |
44 dampingForce = Vector3.Scale(-m_dampingCoefficient, instantVelocity); | |
45 } | |
46 transform.localPosition += (springForce + dampingForce) * Time.deltaTime; | |
47 } | |
48 | |
49 /// <summary> | |
50 /// Translate the widget with the hand during interaction | |
51 /// </summary> | |
52 protected override void ApplyInteractions() | |
53 { | |
54 Vector3 displacement = Vector3.Scale(transform.parent.InverseTransformPoint(m_target.transform.position) - m_targetPivot, m_interactionConstraints); | |
55 transform.localPosition = displacement + m_pivot; | |
56 } | |
57 | |
58 protected override void Awake() | |
59 { | |
60 base.Awake(); | |
61 Vector3 k = springCoefficient; | |
62 m_dampingCoefficient = 2 * new Vector3(Mathf.Sqrt(k.x), Mathf.Sqrt(k.y), Mathf.Sqrt(k.z)); | |
63 } | |
64 } | |
65 } |