Skip to content

Commit cd2faa4

Browse files
committed
Fix natural up direction when defined inside a transformed scope
The composition inverted the captured definition transform, producing the wrong direction whenever definingNaturalUpDirection was applied below a transform in the chain. Querying from the same local frame returned a direction rotated by the captured transform's square rather than the original direction. Masked in existing tests because they all defined the up direction at the outermost level (identity transform).
1 parent 016e687 commit cd2faa4

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

Sources/Cadova/Abstract Layer/Environment/Values/NaturalUpDirection.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public extension EnvironmentValues {
3333
///
3434
var naturalUpDirection: Direction3D {
3535
let upDirection = naturalUpDirectionData
36-
let upTransform = upDirection.transform.inverse.concatenated(with: transform.inverse)
36+
let upTransform = upDirection.transform.concatenated(with: transform.inverse)
3737
return Direction3D(upTransform.apply(to: upDirection.direction.unitVector) - upTransform.offset)
3838
}
3939

Tests/Tests/NaturalUpDirection.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,30 @@ struct NaturalUpDirectionTests {
2424
.triggerEvaluation()
2525
}
2626

27+
@Test func `up direction is preserved when defined inside a rotated scope`() async throws {
28+
try await Box(1)
29+
.readingEnvironment(\.naturalUpDirection) { body, direction in
30+
#expect(direction .up)
31+
body
32+
}
33+
.definingNaturalUpDirection(.up)
34+
.rotated(x: 90°)
35+
.triggerEvaluation()
36+
}
37+
38+
@Test func `up direction is transformed correctly by surrounding rotation`() async throws {
39+
// Outer up = world +Y. Inside a +90° rotation around X, the local axis that
40+
// points to world +Y is local -Z.
41+
try await Box(1)
42+
.readingEnvironment(\.naturalUpDirection) { body, direction in
43+
#expect(direction .negativeZ)
44+
body
45+
}
46+
.rotated(x: 90°)
47+
.definingNaturalUpDirection(.positiveY)
48+
.triggerEvaluation()
49+
}
50+
2751
@Test func `perpendicular direction returns nil XY angle`() async throws {
2852
try await Box(1)
2953
.readingEnvironment(\.naturalUpDirectionXYAngle) { body, angle in

0 commit comments

Comments
 (0)