How does "get look at rotation" work from a mathematical point of view

Hello, please help me for the research article “applications of mathematical functions in unreal engine” I would like to know how the “get look at rotation” function works from a mathematical point of view, I understand how it works, but I wonder what it does with two vectors to convert them to Euler angles, I assume that first it looks for the product of two vectors to determine the axis of rotation is then probably looking for a scalar product and somehow with the help of the rotation matrix converts them into Euler angles. I would appreciate your help

Hey Weytant! I don’t know if this is the exact code, but it’s derivative of this:

sfquat LookAt(sfvec3f target,sfvec3f current, sfvec3f eye,sfvec3f up) {
// turn vectors into unit vectors
n1 = (current - eye).norm();
n2 = (target - eye).norm();
d = sfvec3f.dot(n1,n2);
// if no noticable rotation is available return zero rotation
// this way we avoid Cross product artifacts
if( d > 0.9998 ) return new sfquat( 0, 0, 1, 0 );
// in this case there are 2 lines on the same axis
if(d < -0.9998){
n1 = n1.Rotx( 0.5f );
// there are an infinite number of normals
// in this case. Anyone of these normals will be
// a valid rotation (180 degrees). so rotate the curr axis by 0.5 radians this way we get one of these normals
}
sfvec3f axis = n1;
axis.cross(n2);
sfquat pointToTarget= new sfquat(1.0 + d,axis.x,axis.y,axis.z);
pointToTarget.norm();
// now twist around the target vector, so that the ‘up’ vector points along the z axis
sfmatrix projectionMatrix=new sfmatrix();
double a = pointToTarget.x;
double b = pointToTarget.y;
double c = pointToTarget.z;
projectionMatrix.m00 = bb+cc;
projectionMatrix.m01 = -ab;
projectionMatrix.m02 = -a
c;
projectionMatrix.m10 = -ba;
projectionMatrix.m11 = a
a+cc;
projectionMatrix.m12 = -b
c;
projectionMatrix.m20 = -ca;
projectionMatrix.m21 = -c
b;
projectionMatrix.m22 = aa+bb;
sfvec3f upProjected = projectionMatrix.transform(up);
sfvec3f yaxisProjected = projectionMatrix.transform(new sfvec(0,1,0);
d = sfvec3f.dot(upProjected,yaxisProjected);
// so the axis of twist is n2 and the angle is arcos(d)
//convert this to quat as follows
double s=Math.sqrt(1.0 - dd);
sfquat twist=new sfquat(d,n2
s,n2s,n2s);
return sfquat.mul(pointToTarget,twist);

There are a ton of small equations used, that generates the arcos dot product.
Now, this one returns quaternions, there is a simpler LookAt function in mathematics, however it does not apply rotation and assumes a standard starting point. This is almost never applicable in games since the starting point needs to be anywhere.

2 Likes

A rotation is just the “forward, side, up” vectors of the basis (tripod) stored in the matrix columns (or rows, depending on your convention.)

Look-at is a pretty simple operation:

  1. Calculate Target - Eye, and normalize. This is your forward vector. (X)
  2. Cross world up with forward, and normalize. This generates your side vector. (Y)
  3. Cross your forward and side vector. This generates your look up vector. (Z)

The matrix you just calculated is the forward rotation – taking a camera, point the camera at the target. If you want the reverse rotation – transform an object into the camera’s space – then invert the matrix, which ends up being the same as the transpose for an orthonormal matrix like this.

2 Likes