旋转世界原点周围的物体

时间:2015-02-11 13:31:43

标签: matrix vector unity3d rotation

我目前正在尝试围绕世界的x轴旋转positionVector(0,0,1),然后将其旋转回原来的位置(只是试图让它工作)。我读了旋转矩阵并让它工作(sorta),但我现在很困惑。

当图像和代码显示我在起始点(0,0,1)创建一个立方体并在这种情况下向下旋转30度。但顺时针旋转似乎旋转超过30度。然而,当我逆时针旋转(30度)时,它会旋转适当的量。这导致它不应该以它的起点结束(0,0,1)。

enter image description here


我想知道你们中是否有人能说清楚为什么会这样,以及如何解决这个问题。提前谢谢你们!



public float RotAngle = 330f;

public GameObject cube;
public GameObject original;
public GameObject relocator;
public GameObject initialTurn;

void Start ()
  {
        Vector3 pixelPos = new Vector3(0f, 0f, 1f);
  
        original = GameObject.Instantiate(cube,pixelPos,Quaternion.identity) as GameObject;
        original.name = "Original";


        initialTurn = GameObject.Instantiate(cube, pixelPos, Quaternion.identity) as GameObject;
        initialTurn.name = "InitialTurn";

        relocator = GameObject.Instantiate(cube, pixelPos, Quaternion.identity) as GameObject;
        relocator.name = "Relocator";
  }
void Update()
  {
        initialTurn.transform.position = RotateAroundOrigin(original.transform.position, RotAngle*Mathf.Deg2Rad);
        relocator.transform.position = RotateAroundOrigin(initialTurn.transform.position, (RotAngle * -1f) * Mathf.Deg2Rad);
  }

Vector3 RotateAroundOrigin(Vector3 startPos,float angle)
    {
        startPos.Normalize();
        startPos.y = (startPos.y * Mathf.Cos(angle)) - (startPos.z * Mathf.Sin(angle));
        startPos.z = (startPos.y * Mathf.Sin(angle)) + (startPos.z * Mathf.Cos(angle));
        return startPos.normalized;
    }




1 个答案:

答案 0 :(得分:1)

您可以使用四元数很容易地旋转方向向量。

试试这个:

Vector3 RotateAroundOrigin(Vector3 startPos,float angle)
{
    startPos.Normalize();
    Quaternion rot = Quaternion.Euler(angle, 0.0f, 0.0f); // Rotate [angle] degrees about the x axis.
    startPos = rot * startPos;
    return startPos;
}