  # Look at behavior + Z.Vec3 and Z.Mat documentation

#1

Hey,

Any possibility of providing some docs (or hints) for functions in Z.Vec3 and Z.Mat? I’m especially interested in Z.Mat.createRotationMatrix() and Z.Mat.anglesFromRotationMatrix().

I’m trying to build up a ‘look at’ function for rotating objects towards a specified point. I’ve made a heading vector from the objects current Euler angles and another one pointing towards my target point. If I’m not entirely mistaken, I can use atan2 to get the required rotation angle and define the axis to rotate around with a cross product. My problem lies in applying this angle and rotation-axis to the Euler angles of my zappar object.

I was wondering if the Z.Mat functions could help me with this issue. Math certainly isn’t my strong suit but I still feel like I might be fairly close with this. Any help would be appreciated!

Z.Mat.anglesFromRotationMatrix freezes app
#2

Hey Ero,

Good catch, look like we missed them when documenting.

While we work on doing so the platform team provided some descriptions below which will hopefully help:

``````// reference: http://www.euclideanspace.com/maths/geometry/rotations/conversions/angleToMatrix/index.htm
matrixFromAxisAngle(axis, angle) {
var c = Math.cos(angle);
var s = Math.sin(angle);
var t = 1.0 - c;

var m00 = c + axis*axis*t;
var m11 = c + axis*axis*t;
var m22 = c + axis*axis*t;

var tmp1 = axis*axis*t;
var tmp2 = axis*s;
var m10 = tmp1 + tmp2;
var m01 = tmp1 - tmp2;
tmp1 = axis*axis*t;
tmp2 = axis*s;
var m20 = tmp1 - tmp2;
var m02 = tmp1 + tmp2;
tmp1 = axis*axis*t;
tmp2 = axis*s;
var m21 = tmp1 + tmp2;
var m12 = tmp1 - tmp2;

return [m00, m01, m02,
m10, m11, m12,
m20, m21, m22];
}

normalize(vec) {
return Z.Vec3.scalarMultiply(forwardVec, 1.0 / Z.Vec3.magnitude(forwardVec));
}

cross(vecA, vecB) {
var x = vecA * vecB - vecB * vecA;
var y = vecA * vecB - vecB * vecA;
var z = vecA * vecB - vecB * vecA;

return [x, y, z]
}

lookAt(forwardVec, targetVec) {
// normalize
var forward = normalize(forwardVec);
var target = normalize(targetVec);

// compute the rotation axis
var axis = cross(forward, target);

// compute the angle
var cosTheta = Z.Vec3.dot(forward, target);
var angle = Math.acos(cosTheta);

// compute the rotation matrix
var rotM = matrixFromAxisAngle(axis, angle);

// get the Euler angels
return Z.Mat.anglesFromRotationMatrix(rotM);
}

// let's say, the object's position is objPos with the defined forward vector as objForward, and the look at target's postion is targetPos, then call
var angles = lookAt(objForward, Z.Vec3.subtract(targetPos, objPos));
``````

Cheers,
Mark

#3

Hey Mark,

Thank you! That helps a lot.

I managed to implement this behavior with simpler math, but with separate transform groups for pitch and yaw. I think what you posted makes things cleaner though so I’ll probably switch over to that.

I used this behavior to implement hacky line drawing between points and it’s working quite well. Probably not the best performance-wise, but it works for now. Now this made me think of more feature requests. Thanks again!

#4

This function blows up and freezes the whole app.

#5

Thanks for the report, it does indeed look to be broken (the implementation tries to reference `Z` which is actually a closure variable only available in script nodes - it’s possible this wasn’t the case when the code was written and no one has noticed before, our set of regression tests don’t exercise this function).

The simplest approach is just to avoid `Z.Mat.anglesFromRotationMatrix()` and just include the function locally instead:

``````function anglesFromRotationMatrix(mat, compare?) {
if (!compare) { compare = [0,0,0]; };

var x1 = 0;
var y1 = 0;
var z1 = 0;
var x2 = 0;
var y2 = 0;
var z2 = 0;

var m00 = mat;
var m01 = mat;
var m02 = mat;
var m12 = mat;
var m22 = mat;

var m10 = mat;
var m20 = mat;

if (Math.abs(m02) < 0.99999 ) {
y1 = Math.asin(m02);
y2 = Math.PI - y1;

x1 = Math.atan2( -m12 / Math.cos(y1), m22 / Math.cos(y1) );
x2 = Math.atan2( -m12 / Math.cos(y2), m22 / Math.cos(y2) );

z1 = Math.atan2( -m01 / Math.cos(y1), m00 / Math.cos(y1) );
z2 = Math.atan2( -m01 / Math.cos(y2), m00 / Math.cos(y2) );
} else if (m02<0) {
// m10 =  cx * sz - sx * cz = sin(z - x);
// m20 =  sx * sz + cx * cz = cos(z - x);
y1 = y2 = -Math.PI / 2;
x1 = x2 = compare;
z1 = z2 = x1 + Math.atan2(m10, m20);
} else {
// m10 =  cx * sz + sx * cz = sin(x + z);
// m20 =  sx * sz - cx * cz = -cos(x + z);
y1 = y2 = Math.PI / 2;
x1 = x2 = compare;
z1 = z2 = Math.atan2(m10, -m20) - x1;
}

var v1 = [x1, y1, z1];
var v2 = [x2, y2, z2];

v1 += (2 * Math.PI) * Math.round((compare - v1) / (2 * Math.PI));
v1 += (2 * Math.PI) * Math.round((compare - v1) / (2 * Math.PI));
v1 += (2 * Math.PI) * Math.round((compare - v1) / (2 * Math.PI));

v2 += (2 * Math.PI) * Math.round((compare - v2) / (2 * Math.PI));
v2 += (2 * Math.PI) * Math.round((compare - v2) / (2 * Math.PI));
v2 += (2 * Math.PI) * Math.round((compare - v2) / (2 * Math.PI));

var distV1 = Z.Vec3.magnitude( Z.Vec3.subtract(v1, compare) );
var distV2 = Z.Vec3.magnitude( Z.Vec3.subtract(v2, compare) );

if (distV1 > distV2) {
return v2;
}
return v1;
}
``````

I think the idea of “compare” is you can pass in the current rotation angles and it should give you the minimum rotation, so if you’re already at [0, 0, 350] and the result needs to be [0, 0, 10] it will give you [0, 0, 370] instead.

Leaving this here for anyone who needs to do a “look at” within a scene. For Instant Tracking / World Tracking placement though there is an easier way - will reply to the other thread on that.

Z.Mat.anglesFromRotationMatrix freezes app