# “aim at” in 3D space algorithm

I'd to calculate the cartesian rotate values of "aiming" one point at another in 3D space.

For example, assuming rotation order of XYZ:
Point A is at [0, 0, 0] Point B is at [2, 2, 0]

If point A was "aiming" at point B with primary (aim) axis X, secondary (up) Y; point A's rotate values would be [0, 0, 45]. These values are what I'd like to calculate.

It's assumed that point A is at the origin. Let d be the unit vector in the direction point A is currently looking at, and p be the unit vector in the direction of B - A.

Notice that d and p necessarily define a plane in 3D-space. If you can imagine this, then it becomes clear that a single rotation of d by an amount equal to the angle between these two vectors about the normal to this plane will orient point A towards point B. The goal is to find out what the angle between these two vectors and the normal to the plane they define are. (Don't worry, I will get to Euler Angles soon!)

With basic linear algebra we have (I am assuming a familiarity with these operations):

acos(dot(d,p)) = the angle between d and p, I'll call it theta.

cross(d, p)/length(cross(d,p)) = unit normal to the plane defined by d and p, I'll call it n.

With an angle to rotate by and an axis of rotation, we can define a quaternion to represent this rotation. (If you aren't familiar with quaternions at all, it's fine; they're only used as an intermediate step here to get to the Euler angles you want.)

This quaternion will be equal to:

q = cos(theta/2) + (nxi + nyj + nzk) * sin(theta/2), where nx is the x-component of n, and so on.

Finally, there is the formula for converting from quaternion to Euler angle representation of a rotation.

and in our case:

q0 = cos(theta/2)

q1 = sin(theta/2) * nx

q2 = sin(theta/2) * ny

q3 = sin(theta/2) * nz