src/tools/quaternion.cpp
// Returns a quaternion such that q*start = dest

Returns a quaternion such that q*start = dest
Returns a quaternion such that q * start = dest
quat rotationBetweenVectors(vec3 start, vec3 dest) {
	start = normalize(start);
	dest = normalize(dest);
	float cos_theta = dot(start, dest);
	vec3 rotation_axis;
	if (cos_theta < -1 + 0.001f) {
		rotation_axis = cross(vec3(0.0f, 0.0f, 1.0f), start);
		if (length2(rotation_axis) < 0.01 )
			rotation_axis = cross(vec3(1.0f, 0.0f, 0.0f), start);
		rotation_axis = normalize(rotation_axis);
		return angleAxis(180.0f, rotation_axis);
	}
	rotation_axis = cross(start, dest);
	float s = sqrt( (1 + cos_theta) * 2 );
	float invs = 1 / s;
	return quat(
	           s * 0.5f,
	           rotation_axis.x * invs,
	           rotation_axis.y * invs,
	           rotation_axis.z * invs
	       );
}
quat rotationBetweenVectors ( vec3 start , vec3 dest ) { start = normalize ( start ) ; dest = normalize ( dest ) ; float cos_theta = dot ( start , dest ) ; vec3 rotation_axis ; if ( cos_theta < -1 + 0.001f ) { rotation_axis = cross ( vec3 ( 0.0f , 0.0f , 1.0f ) , start ) ; if ( length2 ( rotation_axis ) < 0.01 ) rotation_axis = cross ( vec3 ( 1.0f , 0.0f , 0.0f ) , start ) ; rotation_axis = normalize ( rotation_axis ) ; return angleAxis ( 180.0f , rotation_axis ) ; } rotation_axis = cross ( start , dest ) ; float s = sqrt ( ( 1 + cos_theta ) * 2 ) ; float invs = 1 / s ; return quat ( s * 0.5f , rotation_axis . x * invs , rotation_axis . y * invs , rotation_axis . z * invs ) ; }
Returns a quaternion such that q*start = dest
Returns a quaternion such that q * start = dest