SeP*_*läm 9 c# math accelerometer unity-game-engine tilt
这几行C#代码使用加速度计计算手机围绕y轴的旋转:
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;
}
Run Code Online (Sandbox Code Playgroud)
如果手机平放在桌子上,你知道数学运算吗?即如果inclination小于25度或超过155度?该代码源自此SO帖子,其中提到可以使用指南针.不幸的是我不知道如何,所以你的建议非常感谢.另外,如果可能的话,我想避免使用陀螺仪,而是坚持使用加速度计.
任何建议都是受欢迎的.谢谢.
为什么不使用陀螺仪呢?
如果你仔细想想,如果旋转轴与加速度之和完全平行,那么从加速度矢量中获得任何类型的旋转信息似乎在物理上是不可能的。
(也许你可以从向心力获得它的速度,但除此之外别无他法。)
| 归档时间: |
|
| 查看次数: |
734 次 |
| 最近记录: |