Given a parent object A and a child object B, how do I find the rotation of A such that B is pointing toward the position of a target C.

This is a simplified way of solving 'what angle does the torso need to be so that the hand is aiming a weapon at a target?'.

I can solve easily in 2D by iteratively checking how much B needs to turn to look at C and then applying that rotation delta to A, but in 3D this method seems to breaks down.

Does this problem have a known name, and what types of algorithms should I be researching in order to solve it?

Initial state Initial state

Desired state Desired state

  • \$\begingroup\$ Think about the problem backwards. How would you rotate C around the origin of A so that it lines up with B. That should be an easier rotation to find, then apply the inverse to A \$\endgroup\$ Commented Aug 28, 2017 at 7:15
  • \$\begingroup\$ Cross product of two vectors would work would it not ? how are your objects attatched ? \$\endgroup\$
    – RNewell122
    Commented Aug 28, 2017 at 8:03
  • \$\begingroup\$ @RNewell122 Not sure what you mean. Cross product of which two vectors? Objects are attached using an ordinary parent-child relationship where B is defined in the space of A. \$\endgroup\$
    – Rotem
    Commented Aug 28, 2017 at 8:44
  • \$\begingroup\$ @Rotem youtube.com/watch?v=Q9FZllr6-wY <- this video explains cross and dot product, you use it to know the angle between two vectors, its in 2d but you just convert it to 3d, and then you do an while B's dot product with A is <= 1 then turn in a movement vector towards it.You'll have to convert your dot to radians, its explained better in videos. \$\endgroup\$
    – RNewell122
    Commented Aug 28, 2017 at 12:02

1 Answer 1


Think about the problem backwards, how would we rotate C around A so that it lines up with B. If we can solve this problem, we just need to rotate A by the same amount, but in the opposite direction.

If you're familiar with linear algebra, this should be pretty straight forward. If you're not, I can try and write up some pseudo-code to complement this answer.

shitty diagram

From the above diagram, we can see that B (the gun) forms a line if sight (green), a straight line (2D or 3D), we know that we want C somewhere along this line. Since we're rotating around A, the distance (d) between A and C will remain the same, so we just need to find the 2 points (x1 and x2) along the line which satisfy this. Construct a right angle triangle (A x3 x2), where x3 is the point on the line closest to A. Using the Pythagorean theorem, we can get y. And from y we can get both x1 and x2. Choose the point which point is in front of the gun, not behind. Finally we can get the axis of rotation, and the angle of rotation (theta). The axis can be obtained by the cross product between the vectors (x2-A)x(C-A). The angle (theta) can be found by taking the inverse cosine of the dot product between the same vectors after normalization.

That should give you the inverse rotation, so just rotate A by negative theta, around the same axis.

I haven't walked through the math myself, but is should simplify into a fairly short algorithm.

Note: in 2D there is only 1 solution, but in 3D there are infinite rotations that satisfy your problem. My solution will find the smallest rotation, which may not be the most natural looking for a guy aiming a gun.

  • \$\begingroup\$ Thank you, that is very well explained. I've followed your method and have verified that I am able to get the correct point x2, yet still fail on applying the inverse rotation. I will continue to try. \$\endgroup\$
    – Rotem
    Commented Aug 28, 2017 at 8:42
  • \$\begingroup\$ Ok, got it working, was missing the acos after the dot product. Thanks again. \$\endgroup\$
    – Rotem
    Commented Aug 28, 2017 at 9:00
  • \$\begingroup\$ After some more thought I've realised there's probably an easier way. If this is just for a dude to aim a gun, then you could make him aim horizontally first, then just adjust vertically. But I'm super happy you've got the ideal solution working :) \$\endgroup\$ Commented Aug 28, 2017 at 11:48

You must log in to answer this question.

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