Skip to content

Commit

Permalink
Cancel head tracking jumps on all axes
Browse files Browse the repository at this point in the history
Detect an erroneous jump by a large change in orientation. In this
case, do not accept orientation updates until the Z and X axes have
corrected themselves. At that point, cancel out Y axis change, and
accept further updates.
  • Loading branch information
caspian-maclean committed Jun 23, 2015
1 parent 8a29f93 commit 52cec0a
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions Cardboard/Scripts/CardboardHead.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public class CardboardHead : MonoBehaviour {
// If set, the head transform will be relative to it.
public Transform target;
public Quaternion fixQuaternion;
public Quaternion lastRot;
public Quaternion acceptedRot;
public float acceptRadius;
public bool glitchFixStarted = false;

// Determine whether head updates early or late in frame.
Expand Down Expand Up @@ -68,16 +69,25 @@ public class CardboardHead : MonoBehaviour {
if (trackRotation) {
var rot = Cardboard.SDK.HeadRotation;
if (!glitchFixStarted) {
lastRot=rot;
acceptedRot=rot;
fixQuaternion = Quaternion.Euler(0F,0F,0F);
glitchFixStarted = true;
acceptRadius = 7F;
}
if (Quaternion.Angle(rot,lastRot) > 15) {
var newFixQuaternion = fixQuaternion * lastRot * Quaternion.Inverse(rot);
//only correct y axis, cardboard will correct the other axes quickly, probably using accelerometer
fixQuaternion = Quaternion.Euler(0F,newFixQuaternion.eulerAngles.y, 0F);
acceptRadius += 5F;
if (Quaternion.Angle(rot,acceptedRot) <= acceptRadius) {
acceptedRot = rot;
acceptRadius = 7F;
} else {
var diffRot = acceptedRot * Quaternion.Inverse(rot);
var projection = Quaternion.Euler(0F, diffRot.eulerAngles.y, 0F) * rot;
if (Quaternion.Angle(rot,projection) <= acceptRadius) {
acceptedRot = rot;
acceptRadius = 7F;
fixQuaternion = Quaternion.Euler(0F,diffRot.eulerAngles.y, 0F);
}
}
lastRot=rot;
rot = acceptedRot;
if (target == null) {
transform.localRotation = fixQuaternion*rot;
} else {
Expand Down

0 comments on commit 52cec0a

Please sign in to comment.