How to get a screen to word position in arkit

Hello,

I tried to implement a game in arkit, when user tap the screen, the bullet will be fired from the touched position.

I know how to get 2D position on the screen as below

void HandleTapGesture(UITapGestureRecognizer sender)
        {
            SCNView areaPanned = sender.View as SCNView;
            CGPoint point = sender.LocationInView(areaPanned);

but I don't know how to transfer this position into the world position. Then I can use it in below scenario

let bullet = SCNNode(geometry: SCNSphere(radius: 0.08))
bullet.position = ?

That position type is

open var position: SCNVector3

Can anyone share any opinion? Many thanks.

  • sorry paste the wrong code to get 2D position

    it should be below

    let scnView = sender.view as! ARSCNView let holdLocation = sender.location(in: scnView)`

Add a Comment

Replies

Your question here could be interpreted a couple of different ways, but I'm going to guess that what you'd like to do is raycast from a 2D screen point and see if you hit anything in the real world?

If so, you may be interested in this sample code project: https://developer.apple.com/documentation/arkit/content_anchors/visualizing_and_interacting_with_a_reconstructed_scene

It uses ARView's raycast method to find 3D points in the scene reconstruction mesh from a tap location.