annotate Orchestland/Assets/LeapMotion/Widgets/Scripts/Scroll/ScrollBase.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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 ´╗┐using UnityEngine;
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
2 using System.Collections;
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
3 using LMWidgets;
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
4
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
5 public class ScrollBase : LeapPhysicsBase {
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
6 public float InteractionScale = 1.0f;
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
7
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
8 // How strong is the snapping spring.
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 public float SnapSpringForce = 100.0f;
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
10
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
11 // How much drag should be applied ( as a percentage of velocity/second )
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
12 public float Drag = 5.0f;
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
13
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
14 // The transform of the root game object of the content to be scrolled.
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
15 public Transform ContentTransform;
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
16
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
17 // Used to define and top and bottom of the scroll content
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
18 public Transform ContentTopBound;
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
19 public Transform ContentBottomBound;
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
20
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
21 // Used to define the top and bottom of the scroll container
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
22 public Transform ContainerTopBound;
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
23 public Transform ContainerBottomBound;
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
24
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
25 // The current velocity of the content.
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
26 protected float m_velocity = 0.0f;
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
27
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
28 // The dampening force for the edge spring.
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
29 protected float m_dampingForce = 0.0f;
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
30
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
31 // The location of the scrollable content
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
32 // when interaction began.
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
33 protected Vector3 m_contentPivot;
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
34
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
35
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
36 protected virtual void Start () {
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
37 // Set critical dampening force to avoid oscillation.
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
38 m_dampingForce = Mathf.Sqrt(4.0f * SnapSpringForce);
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
39 }
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
40
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
41 protected override void ResetPivots() {
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
42 base.ResetPivots();
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
43 m_contentPivot = ContentTransform.localPosition;
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
44 }
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
45
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
46 protected override void ApplyPhysics() {
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
47 applyOverrunSpringForces();
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
48 applyDrag();
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
49 applyVelocity();
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
50 }
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
51
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
52 protected void applyDrag() {
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
53 m_velocity -= m_velocity * Mathf.Max(0, Drag * Time.deltaTime);
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
54 }
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
55
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
56 protected void applyVelocity() {
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
57 Vector3 currentPosition = ContentTransform.localPosition;
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
58 currentPosition.y += m_velocity * Time.deltaTime;
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
59 ContentTransform.localPosition = currentPosition;
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
60 }
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
61
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
62 /// <summary>
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
63 /// Applies a spring force to velocity to return content to scroller bounds.
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
64 /// </summary>
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
65 protected void applyOverrunSpringForces() {
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
66 float overrunDistance = calculateOverrunMagnitude() * InteractionScale;
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
67
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
68 if (overrunDistance != 0.0f) {
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
69 float springForce = calculate1DSpringForce(overrunDistance);
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
70 m_velocity += springForce * Time.deltaTime;
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
71 }
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
72 }
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
73
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
74 // offsetVector is the vector by which the object is offset from the goal
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
75 protected float calculate1DSpringForce(float offsetVector) {
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
76 float springForce = offsetVector * SnapSpringForce;
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
77 float dampingForce = m_dampingForce * (m_velocity);
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
78 return springForce - dampingForce;
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
79 }
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
80
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
81 // calc the amount and direction (if any) the content has overrun the scroll container.
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
82 protected float calculateOverrunMagnitude() {
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
83 float overrunDistance = 0.0f;
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
84
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
85 // Put all positions in object space.
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
86 Vector3 localContentTop = transform.InverseTransformPoint(ContentTopBound.position);
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
87 Vector3 localContentBottom = transform.InverseTransformPoint(ContentBottomBound.position);
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
88 Vector3 localContainerTop = transform.InverseTransformPoint(ContainerTopBound.position);
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
89 Vector3 localContainerBottom = transform.InverseTransformPoint(ContainerBottomBound.position);
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
90
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
91 if (localContentTop.y < localContainerTop.y) {
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
92 overrunDistance = localContainerTop.y - localContentTop.y;
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
93 }
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
94 else if (localContentBottom.y > localContainerBottom.y) {
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
95 overrunDistance = localContainerBottom.y - localContentBottom.y;
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
96 }
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
97
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
98 return overrunDistance;
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
99 }
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
100
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
101 protected override void ApplyInteractions() {
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
102
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
103 Vector3 targetInteractorPositionChange = transform.parent.InverseTransformPoint(m_target.transform.position) - m_targetPivot;
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
104 targetInteractorPositionChange *= InteractionScale;
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
105 targetInteractorPositionChange.x = 0.0f;
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
106 targetInteractorPositionChange.z = 0.0f;
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
107 Vector3 contentCurrentPosition = ContentTransform.localPosition;
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
108 Vector3 newContentPosition = m_contentPivot + targetInteractorPositionChange;
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
109 Vector3 velocity = (newContentPosition - contentCurrentPosition) / Time.deltaTime;
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
110 m_velocity = velocity.y;
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
111
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
112 ContentTransform.localPosition = newContentPosition;
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
113 }
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
114
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
115 protected override void ApplyConstraints() {
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
116 return;
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
117 }
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
118
f7675884f2a1 Add Orchestland project
Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
parents:
diff changeset
119 }