Writing a simple Waypoint System in Augmented Reality on iOS

Have an object navigate through a set of waypoints that follow the user in this simple implementation.

One of my most frequently requested tutorials has been to have augmented reality objects interact with users in the physical world. Many have asked me for a tutorial on having AR objects follow them around.

So in the spirit of those requests, what we’re going to do today is write a simple waypoint system. Here’s basically how it’ll work: upon firing up the app, we’ll begin tracking positions, and as we move around, we’ll add waypoints that any object we add to the scene will follow.

As usual, we’re going to save ourselves some time writing setup code and start with a previous project; namely, my second AR tutorial on SceneKit methods and some basic interactions with virtual objects. This will give us a reasonable spot to begin from, and we can take advantage of a lot of the code we wrote for putting those spheres in AR.

With that said, we’re going to remove a few things from the project that we don’t need.

We’ll start with our current Sphere class that loads our sphere model. We’ll clean it up since we don’t need all the previously written functionality.

Our new sphere is going to be a very simple object controlled by our waypoint system.

class Sphere: SceneObject {
    
    required init() {
        super.init()

        load(from: "sphere.dae")
        self.scale = SCNVector3(0.13, 0.13, 0.13)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

That’s it for the sphere. We load it from file and scale it down. Nothing else is necessary. The load method exists in the SceneObject base class, and that looks like the following.

    func load(from file: String) {
        let nodesInFile = SCNNode.allNodes(from: file)
        nodesInFile.forEach { (node) in
            self.addChildNode(node)
        }
    }

That used to be in the init, but I moved it to its own function to decouple the initialization of a node and its loading of a model.

Avatar photo

Fritz

Our team has been at the forefront of Artificial Intelligence and Machine Learning research for more than 15 years and we're using our collective intelligence to help others learn, understand and grow using these new technologies in ethical and sustainable ways.

Comments 0 Responses

Leave a Reply

Your email address will not be published. Required fields are marked *