局部空间下,如果parent没有任何旋转child的坐标直接等于parent的坐标+child的坐标,但是如果parent有旋转,child的坐标增量是按parent的旋转轴增量来增加
所以就会有unity的这个计算局部坐标物体在世界空间的坐标函数。
void Start()
{
//selfTran = transform as RectTransform;
Debug.LogError($"{obj.transform.localToWorldMatrix}");
Matrix4x4 vp = Matrix4x4.TRS(obj.transform.position, obj.transform.rotation, obj.transform.localScale);
Debug.LogError($"vp:{vp}");
Vector3 np = vp * new Vector4(2,2,0,1);
Debug.LogError($"np:{np}");
//Matrix4x4 mm = Matrix4x4.TRS(Vector3.zero,Quaternion.Euler(0.53f,0,0),Vector3.one) * vp; // 移动的时候会选择父亲的旋转方向去移动
Quaternion qq = obj.transform.rotation* Quaternion.Euler(53f, 80, 62);
Debug.LogError($"rp:{qq.eulerAngles}");
}
这是我的一段测试代码,上面函数本质上等于利用parent的对象矩阵*局部坐标,记得v4最后一个数要是1。
而旋转的话也是根据parent的旋转先转完在执行自己的欧拉旋转。
而且eulerangles不唯一,不同的eulerangles值可能代表的是相同的旋转朝向,因为存在不同的旋转轴顺序。quaternion的顺序是从左往右。