Unity中世界坐标与相对于某个物体的相对坐标的转换

最近在开发XR会议, 会上报一些姿态信息在多个端侧进行同步,由于在不同的端侧定位的位置是不一样的,如果你上传的是世界坐标,其实你在另个端侧根本就不能定位出来。

所以我们在上报姿态信息时,都是相对于某个物体的相对坐标,然后在端侧在进行转换成相对坐标。

设定roomObject为你要改变的相对物体,

世界坐标转换成相对于roomObject这个物体的相对坐标:

 Vector3 headLocalPosition = roomObject.transform.InverseTransformPoint(headWorldPosition);
 Quaternion headLocalRotation = Quaternion.Inverse(roomObject.transform.rotation) * headWorldRotation;

相对于roomObject这个物体的相对坐标转换成世界坐标:

 Vector3 headWorldPosition= roomObject.transform.TransformPoint(headLocalPosition);
 Quaternion headWorldRotation= roomObject.transform.rotation * headLocalRotation;

这样,我们的姿态信息在多个端侧进行流转时,我们统一转换成相对与某个物体的坐标,然后发送给其他端侧,其他端侧在根据相对物体转换成世界坐标。

参考文献:

What is the rotation equivalent of InverseTransformPoint? - Unity Answers

What's the math behind transform.TransformPoint()? - Unity Forum

猜你喜欢

转载自blog.csdn.net/grace_yi/article/details/123360689
今日推荐