I am writing a very basic air plane game in RealityKit, using the transform property of the plane’s entity as its attitude.
The controls only allow the user to change pitch and roll. To enable the plane to turn I want to rotate it around the world space’s y/yaw axis, when the plane is rotated about’s its roll axis – to v roughly approximate how a plane turns:
let dYaw = changeInYawBasedOn(entity.transform.rotation)
if let parent = entity.parent {
var transform = parent.convert(transform: entity.transform, from: entity)
transform.rotation *= simd_quatf(angle: dYaw, axis: [0, 1, 0])
let newTransform = parent.convert(transform: transform, to: entity)
entity.setOrientation(newTransform.rotation, relativeTo: nil)
}
However this code updates the yaw angle of the plane in its own space, as if I had written:
entity.setOrientation(simd_quatf(angle: dYaw, axis: [0, 1, 0], relativeTo: entity)
So for example if the plane is rolled fully on its side, the plane then angles down toward the ground rather than turning on to a new heading.