Skip to content

Commit

Permalink
[#18]Added left and right handler for head shake
Browse files Browse the repository at this point in the history
  • Loading branch information
Songtone committed Oct 16, 2019
1 parent 97250b2 commit 6979a03
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class YesNoMessageGenerator : MonoBehaviour
void Start()
{
VRGestureRecognizer.Current.NodHandler += OnNod;
VRGestureRecognizer.Current.HeadshakeHandler += OnHeadshake;
//VRGestureRecognizer.Current.HeadshakeHandler += OnHeadshake;
}

void OnNod()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public class VRGestureRecognizer : MonoBehaviour
float recognitionInterval = 0.5f;

public event Action NodHandler;
public event Action HeadshakeHandler;
public event Action HeadshakeRightHandler;
public event Action HeadshakeLeftHandler;

public Queue<PoseSample> PoseSamples { get; } = new Queue<PoseSample>();

Expand All @@ -55,7 +56,8 @@ void Update()

// Recognize gestures
RecognizeNod();
RecognizeHeadshake();
RecognizeHeadshakeRight();
RecognizeHeadshakeLeft();
}

IEnumerable<PoseSample> PoseSamplesWithin(float startTime, float endTime)
Expand Down Expand Up @@ -89,7 +91,7 @@ void RecognizeNod()
}
}

void RecognizeHeadshake()
void RecognizeHeadshakeLeft()
{
try
{
Expand All @@ -98,13 +100,37 @@ void RecognizeHeadshake()
var minYaw = PoseSamplesWithin(0.01f, 0.2f).Min(sample => sample.eulerAngles.y);
var yaw = PoseSamples.First().eulerAngles.y;

if ((maxYaw - averageYaw > 10f || averageYaw - minYaw > 10f) &&
if (averageYaw - minYaw > 10f &&
Mathf.Abs(yaw - averageYaw) < 5f)
{
if (prevGestureTime < Time.time - recognitionInterval)
{
prevGestureTime = Time.time;
HeadshakeHandler?.Invoke();
HeadshakeLeftHandler?.Invoke();
}
}
}
catch (InvalidOperationException)
{
// PoseSamplesWithin contains no entry
}
}
void RecognizeHeadshakeRight()
{
try
{
var averageYaw = PoseSamplesWithin(0.2f, 0.4f).Average(sample => sample.eulerAngles.y);
var maxYaw = PoseSamplesWithin(0.01f, 0.2f).Max(sample => sample.eulerAngles.y);
var minYaw = PoseSamplesWithin(0.01f, 0.2f).Min(sample => sample.eulerAngles.y);
var yaw = PoseSamples.First().eulerAngles.y;

if (maxYaw - averageYaw > 10f &&
Mathf.Abs(yaw - averageYaw) < 5f)
{
if (prevGestureTime < Time.time - recognitionInterval)
{
prevGestureTime = Time.time;
HeadshakeRightHandler?.Invoke();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class Main : MonoBehaviour
void Start()
{
VRGestureRecognizer.Current.NodHandler += OnNod;
VRGestureRecognizer.Current.HeadshakeHandler += OnHeadshake;
//VRGestureRecognizer.Current.HeadshakeHandler += OnHeadshake;
}

void OnNod()
Expand Down
10 changes: 8 additions & 2 deletions Social_AI_XR/Assets/cubefly.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,21 @@ void Start()
{

VRGestureRecognizer.Current.NodHandler += OnNod;
VRGestureRecognizer.Current.HeadshakeHandler += OnHeadshake;
VRGestureRecognizer.Current.HeadshakeLeftHandler += OnHeadshakeLeft;
VRGestureRecognizer.Current.HeadshakeRightHandler += OnHeadshakeRight;

}

void OnNod()
{
gameObject.GetComponent<Rigidbody>().AddForce(0, 5, 0, ForceMode.Impulse);
}

void OnHeadshake()
void OnHeadshakeLeft()
{
gameObject.GetComponent<Rigidbody>().AddForce(-5, 0, 0, ForceMode.Impulse);
}
void OnHeadshakeRight()
{
gameObject.GetComponent<Rigidbody>().AddForce(5, 0, 0, ForceMode.Impulse);
}
Expand Down

0 comments on commit 6979a03

Please sign in to comment.