首页 > 解决方案 > 用移动设备的陀螺仪做球平衡游戏

问题描述

正如标题已经说过的那样,我目前正在开发一个小型球平衡游戏,使用陀螺仪旋转来旋转上面有球的浮动平台。我经常遇到的问题是:我无法锁定y轴。我使用了欧拉角,它工作得很好,但是我必须处理万向节锁定。因为我不想处理这个欧拉问题,所以我尝试使用四元数,它确实以某种方式工作。我可以设置旋转并为陀螺仪定义校准偏移。如果我不计算这个偏移量,旋转将是绝对的,而不是相对于游戏世界空间。但是我总是会进行某种 y 旋转,即使我将四元数设置为 0。

例如:我开始游戏,将陀螺仪校准为我的旋转,但之后在现实世界空间中围绕 y 轴旋转。平台始终面向相机,但旋转轴也不是。

正常行为以及它应该如何(在现实世界空间中没有在 y 轴上旋转): https ://www.nani-games.net/share/umSc6XH1vE.gif

实际行为及其不应该如何(在现实世界空间中沿 y 轴旋转): https ://www.nani-games.net/share/AVicmV4fWe.gif

我已经尝试过的列表:

我的场景是如何构建的: https ://www.nani-games.net/share/Unity_fAQKW7cfPK.png

我有两个脚本用于获取陀螺仪旋转,使其统一有用并使用它来旋转其他对象。

一个名为 DeviceRotation 的静态脚本。它激活陀螺并从中获得旋转:

using UnityEngine;

static class DeviceRotation
{
    public static Quaternion offsetRotation;

    private static bool gyroInitialized = false;

    public static bool HasGyroscope
    {
        get
        {
            return SystemInfo.supportsGyroscope;
        }
    }

    public static Quaternion Get()
    {
        if (!gyroInitialized)
        {
            InitGyro();
        }

        return HasGyroscope
            ? ReadGyroscopeRotation()
            : Quaternion.identity;
    }

    private static void InitGyro()
    {
        if (HasGyroscope)
        {
            Input.gyro.enabled = true;
            Input.gyro.updateInterval = 0.0167f; // 60Hz
        }
        gyroInitialized = true;
    }

    private static Quaternion ReadGyroscopeRotation()
    {
        return new Quaternion(0.5f, 0.5f, -0.5f, 0.5f) * Input.gyro.attitude * new Quaternion(0, 0, 1, 0);
    }

    public static void calibrateCoords()
    {
        Quaternion deviceRotation = Get();
        offsetRotation = deviceRotation;
    }
}

附加到平台对象的第二个脚本。它得到一个校准四元数,从校准四元数中计算一个新的四元数和一个不断更新的四元数(都取自陀螺仪旋转),然后将平台变换设置为这个新的旋转:

using UnityEngine;
using UnityEngine.UI;

public class Gyroscope : MonoBehaviour
{
    public Button resetSphere;
    public Button calibrate;
    public GameObject sphere;

    public Text platformCoords;
    public Text gyroOffsetCoords;

    void Start()
    {
        DeviceRotation.calibrateCoords();
        calibrate.onClick.AddListener(DeviceRotation.calibrateCoords);

        setGyroRotation();
        resetSphere.onClick.AddListener(ResetSphere);
    }

    void Update()
    {
        setGyroRotation();

        Vector3 offsetRotationEuler = DeviceRotation.offsetRotation.eulerAngles;
        gyroOffsetCoords.text = "GyroOffset:\n" +
                                "X: " + offsetRotationEuler.x + "\n" +
                                "Y: " + offsetRotationEuler.y + "\n" +
                                "Z: " + offsetRotationEuler.z;
    }

    private void ResetSphere()
    {
        GameObject temp_sphere = Instantiate(sphere);
        temp_sphere.transform.position = new Vector3(0, 3, 0);
    }

    private void setGyroRotation()
    {
        Quaternion deviceRotation = DeviceRotation.Get();
        Quaternion newRotation = Quaternion.Inverse(DeviceRotation.offsetRotation) * deviceRotation;
        transform.localRotation = newRotation;

        Vector3 platformAngles = transform.localRotation.eulerAngles;
        platformCoords.text = "Platform:" + "\n" +
                              "X: " + platformAngles.x + "\n" +
                              "Y: " + platformAngles.y + "\n" +
                              "Z: " + platformAngles.z;
    }
}

因此,总结一切:我的问题是我无法锁定四元数的一个轴(y 轴)。我还尝试了使用 euler 进行所需旋转然后将其转换为四元数的组合(这就是为什么脚本 OffsetGyro 中存在这两个函数 QuaternionToEuler 和 EulerToQuaternion 的原因),但不幸的是这导致了万向节锁定(我猜这真是令人惊讶)。

在这个四元数问题上我真的需要帮助,我已经坚持了好几天了。

标签: c#unity3drotationquaternionsgyroscope

解决方案


所以,我玩了一段时间,并尝试随机创建一个可能的解决方案。这是我当前的函数 setGyroRotation:

private void setGyroRotation()
{
    Quaternion deviceRotation = DeviceRotation.Get();
    Quaternion newRotation = Quaternion.Inverse(DeviceRotation.offsetRotation) * deviceRotation; // Calculating "initial" rotation
    axisObject.transform.rotation = newRotation; // Setting the axis' rotation to our "initial" rotation
    Vector3 unfixedUp = newRotation * Vector3.up;

    float angle = Vector3.Angle(Vector3.up, unfixedUp);
    Vector3 axis = Vector3.Cross(Vector3.up, unfixedUp);

    if (axis == Vector3.zero)
    {
        transform.rotation = Quaternion.AngleAxis(unfixedUp.y >= 0f ? 0f : 180f, Vector3.right);
    }
    else
    {
        transform.rotation = Quaternion.AngleAxis(angle, axis);
    }

    Vector3 platformAngles = transform.localRotation.eulerAngles;
    platformCoords.text = "Platform:" + "\n" +
                            "X: " + platformAngles.x + "\n" +
                            "Y: " + platformAngles.y + "\n" +
                            "Z: " + platformAngles.z;
}

它几乎可以按预期工作:没有万向节锁定(至少没有一个容易发生的情况。只有当您在 x 或 z 上将手机旋转大约 180 度时)并且 y 轴被冻结,但初始/校准旋转轴仍然是一个大问题. 这是一个 gif,希望能比其他的更好地显示问题: https ://www.nani-games.net/share/93eHK0Fz1t.gif

平台上方的轴显示应用了偏移的陀螺仪旋转。


推荐阅读