Home > Back-end >  RealityKit – AnchorEntity moves only to Origin
RealityKit – AnchorEntity moves only to Origin

Time:02-13

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)
}

CodePudding user response:

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.

  • Related