Pedro J
Pedro J

Reputation: 311

RealityKit – AnchorEntity moves only to Origin

The problem is that it rise to origin... not just rotating itself.

video is youtu.be/hFafOjKN3rg

anchorEntity.anchoring = AnchoringComponent(anchor)
anchorEntity.addChild(modelEntityClone)
anchorEntity.addChild(downEntity)
anchorEntity.addChild(sideEntity)
anchorEntity.addChild(rightEntity)
anchorEntity.name = name
        
arView.scene.addAnchor(anchorEntity)
arView.scene.addAnchor(planeEntity)

DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
    print("after 3 sec")
            
    let radians = 90.0 * Float.pi / 180.0
            
    let currentMatrix = anchorEntity.transform.matrix
    let rotation = simd_float4x4(SCNMatrix4MakeRotation(90.0 * .pi/180, 0,1,0))
    let transform = simd_mul(currentMatrix, rotation)                
    anchorEntity.move(to: transform, relativeTo: nil, duration: 3.0)
}

Upvotes: 1

Views: 578

Answers (1)

Andy Jazz
Andy Jazz

Reputation: 58563

Apply anchor's rotation relative to model entity:

let model = ModelEntity(mesh: .generateBox(size: 0.2))
let anchor = AnchorEntity(world: [0, 0.5, 0])
anchor.addChild(model)

let currentMatrix = anchor.transform.matrix
let rotation = simd_float4x4(SCNMatrix4MakeRotation(.pi/2, 0, 1, 0))
let transform = simd_mul(currentMatrix, rotation)
anchor.move(to: transform, relativeTo: model, duration: 3.0)

This post is also useful.

Upvotes: 1

Related Questions