Skip to content

Commit

Permalink
Fixed tracker select display to follow room rotation
Browse files Browse the repository at this point in the history
  • Loading branch information
Kylon99 committed Feb 19, 2021
1 parent 7839721 commit afb01e6
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 35 deletions.
20 changes: 2 additions & 18 deletions AlternativePlay/SaberDeviceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public Pose GetLeftSaberPose(TrackerConfigData configData)
{
// Return adjusted position from the tracker
Pose adjustedPose = this.AdjustForPlayerOrigin(trackerPose.Value);
return this.CalculatePoseFromTrackerData(configData, adjustedPose);
return Utilities.CalculatePoseFromTrackerData(configData, adjustedPose);
}
else
{
Expand All @@ -70,7 +70,7 @@ public Pose GetRightSaberPose(TrackerConfigData configData)
{
// Return adjusted position from the tracker
Pose adjustedPose = this.AdjustForPlayerOrigin(trackerPose.Value);
return this.CalculatePoseFromTrackerData(configData, adjustedPose);
return Utilities.CalculatePoseFromTrackerData(configData, adjustedPose);
}
else
{
Expand Down Expand Up @@ -128,21 +128,5 @@ private Pose AdjustForPlayerOrigin(Pose pose)

return newDevicePose;
}

public Pose CalculatePoseFromTrackerData(TrackerConfigData trackerConfigData, Pose tracker)
{
Pose result = Pose.identity;

// Calculate and apply rotation first
Quaternion extraRotation = Quaternion.Euler(trackerConfigData.EulerAngles);
Quaternion finalRotation = tracker.rotation * extraRotation;
result.rotation = finalRotation;

// Rotate position and add
Vector3 rotatedPosition = tracker.rotation * trackerConfigData.Position;
result.position = tracker.position + rotatedPosition;

return result;
}
}
}
36 changes: 30 additions & 6 deletions AlternativePlay/ShowTrackersBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ namespace AlternativePlay
{
public class ShowTrackersBehavior : MonoBehaviour
{
private MainSettingsModelSO mainSettingsModel;

private bool showTrackers;
private TrackerConfigData selectedTracker;

Expand Down Expand Up @@ -63,6 +65,8 @@ public void SetSelectedSerial(TrackerConfigData tracker)

private void Awake()
{
this.mainSettingsModel = Resources.FindObjectsOfTypeAll<MainSettingsModelSO>().FirstOrDefault();

AssetBundle assetBundle = AssetBundle.LoadFromStream(Assembly.GetExecutingAssembly().GetManifestResourceStream("AlternativePlay.Resources.alternativeplaymodels"));
this.trackerPrefab = assetBundle.LoadAsset<GameObject>("APTracker");
this.saberPrefab = assetBundle.LoadAsset<GameObject>("APSaber");
Expand All @@ -76,17 +80,17 @@ private void Update()
foreach (var tracker in this.trackerInstances)
{
// Update all the tracker poses
bool positionSuccess = tracker.InputDevice.TryGetFeatureValue(CommonUsages.devicePosition, out Vector3 position);
if (positionSuccess) tracker.Instance.transform.position = position;
Pose trackerPose = TrackedDeviceManager.GetDevicePose(tracker.InputDevice) ?? new Pose();
trackerPose = this.AdjustForRoomRotation(trackerPose);

bool rotationSuccess = tracker.InputDevice.TryGetFeatureValue(CommonUsages.deviceRotation, out Quaternion rotation);
if (rotationSuccess) tracker.Instance.transform.rotation = rotation;
tracker.Instance.transform.position = trackerPose.position;
tracker.Instance.transform.rotation = trackerPose.rotation;
}

var selectedTrackerInstance = this.trackerInstances.Find(t => t.Serial == this.selectedTracker.Serial);
if (this.selectedTracker == null || String.IsNullOrWhiteSpace(this.selectedTracker.Serial))
{
// No tracker so disable the saber
// No selected tracker so disable the saber
this.saberInstance.SetActive(false);
return;
}
Expand All @@ -95,11 +99,30 @@ private void Update()
Pose selectedTrackerPose = new Pose(
selectedTrackerInstance.Instance.transform.position,
selectedTrackerInstance.Instance.transform.rotation);
Utilities.TransformSaberFromTrackerData(this.saberInstance.transform, this.selectedTracker, selectedTrackerPose);

Pose pose = Utilities.CalculatePoseFromTrackerData(this.selectedTracker, selectedTrackerPose);
this.saberInstance.transform.position = pose.position;
this.saberInstance.transform.rotation = pose.rotation;

this.saberInstance.SetActive(true);
}

/// <summary>
/// Given any pose this method returns a new pose that is adjusted for the Beat Saber
/// Room Rotation.
/// </summary>
private Pose AdjustForRoomRotation(Pose pose)
{
var roomCenter = this.mainSettingsModel.roomCenter;
var roomRotation = Quaternion.Euler(0, this.mainSettingsModel.roomRotation, 0);

Pose result = pose;
result.position = roomRotation * pose.position;
result.position += roomCenter;
result.rotation = roomRotation * pose.rotation;
return result;
}

// Deletes all the instances and all the locally stored tracker instances data
private void RemoveAllInstances()
{
Expand All @@ -110,6 +133,7 @@ private void RemoveAllInstances()
this.saberInstance = null;
}


private class TrackerInstance
{
public GameObject Instance { get; set; }
Expand Down
11 changes: 0 additions & 11 deletions AlternativePlay/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,5 @@ public static Pose CalculatePoseFromTrackerData(TrackerConfigData trackerConfigD

return result;
}

/// <summary>
/// Transforms the <see cref="Transform"/> of a Unity game object based on the given <see cref="TrackerConfigData"/>
/// given the current tracker's rotation and position.
/// </summary>
public static void TransformSaberFromTrackerData(Transform saberTransform, TrackerConfigData trackerConfigData, Pose tracker)
{
Pose pose = CalculatePoseFromTrackerData(trackerConfigData, tracker);
saberTransform.position = pose.position;
saberTransform.rotation = pose.rotation;
}
}
}

0 comments on commit afb01e6

Please sign in to comment.