Home > Net >  Create AnchorEntity with a given target and minimumBounds
Create AnchorEntity with a given target and minimumBounds

Time:05-19

let anchor = AnchorEntity(plane: .horizontal, minimumBounds: [0.2, 0.2])

This lane spits errors:

  • "Argument passed to call that takes no arguments"
  • "Cannot infer contextual base in reference to member 'horizontal'"

However on Apple website I see the option is correct:

init(plane: AnchoringComponent.Target.Alignment, classification: AnchoringComponent.Target.Classification, minimumBounds: SIMD2<Float>)

Why is that? It's a lane from a project I was trying to recreate, but it's about 2years old and abandoned. Has the syntax changed?

Below I paste a bigger chunk of code, maybe it will be useful:

class ViewController: UIViewController {
    
    @IBOutlet var arView: ARView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
// ========= THIS LANE CAUSES ERRORS =========
        let anchor = AnchorEntity(plane: .horizontal, minimumBounds: [0.2, 0.2])
// ========= THIS LANE CAUSES ERRORS =========

        arView.scene.addAnchor(anchor)
        
        var cards: [Entity] = []
        for _ in 1...16 {
            let box = MeshResource.generateBox(width: 0.04, height: 0.002, depth: 0.04)
            let metalMaterial = SimpleMaterial(color: .gray, isMetallic: true)
            let model = ModelEntity(mesh: box, materials: [metalMaterial])
            
            model.generateCollisionShapes(recursive: true)
            
            cards.append(model)
            
        }

CodePudding user response:

There's no error you've mentioned here. Everything works fine. Make sure you have the appropriate lighting conditions in the room and a suitable surface for tracking. If this error persists, move your code to a new Xcode 13.4 project.

Try my code:

import RealityKit
import UIKit

class ViewController: UIViewController {
    
    @IBOutlet var arView: ARView!
    var tiles: [Entity] = []
    var anchor = AnchorEntity()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.anchor = AnchorEntity(plane: .horizontal, 
                           minimumBounds: [0.2, 0.2])
        arView.scene.addAnchor(anchor)
        
        for i in 1...16 {                
            let box = MeshResource.generateBox(width: 0.04,
                                              height: 0.002,
                                               depth: 0.04)                
            let model = ModelEntity(mesh: box)
            model.generateCollisionShapes(recursive: true)
            tiles.append(model)                
            self.anchor.addChild(tiles[i-1])
        }
        
        for (i, tile) in tiles.enumerated() {                
            let x = Float(i % 4) - 0.75
            let z = Float(i / 4) - 0.75             
            tile.position = [x * 0.05, 0, z * 0.05]
        }
    }
}

CodePudding user response:

Make sure you're targeting a physical or generic iOS device (ie. not the simulator or macOS).

The same constructors for a physical device don't exist in RealityKit for Simulator and macOS.

  • Related