【问题标题】:Comparing Two Quaternions for a mobile game比较手机游戏的两个四元数
【发布时间】:2016-03-20 23:02:35
【问题描述】:

我目前正在使用 Unity 开发一款手机游戏。它涉及使用陀螺仪来获取手机在空间中的方向/旋转。我一直很难在 C# 中比较两个四元数。这是我的代码:

transform.rotation = Input.gyro.attitude;       // Take the gyroscope rotation data and copy it
transform.rotation.Set(0, 0, 0, 1);             // change current Quaternion rotation data of the copy
                                                // Copy shoud now represent a vertical rotation
Angle = Quaternion.Angle(Input.gyro.attitude,); // Calculate the Angle between the 2
if (Angle < 45)                                 // if it's almost in vertical postion start the game
   /* do something */

我实际上想要做的是验证手机是否处于垂直位置(就像你把它贴在额头上一样)。我创建了原始方向的副本并将其更改为我想要的方向。之后我比较两者,如果它们几乎相同,则游戏可以继续。我对四元数几乎一无所知。我试图找出 w,x,y,z 的四元数值应该用来表示垂直方向,但在谷歌上搜索时找不到任何线索。所以这就是我在这里寻求帮助的原因。

【问题讨论】:

    标签: c# android unityscript quaternions gyroscope


    【解决方案1】:

    您提到您正在尝试查看手机是否处于垂直方向。

    我不确定您希望获得的准确度,但如果您只想确定这些,请尝试以下操作:

    if (Screen.orientation == ScreenOrientation.LandscapeLeft) {
        // screen is horizontal
    }
    

    【讨论】:

    • 我要找的不是屏幕的方向,而是手机本身的方向。感谢 Screen.orientation 顺便说一句,将我的游戏设置为仅水平很有用:)
    猜你喜欢
    • 2021-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-12
    • 1970-01-01
    相关资源
    最近更新 更多