I need to calculate the rotation delta of a smoothed/interpolated transform from frame to frame, so that I can make another object rotate by exactly the same amount every frame. I calculate this rotation delta like this in a system that updates after TransformSystemGroup:
[GenerateAuthoringComponent]
public struct TestRotationCopier : IComponentData
{
public Entity Target;
public LocalToWorld PreviousLocalToWorld;
}
[UpdateAfter(typeof(TransformSystemGroup))]
public class CopyRotationDeltaFromInterpolationSystem : SystemBase
{
protected override void OnUpdate()
{
Entities.ForEach((Entity entity, ref TestRotationCopier rotationCopier) =>
{
// Calculate rotation delta of target
LocalToWorld targetLocalToWorld = GetComponent<LocalToWorld>(rotationCopier.Target);
quaternion targetRotationDelta = math.mul(math.inverse(rotationCopier.PreviousLocalToWorld.Rotation), targetLocalToWorld.Rotation);
// Apply rotation delta to self
LocalToWorld selfLocalToWorld = GetComponent<LocalToWorld>(entity);
selfLocalToWorld.Value = new float4x4(math.mul(targetRotationDelta, selfLocalToWorld.Rotation), selfLocalToWorld.Position);
SetComponent(entity, selfLocalToWorld);
// Remember previous LtW
rotationCopier.PreviousLocalToWorld = targetLocalToWorld;
}).Schedule();
}
}
Now here’s the problem: my other object that’s supposed to rotate by the same amount as the tracked object rotates slightly faster, and also rotates by a huge rotation every now and then. For example, here the camera tries to track the rotation of the orange platform: https://i.gyazo.com/53cb69c19b435a952ae7fa72a45acbcd.mp4
What’s weird is that if instead of using the LocalToWorld.Rotation, I just use the Rotation (from the actual transform), the issue disappears completely (but there’s another issue because now my camera rotation isn’t properly interpolated anymore, which means this causes visual jitter of the platform): https://i.gyazo.com/106d9e5a5a56c4aec062d4c747ca9bb5.mp4