0
\$\begingroup\$

I have attached two images with the desired start and end positions.

The Brown Circle is the target rotation. The Orange Square is the parent (the black dot is the pivot point) and the Blue Rounded Square is the child.

How would I calculate the rotation of the parent to align the child to the target rotation?

Start

End

\$\endgroup\$

2 Answers 2

0
\$\begingroup\$

Assuming you have a bearing angle for each object relative to its parent, this is just subtraction.

We want:

parent_angle + child_angle = target_angle

parent_angle = target_angle - child_angle

So in your example, the child is rotated about 45 degrees clockwise from its parent - call that -45 - and the target is unrotated at 0 degrees, so that gives:

parent_angle = 0 - (-45)
             = +45

So the parent needs to be rotated 45 degrees counter-clockwise to compensate for the child's rotation and match it to the target.

This expression can wrap around to values outside the -180 to 180 or 0 to 360 or -pi to pi etc. range you might be using, but you can wrap the result with an angle difference function, if that matters for your use case. Prior Q&A covers how to write such a function, if your math library does not offer one built-in.

\$\endgroup\$
2
  • \$\begingroup\$ Thank you! That is exactly what I was looking for. I initially thought the distance from the parent to the child would affect the final rotation but you cleared that up. \$\endgroup\$ Commented Aug 3, 2022 at 8:04
  • \$\begingroup\$ Be sure to click the checkmark on one of these answers to mark it as "accepted". No hard feelings if you choose the other one — they did beat me to it. 😉 \$\endgroup\$
    – DMGregory
    Commented Aug 3, 2022 at 9:34
0
\$\begingroup\$

I think this might be one of those things that seems trickier than it really is. Since rotations are rigid transformations of space, all vectors are affected equally no matter where in the plane they are. What that means is that we can completely ignore the fact that the pivot point is the parent position, and just think about how much the child needs to rotate.

And so, all you need to do is find the angle between the current orientation and the desired one. For example, the angle between the child's horizontal vector and the target's horizontal vector. You then rotate the parent by that amount, and done!

An important note is that you need to express both vectors in the same coordinate system. So, if the child is expressed in the local space of its parent, you might need to use a transformation matrix or something. The exact steps will of course depend on how all this information is made available in whatever tool you are using.

\$\endgroup\$
1
  • \$\begingroup\$ Thank you, this is what I was aiming for. I didn't know the formula to achieve it. \$\endgroup\$ Commented Aug 3, 2022 at 5:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .