问题
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?
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/351197/getting-absolute-position-and-rotation-from-inside-a-scene-graph