0

I am trying to implement a function that allows the user to tap and add a node to the to a scene at the location where the user clicked. I would like this to be on a plane. I did some research and found the following function, but I get the warning Value of type 'simd_float4x4' has no member 'translation' on the line let translation = hitTestResult.worldTransform.translation

Does anyone know how I can change this so I am not getting the warning?

@objc func addRoomToSceneView(withGestureRecognizer recognizer: UIGestureRecognizer) {
        let tapLocation = recognizer.location(in: sceneView)
        let hitTestResults = sceneView.hitTest(tapLocation, types: .existingPlaneUsingExtent)
        
        guard let hitTestResult = hitTestResults.first else { return }
        //THE FOLLOWING LINE HAS THE warning: Value of type 'simd_float4x4' has no member 'translation'
        let translation = hitTestResult.worldTransform.translation
        let x = translation.x
        let y = translation.y
        let z = translation.z
        
        let room = createMaskedRectangleRoom(width: 4, height: 4, depth: 4, color: .white)
        room.scale = SCNVector3(2, 2, 2)
        room.position = SCNVector3(x,y,z)
        sceneView.scene.rootNode.addChildNode(room)
    }

1 Answers1

0

The result of hitResult.worldTransform is an SCNMatrix4 and a 4x4 matrix has no translation property. When applied to a point using matrix multiplication the transformation done might include a translation, but the underlying data type is essentially a 16 element rectangular array.

You can probably extract the translation aspect of the matrix with:

let translation = SCNVector4(float4x4(hitTestResult.worldTransform) * SIMD4<Float>(0,0,0,1))

I'm assuming the hitTestResult.worldTransform matrix is affine. It would be weird if it were not.

Scott Thompson
  • 22,629
  • 4
  • 32
  • 34