setFromUnitVectors method

Quaternion setFromUnitVectors(
  1. Vector3 vFrom,
  2. Vector3 vTo
)

Implementation

Quaternion setFromUnitVectors(Vector3 vFrom, Vector3 vTo) {
  // assumes direction vectors vFrom and vTo are normalized

  double r = vFrom.dot(vTo) + 1;

  if (r < MathUtils.epsilon) {
    r = 0;

    if (vFrom.x.abs() > vFrom.z.abs()) {
      _x = -vFrom.y;
      _y = vFrom.x;
      _z = 0;
      _w = r;
    } else {
      _x = 0;
      _y = -vFrom.z;
      _z = vFrom.y;
      _w = r;
    }
  } else {
    // cross2( vFrom, vTo ); // inlined to avoid cyclic dependency on Vector3

    _x = vFrom.y * vTo.z - vFrom.z * vTo.y;
    _y = vFrom.z * vTo.x - vFrom.x * vTo.z;
    _z = vFrom.x * vTo.y - vFrom.y * vTo.x;
    _w = r;
  }

  return normalize();
}