Game Development Asked by NoName123 on November 2, 2021
I have a GameObject for which I know its world position and rotation.
From another app, I receive a new rotation and position, which should represent the local position and rotation of my GameObject.
How can I compute the position and rotation of the parent object knowing the (world) position, the (world) rotation, the localPosition and the localRotation of its child? How can I find the necessary transform to obtain the second pair of coordinates from the first pair of coordinates?
If you want a parent world rotation$P_W$ that takes the child local rotation $C_L$ to the provided child world rotation $C_W$, we can express that as an equation:
$$C_W = P_W cdot C_L$$
Then you can simply solve for the parent rotation algebraically:
$$C_W cdot C_L^{-1}= P_W cdot C_L cdot C_L^{-1} = P_W cdot I = P_W$$
So, parent.rotation = childTargetWorldRotation * Quaternion.Inverse(child.localRotation);
(Note that the order of the multiplication here is important - putting the inverse on the left side of the multiplication does not produce the same rotation, in general)
Now that we know the rotation, we can work out how that transforms the parent-to-child vector:
Vector3 worldOffset = parent.TransformVector(child.localPosition);
And then reverse that vector to find where to place the parent:
parent.position = childTargetWorldPosition - worldOffset;
Answered by DMGregory on November 2, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP