setFromTwoVectors method
Implementation
void setFromTwoVectors(Vector3 a, Vector3 b) {
final v1 = a.normalized();
final v2 = b.normalized();
final c = v1.dot(v2);
var angle = math.acos(c);
var axis = v1.cross(v2);
if ((1.0 + c).abs() < 0.0005) {
// c \approx -1 indicates 180 degree rotation
angle = math.pi;
// a and b are parallel in opposite directions. We need any
// vector as our rotation axis that is perpendicular.
// Find one by taking the cross product of v1 with an appropriate unit axis
if (v1.x > v1.y && v1.x > v1.z) {
// v1 points in a dominantly x direction, so don't cross with that axis
axis = v1.cross(Vector3(0.0, 1.0, 0.0));
} else {
// Predominantly points in some other direction, so x-axis should be safe
axis = v1.cross(Vector3(1.0, 0.0, 0.0));
}
} else if ((1.0 - c).abs() < 0.0005) {
// c \approx 1 is 0-degree rotation, axis is arbitrary
angle = 0.0;
axis = Vector3(1.0, 0.0, 0.0);
}
setAxisAngle(axis.normalized(), angle);
}