Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update the pointOfView in ARKit

Tags:

swift

arkit

I'm trying to build my first ARKit app. The purpose of the app is to shoot little blocks in the direction that the camera is facing. Right now, here is the code I have.

 sceneView.scene.physicsWorld.gravity = SCNVector3(x: 0, y: 0, z: -9.8)


 @IBAction func tapScreen() {
    if let camera = self.sceneView.pointOfView {
        let sphere = NodeGenerator.generateCubeInFrontOf(node: camera, physics: true)
        self.sceneView.scene.rootNode.addChildNode(sphere)
        var isSphereAdded = true
        print("Added box to scene")

    }


}

The gravity works fine, whenever I tap on the screen the block shoots out each and every time I tap. However, they all shoot to the same point, no matter which direction the camera is facing. I'm trying to understand how pointOfView works, would I need to re-render the whole scene? Something else that I can't quite think of? Thanks for any help!

like image 753
Alec Norrie Avatar asked Nov 19 '25 12:11

Alec Norrie


1 Answers

Change this line from

self.sceneView.scene.rootNode.addChildNode(sphere)

to

self.sceneView.pointOfView?.addChildNode(sphere)
like image 57
Ru Chern Chong Avatar answered Nov 21 '25 01:11

Ru Chern Chong



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!