Thanks. My autocompletion barely works at times c3.xyz doesn’t show up. It’ll do the trick, I mean it’s not intuitive I had to dig up an old forum post to see that that’s how world position is done. Scale is not pretty. Hope the methods will be added soon.
Minor complaint I think math.transform(float3 point, float4x4 matrix) and math.mul(float3 vector, quaternion rotation) is more intuitive than when arguments are other way around. I’m rotating vector, taking vector and applying rotation. But when I try to do that, it acts like it’s an impossible operation until you realize that it let’s you do it if you switch vector and matrix around.
‘yes’ but it’s a tad more challenging if there is scale.
If there is no scale, just
var rotation = new quaternion(localToWorld.Value);
However if there is scale, you need to remove it first
public static quaternion Rotation(this LocalToWorld localToWorld)
{
var scale = localToWorld.Value.GetScale();
var inverted = scale.Invert();
var value = localToWorld.Value.ScaleBy(inverted); // remove the scale
return new quaternion(value);
}
(GetScale, Invert and ScaleBy are other extensions of mine. let me know if you need)
Finally got around to testing, this doesn’t seem to work with scale. Please correct me if I’m wrong, but here is my test.
[Test]
public void Rotation()
{
var scale = new float3(12, 33, 123);
var e = new float3(0.1f, 0.2f, 0.3f);
var position = new float3(0, 0, 0);
var rotation = quaternion.EulerXYZ(e);
var localToWorld = new LocalToWorld
{
// seems to produce same result
// Value = float4x4.TRS(position, rotation, scale),
// But this is what Transform system uses so replicate
Value = math.mul(new float4x4(rotation, position), float4x4.Scale(scale)),
};
var result = quaternion.LookRotation(localToWorld.Forward, localToWorld.Up);
AssertMath.AreApproximatelyEqual(rotation, result, 0.00001f);
}
Using LookRotationSafe seems to produce the correct result (and matches what I was previously doing.)
var result = quaternion.LookRotationSafe(localToWorld.Forward, localToWorld.Up);
The direction vectors aren’t normalized when there is scale.
/// <summary>
/// Returns a quaternion view rotation given a unit length forward vector and a unit length up vector.
/// The two input vectors are assumed to be unit length and not collinear.
/// If these assumptions are not met use float3x3.LookRotationSafe instead.
/// </summary>
public static quaternion LookRotation(float3 forward, float3 up)
If you normalize the direction vectors LookRotation again produces expected result
var result = quaternion.LookRotation(math.normalize(localToWorld.Forward), math.normalize(localToWorld.Up));