Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue where poke interactors would cause conflicting behaviour with the attach visualizer behaviour #103

Merged
merged 4 commits into from
Dec 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@ namespace RealityToolkit.Input.InteractionBehaviours
{
/// <summary>
/// Attaches the <see cref="IControllerInteractor"/>'s <see cref="Controllers.IControllerVisualizer"/>
/// to the <see cref="Interactables.IInteractable"/> pose.
/// to the <see cref="Interactables.IInteractable"/> pose and keeps them in sync until input is released.
/// </summary>
/// <remarks>
/// Only supports <see cref="IControllerInteractor"/>s.
/// Does not support <see cref="IPokeInteractor"/>s and will ignore them.
/// </remarks>
public class AttachControllerVisualizerBehaviour : BaseInteractionBehaviour
{
[SerializeField, Tooltip("Optional local offset from the object's pivot.")]
Expand All @@ -21,14 +25,14 @@ public class AttachControllerVisualizerBehaviour : BaseInteractionBehaviour
[SerializeField, Tooltip("Optional local offset from the object's pivot.")]
private Vector3 poseLocalRotationOffset = Vector3.zero;

private readonly List<IControllerInteractor> interactors = new List<IControllerInteractor>();
private readonly List<IControllerInteractor> attachedInteractors = new List<IControllerInteractor>();

/// <inheritdoc/>
protected override void Update()
{
for (int i = 0; i < interactors.Count; i++)
for (int i = 0; i < attachedInteractors.Count; i++)
{
var interactor = interactors[i];
var interactor = attachedInteractors[i];
var pose = GetGrabPose(interactor);

interactor.Controller.Visualizer.PoseDriver.SetPositionAndRotation(pose.position, pose.rotation);
Expand All @@ -38,63 +42,64 @@ protected override void Update()
/// <inheritdoc/>
protected override void OnSelectEntered(InteractionEventArgs eventArgs)
{
if (eventArgs.Interactor is not IControllerInteractor controllerInteractor)
if (eventArgs.Interactor is not IControllerInteractor controllerInteractor ||
eventArgs.Interactor is IPokeInteractor)
{
return;
}

Attach(controllerInteractor);
AttachVisualizer(controllerInteractor);
}

/// <inheritdoc/>
protected override void OnSelectExited(InteractionExitEventArgs eventArgs)
{
if (eventArgs.Interactor is not IControllerInteractor controllerInteractor)
if (eventArgs.Interactor is not IControllerInteractor controllerInteractor ||
eventArgs.Interactor is IPokeInteractor)
{
return;
}

Detach(controllerInteractor);
DetachVisualizer(controllerInteractor);
}

/// <inheritdoc/>
protected override void OnGrabEntered(InteractionEventArgs eventArgs)
{
if (eventArgs.Interactor is not IControllerInteractor controllerInteractor)
if (eventArgs.Interactor is not IControllerInteractor controllerInteractor ||
eventArgs.Interactor is IPokeInteractor)
{
return;
}

Attach(controllerInteractor);
AttachVisualizer(controllerInteractor);
}

/// <inheritdoc/>
protected override void OnGrabExited(InteractionExitEventArgs eventArgs)
{
if (eventArgs.Interactor is not IControllerInteractor controllerInteractor)
if (eventArgs.Interactor is not IControllerInteractor controllerInteractor ||
eventArgs.Interactor is IPokeInteractor)
{
return;
}

Detach(controllerInteractor);
DetachVisualizer(controllerInteractor);
}

private void Attach(IControllerInteractor currentInteractor)
private void AttachVisualizer(IControllerInteractor currentInteractor)
{
interactors.EnsureListItem(currentInteractor);
attachedInteractors.EnsureListItem(currentInteractor);
currentInteractor.Controller.Visualizer.VisualizerPoseOverrideSource = transform;
}

private void Detach(IControllerInteractor currentInteractor)
private void DetachVisualizer(IControllerInteractor currentInteractor)
{
interactors.SafeRemoveListItem(currentInteractor);
attachedInteractors.SafeRemoveListItem(currentInteractor);
currentInteractor.Controller.Visualizer.VisualizerPoseOverrideSource = null;
}

private Pose GetGrabPose(IControllerInteractor controllerInteractor)
{
return new Pose(GetGrabPosition(controllerInteractor), GetGrabRotation(controllerInteractor));
}
private Pose GetGrabPose(IControllerInteractor controllerInteractor) => new Pose(GetGrabPosition(controllerInteractor), GetGrabRotation(controllerInteractor));

private Vector3 GetGrabPosition(IControllerInteractor currentInteractor)
{
Expand All @@ -111,10 +116,10 @@ private Vector3 GetGrabPosition(IControllerInteractor currentInteractor)
return worldAttachmentPosition;
}

private Quaternion GetGrabRotation(IControllerInteractor currentInteractor)
private Quaternion GetGrabRotation(IControllerInteractor interactor)
{
var worldAttachmentRotation = Quaternion.Euler(poseLocalRotationOffset);
var localControllerOffset = currentInteractor.Controller.Visualizer.GripPose.localRotation;
var localControllerOffset = interactor.Controller.Visualizer.GripPose.localRotation;
return transform.rotation * worldAttachmentRotation * localControllerOffset;
}
}
Expand Down