这几行C#代码使用加速度计计算手机绕y轴的旋转:
你知道如果手机放在桌子上时,使它工作所需的数学吗?即如果倾斜度小于25度或大于155度,
欢迎任何建议。谢谢。
private float GetRoll() {
/*
* Sum up the accelerometer events
*/
Vector3 accelerationVector = Vector3.zero;
for (int i=0; i < Input.accelerationEventCount; i++) {
AccelerationEvent accEvent = Input.GetAccelerationEvent(i);
accelerationVector += accEvent.acceleration * accEvent.deltaTime;
}
accelerationVector.Normalize();
int inclination = (int) Mathf.Round(Mathf.Rad2Deg * Mathf.Acos(accelerationVector.z));
float roll = 0;
if (inclination < 25 || inclination > 155) {
/*
* How to calculate the rotation here?
*/
} else {
roll = Mathf.Round(Mathf.Rad2Deg * Mathf.Atan2(accelerationVector.x, accelerationVector.y));
}
return roll;
}
你知道如果手机放在桌子上时,使它工作所需的数学吗?即如果倾斜度小于25度或大于155度,
inclination
是什么?
这段代码来自这篇SO文章,提到了可以使用指南针。不幸的是,我不知道如何操作,非常需要你的建议。此外,如果可能的话,我希望避免使用陀螺仪,而是坚持使用加速度计。欢迎任何建议。谢谢。