Skip to content

Commit 711f9cf

Browse files
authored
fix: TransformComponent.SetWorld rotation multiplication order (#3087)
1 parent 211c987 commit 711f9cf

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

sources/engine/Stride.Engine.Tests/TestTransformComponent.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,5 +78,26 @@ public void TestSetWorldTransformation()
7878
Assert.Equal(Vector3.Zero, pos);
7979
Assert.Equal(Quaternion.Identity, rot);
8080
}
81+
82+
[Fact]
83+
public void TestSetWorldTransformationNested()
84+
{
85+
var scene = new Scene();
86+
var parent = new Entity { Scene = scene }.Transform;
87+
var child = new Entity { Transform = { Parent = parent } }.Transform;
88+
var targetPosition = new Vector3(2, 2, 2);
89+
var targetRotation = Quaternion.RotationY(MathF.PI * 0.1f);
90+
91+
parent.Position = new Vector3(1, 1, 1);
92+
parent.Rotation = Quaternion.RotationX(MathF.PI * 0.05f);
93+
94+
child.UpdateWorldMatrix();
95+
child.SetWorld(targetPosition, targetRotation);
96+
child.UpdateWorldMatrix();
97+
98+
child.GetWorldTransformation(out var position, out var rotation, out _);
99+
Assert.Equal(targetPosition, position);
100+
Assert.Equal(targetRotation, rotation);
101+
}
81102
}
82103
}

sources/engine/Stride.Engine/Engine/EntityTransformExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ public static void SetWorld(this TransformComponent transformComponent, Vector3
278278
Vector3.Transform(ref position, ref worldMatrixInv, out position);
279279

280280
worldMatrix.Decompose(out _, out Quaternion parentRot, out _);
281-
rotation = Quaternion.Invert(parentRot) * rotation;
281+
rotation = rotation * Quaternion.Invert(parentRot);
282282

283283
transformComponent.Position = position;
284284
transformComponent.Rotation = rotation;
@@ -335,7 +335,7 @@ public static void SetWorld(this TransformComponent transformComponent, Quaterni
335335
}
336336

337337
transformComponent.Parent.WorldMatrix.Decompose(out _, out Quaternion parentRot, out _);
338-
transformComponent.Rotation = Quaternion.Invert(parentRot) * rotation;
338+
transformComponent.Rotation = rotation * Quaternion.Invert(parentRot);
339339
}
340340
}
341341
}

0 commit comments

Comments
 (0)