Diksha235
Diksha235

Reputation: 452

When to use worldTransform() and when to use transform() in ARKit

I have been learning ARKit there are two basics to transform a object with respect to its relative position. Want to know when to use transform() method and worldTransform() method , a clear difference with example will be useful.

let transform = result.worldTransform
let isOnPlane = result.anchor is ARPlaneAnchor
object.setTransform(transform, relativeTo: cameraTransform,
                           smoothMovement: !isOnPlane,
                                alignment: planeAlignment,
                           allowAnimation: allowAnimation)

Upvotes: 2

Views: 2773

Answers (1)

Andy Jazz
Andy Jazz

Reputation: 58563

1

For ARAnchor and ARCamera use a local transform instance property.

transform is a matrix encoding the position, orientation, and scale of the anchor relative to the world coordinate space of the AR session the anchor is placed in.

For example, you can easily get ARAnchor's or ARCamera's transform represented by 4x4 matrix.

var transform: simd_float4x4 { get }

You should use this transform property this way (for locally positioned and oriented objects):

var translation = matrix_identity_float4x4
translation.columns.3.z = -0.25
let transform = currentFrame.camera.transform * translation 

// OR

let transform = currentFrame.camera.transform
let anchor = ARAnchor(transform: transform)                        
sceneView.session.add(anchor: anchor)

2

For hitTestResults and ARAnchors use a global worldTransform instance property.

worldTransform is the position and orientation of the hit test result relative to the world coordinate system.

var worldTransform: simd_float4x4 { get }

And you can use it this way:

if !hitTestResult.isEmpty {

    guard let hitResult = hitTestResult.first 
    else { return }

    addPlane(hitTestResult: hitResult)
    print(hitResult.worldTransform.columns.3)
}

// OR

let anchor = ARAnchor(name: "PANTHER", transform: hitTestResult.worldTransform)
sceneView.session.add(anchor: anchor)

Upvotes: 2

Related Questions