AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
OrbitPoint.cs
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License. See LICENSE in the project root for license information.
3 
4 using UnityEngine;
5 using System.Collections;
6 
7 namespace HoloToolkit.Examples.Prototyping
8 {
12  public class OrbitPoint : MonoBehaviour
13  {
14  [Tooltip("position to orbit around")]
15  public Vector3 CenterPoint = new Vector3();
16 
17  [Tooltip("Axis to orbit around")]
18  public Vector3 Axis = Vector3.forward;
19 
20  [Tooltip("size of the orbit")]
21  public float Radius = 0.2f;
22 
23  [Tooltip("orbit speed")]
24  public float RevolutionSpeed = 2.0f;
25 
26  [Tooltip("starting position based on 360 degrees")]
27  public float StartAngle = 0;
28 
29  [Tooltip("Limit the amount or revolutions, set to zero for infinite")]
30  public float Revolutions = 0;
31 
32  [Tooltip("Orbit the other way")]
33  public bool Reversed = false;
34 
35  [Tooltip("Start automatically")]
36  public bool AutoPlay = true;
37 
38  [Tooltip("pause the orbit or status")]
39  public bool IsPaused = false;
40 
44  public int RevolutionCount { get; private set; }
45 
46  [Tooltip("Smooth in and out of the orbit")]
47  public bool SmoothEaseInOut = false;
48 
49  [Tooltip("Smoothness factor")]
50  public float SmoothRatio = 1;
51 
52  // starting angle
53  private float mAngle = 0;
54 
55  // current time
56  private float mTime = 0;
57 
58  // position
59  private Vector3 mPositionVector;
60 
61  // rotation
62  private Vector3 mRotatedPositionVector;
63 
67  private void Start()
68  {
69  RevolutionCount = 0;
70  mPositionVector = transform.up;
71 
72  // are we rotating around the y
73  if (!Mathf.Approximately(Vector3.Angle(Axis, mPositionVector), 90))
74  {
75  // are we rotating around the z
76  mPositionVector = transform.forward;
77  if (!Mathf.Approximately(Vector3.Angle(Axis, mPositionVector), 90))
78  {
79  float x = Mathf.Abs(Axis.x);
80  float y = Mathf.Abs(Axis.y);
81  float z = Mathf.Abs(Axis.z);
82 
83  if (x > y && x > z)
84  {
85  // left or right - cross with the z axis
86  mPositionVector = Vector3.Cross(Axis * Radius, Vector3.forward);
87  }
88 
89  if (z > y && z > x)
90  {
91  // forward or backward - cross with the x axis
92  mPositionVector = Vector3.Cross(Axis * Radius, Vector3.right);
93  }
94 
95  if (y > z && y > x)
96  {
97  // up or down - cross with the x axis
98  mPositionVector = Vector3.Cross(Axis * Radius, Vector3.right);
99  }
100  }
101  }
102 
103  // set the position
104  transform.Rotate(Axis, mAngle - StartAngle);
105  mRotatedPositionVector = Quaternion.AngleAxis(mAngle - StartAngle, Axis) * mPositionVector * Radius;
106  transform.localPosition = CenterPoint + mRotatedPositionVector;
107 
108  IsPaused = !AutoPlay;
109  }
110 
114  public void StartOrbit()
115  {
116  IsPaused = false;
117  RevolutionCount = 0;
118  }
119 
123  public void ResetOrbit()
124  {
125  mAngle = 0;
126  }
127 
128  // easing function
129  public float QuartEaseInOut(float s, float e, float v)
130  {
131  //e -= s;
132  if ((v /= 0.5f) < 1)
133  return e / 2 * v * v * v * v + s;
134 
135  return -e / 2 * ((v -= 2) * v * v * v - 2) + s;
136  }
137 
138  // Update is called once per frame
139  private void Update()
140  {
141  if (IsPaused) return;
142 
143  // get the percent of time passed
144  float percentage = mTime / RevolutionSpeed;
145 
146  // are we smooth?
147  if (SmoothEaseInOut)
148  {
149  // apply the smooth factor
150  float linearSmoothing = 1 * (percentage * (1 - SmoothRatio));
151  percentage = QuartEaseInOut(0, 1, percentage) * SmoothRatio + linearSmoothing;
152  }
153 
154  // update the angle
155  mAngle = 0 - (percentage) * 360;
156 
157  // rotate the right direction
158  if (Reversed)
159  {
160  mAngle = -mAngle;
161  }
162 
163  // set the position
164  transform.Rotate(Axis, mAngle - StartAngle);
165  mRotatedPositionVector = Quaternion.AngleAxis(mAngle - StartAngle, Axis) * mPositionVector * Radius;
166  transform.localPosition = CenterPoint + mRotatedPositionVector;
167 
168  // update the time
169  mTime += Time.deltaTime;
170  if (mTime >= RevolutionSpeed)
171  {
172  mTime = RevolutionSpeed - mTime;
173  RevolutionCount += 1;
174 
175  if (RevolutionCount >= Revolutions && Revolutions > 0)
176  {
177  IsPaused = true;
178  mTime = 0;
179  }
180  }
181  }
182  }
183 }
float QuartEaseInOut(float s, float e, float v)
Definition: OrbitPoint.cs:129
void StartOrbit()
Start the orbit animation
Definition: OrbitPoint.cs:114
places the object in orbit around a point
Definition: OrbitPoint.cs:12
void ResetOrbit()
reset the orbit position (does not stop)
Definition: OrbitPoint.cs:123