rotate an entity in Realitykit

I am having trouble rotating an entity that is added in Reality Composer but rotated in code. I decided to follow the information in the Developer Documentation called Manipulating Reality Composer Scenes from Code. (https://developer.apple.com/documentation/realitykit/creating_3d_content_with_reality_composer/manipulating_reality_composer_scenes_from_code)
I am starting with a default ARKit program that just displays the Steel Box. When I add code to rotate the box 45° it also shrinks the box along the x axis. The cube gets flattened about half way.
Is there a way to rotate without it scaling at the same time?
Code Block
import UIKit
import RealityKit
class ViewController: UIViewController {
    
    @IBOutlet var arView: ARView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Load the "Box" scene from the "Experience" Reality File
        let boxAnchor = try! Experience.loadBox()
        
        // Add the box anchor to the scene
        arView.scene.anchors.append(boxAnchor)
        
        if let box = boxAnchor.findEntity(named: "lid") {
            let radians = -45.0 * Float.pi / 180.0
            box.transform.rotation += simd_quatf(angle: radians, axis: SIMD3<Float>(1,0,0))
        }
        
    }
}

I have tried adding the "if let box =" part to above the "arView.scene.anchors.append(boxAnchor)" part and that doesn't help.
I decided to go for the default code and work my way up to getting this to work with a touch to the box on the screen. Touching anywhere else should not rotate the box. Setting up a rotation animation inside Reality Composer is working, but I want a tap to rotate and a another tap to un-rotate. It will be opening a lid and closing a lid.
I am on Catalina and xCode12.
I have also noticed that the box is referred to as an object, entity, or element in the different pages of the documentation. This makes it difficult to we search for the issue I am having.

Replies

I have found out that .rotation is not working for me and I can use .orientation for setting the model in 3D space. I added the toy_biplane.usdz file downloaded from Apple into the Project Navigator. Then I started messing with the code. And reading more and messing more. This is where I am for now. I commented out things as I went along, just in case...
Code Block
import UIKit
import RealityKit
class ViewController: UIViewController {
    
    @IBOutlet var arView: ARView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Load the "Box" scene from the "Experience" Reality File
        // let boxAnchor = try! Experience.loadBox()
        let planeAnchor = AnchorEntity(plane: .horizontal)
        // Add the box anchor to the scene
        arView.scene.anchors.append(planeAnchor)
        
//        if let box = boxAnchor.findEntity(named: "lid") {
//            let radians = -45.0 * Float.pi / 180.0
//            box.transform.rotation += simd_quatf(angle: radians * 0, axis: SIMD3<Float>(1,0,0))
//        }
        let biplane = try? Entity.load(named: "toy_biplane.usdz")
        planeAnchor.addChild(biplane!)
        // var flyingBiplane = biplane?.transform
        let radians = 90.0 * Float.pi / 180.0
        biplane?.transform.translation += SIMD3<Float>(0.0, 1.0, 0.0)  // This is Meters!!!  not CM.
//        biplane?.transform.rotation += simd_quatf(angle: radians, axis: SIMD3<Float>(0,1,0))
        biplane?.orientation = simd_quatf(angle: radians, axis: SIMD3(x: 0, y: 1, z: 0))
        biplane?.transform.scale *= 0.5
    }
}


I am starting to get the result I am looking for.
Post not yet marked as solved Up vote reply of fid Down vote reply of fid

Hey @fid Were you able to fix the rotation issue? I've tried with all rotation/orientation/etc/etc I got zip luck. ANy hints how to fix this darn thing? https://developer.apple.com/forums/thread/683112#683112021

Hi @fid, @Dariusz1989 was having a similar issue as you, I would take a look at their thread. simd_quatf works differently from something like simd_float3. If you want to add an additional rotation to a quaternion, you need to use *= instead of +=. So your rotation code should look something like box.transform.rotation *= simd_quatf(angle: radians, axis: SIMD3<Float>(1,0,0))

I would prefer to keep each axis angle as variables and calculate the rotation matrix each time as below:

let rotationQuaternionX = simd_quatf(angle: angleX, axis: SIMD3<Float>(1, 0, 0)) let rotationQuaternionY = simd_quatf(angle: angleY, axis: SIMD3<Float>(0, 1, 0)) let rotationQuaternionZ = simd_quatf(angle: angleZ, axis: SIMD3<Float>(0, 0, 1))

box.transform.rotation = rotationQuaternionX * rotationQuaternionY * rotationQuaternionZ