如何正确地将四个四基因转变为自身

发布于 2025-01-20 16:13:07 字数 2059 浏览 1 评论 0原文

在Unity项目中,我必须将Quaternion类转换为Vector3,然后转换回Quathnion。

因此,我编写了代码:

public void method(Quaternion rotation)
{
    Vector3 vector = rotation.eulerAngles;
    process(vector); // doesn't change vector
    Quaternion result = Quaternion.Euler(vector.x, vector.y, vector.z);

    if (rotation != result)
    {
        using (StreamWriter writer = new StreamWriter("Quaternions", true))
        {
            writer.Write(rotation + "\t->\t" + vector + "\t->\t" + result + "\n");
        }
    }
}

获得的结果意味着Quaternions rotation结果不一样。

(0.1, 0.0, 0.0, 1.0)    ->  (13.4, 348.3, 359.3)    ->  (0.1, -0.1, 0.0, 1.0)
(0.1, -0.1, 0.0, 1.0)   ->  (10.3, 0.1, 358.6)  ->  (-0.1, 0.0, 0.0, -1.0)
(0.1, 0.1, 0.0, 1.0)    ->  (13.2, 349.1, 359.3)    ->  (0.1, -0.1, 0.0, 1.0)
(0.1, -0.1, 0.0, 1.0)   ->  (10.3, 0.1, 358.6)  ->  (-0.1, 0.0, 0.0, -1.0)
(0.1, 0.1, 0.0, 1.0)    ->  (13.9, 346.0, 359.6)    ->  (0.1, -0.1, 0.0, 1.0)

我尝试将quaternion.euler(vector.x,vector.y,vector.z)更改为quaternion.euler(vector)if(rotation!= = =)结果) to if(rotation.normalized!= result.normalized),但没有任何更改。

请教我如何正确地将四元组转换为vector3并重新转换为季节。

提前致谢。

编辑:精确版本

(0.1208651, -0.1202484, 0.0108935, 0.9852986)   ->  (13.9, 346.3, 359.4)    ->  (0.1205529, -0.1181887, 0.0094058, 0.9856012)
(0.1117781, -0.0784635, 0.0001221, 0.9906307)   ->  (13.9, 346.6, 359.4)    ->  (0.1206529, -0.1155783, 0.0093524, 0.9858990)
(0.1206530, -0.1155784, 0.0093524, 0.9858992)   ->  (13.2, 349.1, 359.3)    ->  (0.1146793, -0.0939169, 0.0047323, 0.9889418)
(0.1102806, -0.0695964, -0.0030001, 0.9914563)  ->  (13.7, 347.4, 359.1)    ->  (0.1191836, -0.1079238, 0.0057138, 0.9869727)
(0.1191836, -0.1079238, 0.0057138, 0.9869727)   ->  (12.9, 350.0, 359.1)    ->  (0.1130020, -0.0854713, 0.0017047, 0.9899103)

更新: 谢谢大家回答!我决定不将Quaternion转换为vector3和返回。

In Unity project I have to convert Quaternion class to Vector3 and then convert back into Quaternion.

So I wrote the code:

public void method(Quaternion rotation)
{
    Vector3 vector = rotation.eulerAngles;
    process(vector); // doesn't change vector
    Quaternion result = Quaternion.Euler(vector.x, vector.y, vector.z);

    if (rotation != result)
    {
        using (StreamWriter writer = new StreamWriter("Quaternions", true))
        {
            writer.Write(rotation + "\t->\t" + vector + "\t->\t" + result + "\n");
        }
    }
}

And got results means Quaternions rotation and result are not same.

(0.1, 0.0, 0.0, 1.0)    ->  (13.4, 348.3, 359.3)    ->  (0.1, -0.1, 0.0, 1.0)
(0.1, -0.1, 0.0, 1.0)   ->  (10.3, 0.1, 358.6)  ->  (-0.1, 0.0, 0.0, -1.0)
(0.1, 0.1, 0.0, 1.0)    ->  (13.2, 349.1, 359.3)    ->  (0.1, -0.1, 0.0, 1.0)
(0.1, -0.1, 0.0, 1.0)   ->  (10.3, 0.1, 358.6)  ->  (-0.1, 0.0, 0.0, -1.0)
(0.1, 0.1, 0.0, 1.0)    ->  (13.9, 346.0, 359.6)    ->  (0.1, -0.1, 0.0, 1.0)

I tried to change Quaternion.Euler(vector.x, vector.y, vector.z) to Quaternion.Euler(vector), if (rotation != result) to if (rotation.normalized != result.normalized), but nothing changed.

Please teach me how I can correctly transform Quaternion to Vector3 and back into Quaternion.

Thanks in advance.

Edit: precise version

(0.1208651, -0.1202484, 0.0108935, 0.9852986)   ->  (13.9, 346.3, 359.4)    ->  (0.1205529, -0.1181887, 0.0094058, 0.9856012)
(0.1117781, -0.0784635, 0.0001221, 0.9906307)   ->  (13.9, 346.6, 359.4)    ->  (0.1206529, -0.1155783, 0.0093524, 0.9858990)
(0.1206530, -0.1155784, 0.0093524, 0.9858992)   ->  (13.2, 349.1, 359.3)    ->  (0.1146793, -0.0939169, 0.0047323, 0.9889418)
(0.1102806, -0.0695964, -0.0030001, 0.9914563)  ->  (13.7, 347.4, 359.1)    ->  (0.1191836, -0.1079238, 0.0057138, 0.9869727)
(0.1191836, -0.1079238, 0.0057138, 0.9869727)   ->  (12.9, 350.0, 359.1)    ->  (0.1130020, -0.0854713, 0.0017047, 0.9899103)

Upd:
Thank you everyone who answered! I decided to not convert Quaternion to Vector3 and back.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不及他 2025-01-27 16:13:07

首先,在 Unity 中调试某些东西的方式很奇怪。

现在回答问题。为什么你要尝试将某种东西转换成另一种东西,然后又恢复到原来的样子?根据您的代码,您不会更改创建的 Vector3,那么为什么不只使用原始的四元数呢?

EulerAngles 无法真正保存与四元数本身一样多的有关四元数旋转的信息,因此在使用 Euler 时信息很可能会丢失或更改。

以下是您可能想要查看的 Unity 文档的两个链接。第一个解释了为什么使用 EulerAngles 往往会不一致,而另一个可能对您想要做的任何事情都有解决方案。

https://docs.unity3d.com/ScriptReference/Quaternion-eulerAngles.html

https://docs.unity3d.com/ScriptReference/Quaternion-operator_multiply.html

first off, odd way to debug something in Unity.

And now to the question. Why are you trying to convert something to something else and then back to itself? According to your code you are not changing the created Vector3, so why not just use the original Quaternion?

EulerAngles can't really hold as much information about the rotation of a Quaternion than the Quaternion itself, so It's very likely that information can get lost or altered when working with Eulers.

Here are the two links to Unity-Documentation that you might wanna look at. The first explains, why working with EulerAngles tends to be inconsistent and the other might have a solution for whatever it is you're trying to do.

https://docs.unity3d.com/ScriptReference/Quaternion-eulerAngles.html

https://docs.unity3d.com/ScriptReference/Quaternion-operator_multiply.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文