从两个矢量旋转/方向矢量、方向、两个

2023-09-07 23:42:58 作者:不再依赖人海中的你

我工作的一个小Unity3D项目,我需要从另一个工具中导入一些数据。该工具使我的面向对象通过两个向量,我需要端口统一。

作为一个例子,我有这两个向量;

  X =向量(0.707,0.707,0)
Y =向量(-0.707,0.707,0)
 

由于只有这一信息,我需要了解我的对象需要在Z轴上旋转45度。这两个向量重新present我的对象,而不是起点,也不是终点的向上和向右载体。

修改

好了,现在我可以做单轴旋转!是啊!提供了这个功能;

 函数RotationMaster(VX:Vector3类型,VY:Vector3类型){
    VAR VZ = Vector3.Cross(VX,VY);

    VAR X = Mathf.Atan2(-vz.y,vz.z)* Mathf.Rad2Deg;
    变种Y = Mathf.Atan2(vx.z,其中Vx.x)* Mathf.Rad2Deg;
    变种Z = Mathf.Atan2(-vx.y,其中Vx.x)* Mathf.Rad2Deg;

    的debug.log(X:+ X +; Y:+ Y +; Z:+ Z);

    transform.rotation = Quaternion.Euler(X,Y,Z);
}
 
怎样判断两个矢量叉乘后得的第三个矢量的方向

现在我可以通过简单的旋转这样的:

  RotationMaster(Vector3类型(1,0,0),Vector3类型(0,0.707,0.707));
RotationMaster(Vector3类型(1,0,0),Vector3类型(0,-1,0));
 

和,只要我不玩多轴,它会奏效。但只要我尝试更配合物旋转,失败......作为一个例子;

  RotationMaster(Vector3类型(-0.5,-0.5,0.707),Vector3类型(-0.707,0.707,0));
 

这是错误的...: - (

任何线索?

解决方案

  Quaternion.SetLookRotation
 

在(与一些调整...)我一直在寻找!感谢所有您的帮助!

I'm working on a small Unity3D project and I need to import some data from another tool. That tool gives me the object orientation through two vectors, and I need to port that to Unity.

As an example, I have these two vectors;

x = Vector( 0.707, 0.707, 0)
y = Vector(-0.707, 0.707, 0)

With only this information, I need to understand that my object needs to rotate 45 degrees on the Z axis. The two vectors represent the up and right vectors of my object, not a starting point nor a destination.

EDIT

Ok, now I'm able to do single-axis rotation! Yeah! Provided this function;

function RotationMaster (vx : Vector3, vy : Vector3) {
    var vz = Vector3.Cross(vx, vy);

    var x = Mathf.Atan2(-vz.y, vz.z) * Mathf.Rad2Deg;
    var y = Mathf.Atan2( vx.z, vx.x) * Mathf.Rad2Deg;
    var z = Mathf.Atan2(-vx.y, vx.x) * Mathf.Rad2Deg;

    Debug.Log("x : " + x + "; y : " + y + "; z : " + z);

    transform.rotation = Quaternion.Euler(x,y,z);
}

I can now pass simple rotation like that:

RotationMaster(Vector3(1,0,0), Vector3(0,0.707,0.707));
RotationMaster(Vector3(1,0,0), Vector3(0,-1,0));

And as long as I'm not playing with multiple axis, it will work. But as soon as I try a more complexe rotation, it fails... As an example;

RotationMaster(Vector3(-0.5,-0.5,0.707), Vector3(-0.707,0.707,0));

This is wrong... :-(

Any clue?

解决方案

Quaternion.SetLookRotation

was (with few tweaks...) what I was looking for! Thanks to all for your help!