从场景图中获取绝对位置和旋转?
所以我多次遇到这个问题:
我的 3D 场景图中有一些对象,它是其他对象的子对象。 我们称它们为 c(子级)和 p(父级)。
c 的位置是相对于 p 定义的。 因此 c 可能有一个位置 (1,0,0) 但当然,由于 p 有一些其他位置,比如 (1,2,3),它实际上并不是在我们世界的原始位置渲染的,而是在 (2 ,2,3)。
现在假设出于某种原因我们想知道 c 在世界坐标中的绝对位置(或旋转,问题是相同的),这通常是如何完成的?
c 应该知道它的父级并能够查询该位置并将其添加到它自己的位置并最终返回绝对位置吗?
So I have run up against this problem a few times:
I have some object in my 3D Scene Graph which is a child of some other object. Lets call them c (Child) and p (Parent).
c's position is defined relative to p. Thus c may have a position (1,0,0) but of course, due to p having some other position, say (1,2,3), it is not actually rendered at the original of our world, but at (2,2,3).
Now lets say for some reason we want to know c's absolute position in world coordinates (or rotation, the problem is the same), how is this commonly done?
Should c know about it's parent and be able to query for that position and add it to it's own finally returning an absolute position?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将子坐标系中的坐标转换为全局坐标系是实际渲染场景图所需的事情之一,因此这是一个合理的事情。
如何完成此操作取决于您使用的图形库。 通常场景图中的每个节点都会包含一个矩阵,该矩阵将该节点的坐标系转换为全局坐标。 该矩阵的计算方法是将父节点的矩阵与从父节点坐标到子坐标的变换矩阵相乘,反之亦然,具体取决于事物的定义方式。
如果您有这样的矩阵,您可以通过将子坐标中的点乘以节点的矩阵来进行所需的计算。 再次强调,顺序在这里很重要。
Converting coordinates in a child's coordinate system to the global coordinate system is one of the things that is needed to actually render the scene graph, so this is a reasonable thing to want.
How you accomplish this will depend on the graphics library you are using. Often each node in the scene graph will include a matrix which transforms that node's coordinate system into the global coordinates. This matrix is calculated by multiplying the matrix from the parent node with the transformation matrix from the parent's coordinates to the child coordinates, or vice versa depending on how things are defined.
If you have a matrix like this, you can do the calculation you want by multipling the point in child coordinates by the node's matrix. Again, order is important here.
恕我直言,父对象了解其子对象比相反更常见。 哪个适合您取决于子级是否需要知道自己的位置,或者是否是其他一些外部树遍历算法需要知道它。
另外,我认为旋转不能像平移一样处理。 如果旋转父对象,它也会平移子对象的位置。 因此,为什么大多数 3D 系统使用齐次坐标,齐次坐标使用 (4x4) 变换矩阵,将旋转和平移结合在一个矩阵中。
IMHO, it's more usual for parent objects to know about their children, than the other way around. Which is right for you depends on whether the child needs to know its own position, or whether it's some other external tree traversal algorithm that needs to know it.
Also, I don't think rotation can be handled the same as translation. If the parent object is rotated it'll also translate the position of the child object. Hence why most 3D systems use homogenous coordinates which use (4x4) transformation matrices which combine both rotations and translations in one matrix.