Skip to content

Commit 6044962

Browse files
committed
Cursor now honors default distances via RayStep utility functions
1 parent 06c606a commit 6044962

File tree

2 files changed

+97
-5
lines changed

2 files changed

+97
-5
lines changed

Assets/HoloToolkit/Input/Scripts/Cursor/Cursor.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -288,10 +288,7 @@ protected virtual void UpdateCursorTransform()
288288
{
289289
FocusDetails focusDetails = FocusManager.Instance.GetFocusDetails(Pointer);
290290
GameObject newTargetedObject = focusDetails.Object;
291-
292-
// Get the forward vector looking back along the pointing ray.
293-
RayStep lastStep = Pointer.Rays[Pointer.Rays.Length - 1];
294-
Vector3 lookForward = -lastStep.direction;
291+
Vector3 lookForward = Vector3.forward;
295292

296293
// Normalize scale on before update
297294
targetScale = Vector3.one;
@@ -301,7 +298,9 @@ protected virtual void UpdateCursorTransform()
301298
{
302299
TargetedObject = null;
303300
TargetedCursorModifier = null;
304-
targetPosition = lastStep.terminus;
301+
302+
targetPosition = RayStep.GetPointByDistance(Pointer.Rays, DefaultCursorDistance);
303+
lookForward = -RayStep.GetDirectionByDistance(Pointer.Rays, DefaultCursorDistance);
305304
targetRotation = lookForward.magnitude > 0 ? Quaternion.LookRotation(lookForward, Vector3.up) : transform.rotation;
306305
}
307306
else
@@ -316,6 +315,10 @@ protected virtual void UpdateCursorTransform()
316315
else
317316
{
318317
// If no modifier is on the target, just use the hit result to set cursor position
318+
// Get the look forward by using distance between pointer origin and target position
319+
// (This may not be strictly accurate for extremely wobbly pointers, but it should produce usable results)
320+
float distanceToTarget = Vector3.Distance(Pointer.Rays[0].origin, focusDetails.Point);
321+
lookForward = -RayStep.GetDirectionByDistance(Pointer.Rays, distanceToTarget);
319322
targetPosition = focusDetails.Point + (lookForward * SurfaceCursorDistance);
320323
Vector3 lookRotation = Vector3.Slerp(focusDetails.Normal, lookForward, LookRotationBlend);
321324
targetRotation = Quaternion.LookRotation(lookRotation == Vector3.zero ? lookForward : lookRotation, Vector3.up);

Assets/HoloToolkit/Input/Scripts/Focus/RayStep.cs

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,94 @@ public static implicit operator Ray(RayStep r)
4444
{
4545
return new Ray(r.origin, r.direction);
4646
}
47+
48+
#region static utility functions
49+
50+
/// <summary>
51+
/// Returns a point along an array of RaySteps by distance
52+
/// </summary>
53+
/// <param name="steps"></param>
54+
/// <param name="distance"></param>
55+
/// <returns></returns>
56+
public static Vector3 GetPointByDistance(RayStep[] steps, float distance)
57+
{
58+
Debug.Assert(steps != null);
59+
Debug.Assert(steps.Length > 0);
60+
61+
Vector3 point = Vector3.zero;
62+
float remainingDistance = distance;
63+
64+
for (int i = 0; i < steps.Length; i++)
65+
{
66+
if (remainingDistance > steps[i].length)
67+
{
68+
remainingDistance -= steps[i].length;
69+
}
70+
else
71+
{
72+
point = Vector3.Lerp(steps[i].origin, steps[i].terminus, remainingDistance / steps[i].length);
73+
remainingDistance = 0;
74+
break;
75+
}
76+
}
77+
78+
if (remainingDistance > 0)
79+
{
80+
// If we reach the end and still have distance left, set the point to the terminus of the last step
81+
point = steps[steps.Length - 1].terminus;
82+
}
83+
84+
return point;
85+
}
86+
87+
/// <summary>
88+
/// Returns a RayStep along an array of RaySteps by distance
89+
/// </summary>
90+
/// <param name="steps"></param>
91+
/// <param name="distance"></param>
92+
/// <returns></returns>
93+
public static RayStep GetStepByDistance(RayStep[] steps, float distance)
94+
{
95+
Debug.Assert(steps != null);
96+
Debug.Assert(steps.Length > 0);
97+
98+
RayStep step = new RayStep();
99+
float remainingDistance = distance;
100+
101+
for (int i = 0; i < steps.Length; i++)
102+
{
103+
if (remainingDistance > steps[i].length)
104+
{
105+
remainingDistance -= steps[i].length;
106+
}
107+
else
108+
{
109+
step = steps[i];
110+
remainingDistance = 0;
111+
break;
112+
}
113+
}
114+
115+
if (remainingDistance > 0)
116+
{
117+
// If we reach the end and still have distance left, return the last step
118+
step = steps[steps.Length - 1];
119+
}
120+
121+
return step;
122+
}
123+
124+
/// <summary>
125+
/// Returns a direction along an array of RaySteps by distance
126+
/// </summary>
127+
/// <param name="steps"></param>
128+
/// <param name="distance"></param>
129+
/// <returns></returns>
130+
public static Vector3 GetDirectionByDistance(RayStep[] steps, float distance)
131+
{
132+
return GetStepByDistance(steps, distance).direction;
133+
}
134+
135+
#endregion
47136
}
48137
}

0 commit comments

Comments
 (0)