namespace Zinnia.Tracking.Velocity
{
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR;
using Zinnia.Extension;
///
/// Retrieves the velocity estimations for an .
///
public class XRNodeVelocityEstimator : VelocityTracker
{
[Tooltip("The node to query velocity estimations for.")]
[SerializeField]
private XRNode node = XRNode.LeftHand;
///
/// The node to query velocity estimations for.
///
public XRNode Node
{
get
{
return node;
}
set
{
node = value;
}
}
[Tooltip("An optional object to consider the source relative to when estimating the velocities.")]
[SerializeField]
private GameObject relativeTo;
///
/// An optional object to consider the source relative to when estimating the velocities.
///
public GameObject RelativeTo
{
get
{
return relativeTo;
}
set
{
relativeTo = value;
}
}
///
/// A collection of node states.
///
protected readonly List nodesStates = new List();
///
/// Clears .
///
public virtual void ClearRelativeTo()
{
if (!this.IsValidState())
{
return;
}
RelativeTo = default;
}
///
protected override Vector3 DoGetVelocity()
{
GetNodeState().TryGetVelocity(out Vector3 result);
result = (RelativeTo != null ? RelativeTo.transform.rotation : Quaternion.identity) * result;
return result;
}
///
protected override Vector3 DoGetAngularVelocity()
{
GetNodeState().TryGetAngularVelocity(out Vector3 result);
GetNodeState().TryGetRotation(out Quaternion rotation);
result = (RelativeTo != null ? RelativeTo.transform.rotation : Quaternion.identity) * rotation * result;
return result;
}
///
/// Gets the state of .
///
/// The associated node state.
protected virtual XRNodeState GetNodeState()
{
InputTracking.GetNodeStates(nodesStates);
foreach (XRNodeState state in nodesStates)
{
if (state.nodeType == Node)
{
return state;
}
}
return new XRNodeState();
}
}
}