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