Extreme necrobump, but I just found that:math.mul(quaternion.AxisAngle(), float3) does not equal: math.mul(Quaternion.AngleAxis(), float3) Quaternion’sAngleAxis() gives the correct result whereas *quaternion’s.*AxisAngle() gives the wrong result, where both receive the same input. Why? Did I miss something?
Welcome to our club. We are all friendly here, and no one needs to be ashamed. Coffee and soft drinks are free and everyone get’s their own yellow rubber duck on their first evening.
using static math;
/// <summary>
/// Returns the angle in radian between /from/ and /to/. This is always the smallest
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Radian(float3 from, float3 to)
{
// sqrt(a) * sqrt(b) = sqrt(a * b) -- valid for real numbers
var denominator = sqrt(lengthsq(from) * lengthsq(to));
if (denominator < UnityEpsilonNormalSqrt) return 0F;
var d = clamp(dot(from, to) / denominator, -1F, 1F);
return acos(d);
}
/// <summary>
/// Returns the angle in degrees between /from/ and /to/. This is always the smallest
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Angle(float3 from, float3 to) => degrees(Radian(from, to));
/// <summary>
// The smaller of the two possible radian between the two vectors is returned, therefore the result will never be greater than 180 degrees or smaller than -180 degrees.
// If you imagine the from and to vectors as lines on a piece of paper, both originating from the same point, then the /axis/ vector would point up out of the paper.
// The measured angle between the two vectors would be positive in a clockwise direction and negative in an anti-clockwise direction.
/// </summary>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float SignedRadian(in float3 from, in float3 to, in float3 axis)
{
var unsignedRadian = Radian(from, to);
if (unsignedRadian == 0) return 0F;
var s = sign(dot(cross(from, to), axis));
return s > 0 ? unsignedRadian : -unsignedRadian;
}
/// <summary>
// The smaller of the two possible angles between the two vectors is returned, therefore the result will never be greater than 180 degrees or smaller than -180 degrees.
// If you imagine the from and to vectors as lines on a piece of paper, both originating from the same point, then the /axis/ vector would point up out of the paper.
// The measured angle between the two vectors would be positive in a clockwise direction and negative in an anti-clockwise direction.
/// </summary>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float SignedAngle(in float3 from, in float3 to, in float3 axis) => degrees(SignedRadian(from, to, axis));
Yeah, that’s a little weird. This is on Unity 2019.4.18, Unity Mathematics 1.2.1.
Fortunately, in my case, I can do the quaternion.Inverse before going into jobs … which is better in terms of performance, anyways. But turns out, in 1.2.1, math has it, so it’s math.inverse(quaternion). But 1.2.1 also seems kind of old, now