Search code examples
xcodeaugmented-realityarkit

How to get the screen central position in arkit?


I set a beat sight in the middle of the screen, and when I tap the screen and fire a bullet, on the ipad the beat sight and bullet are in the same position as expected, but on iphone (11) the bullet position is above the beat sight.

on ipad enter image description here on iphone

enter image description here

The beat sight config is below

enter image description here

And the bullet position is below

   guard let pointOfView = sceneView.pointOfView else {return}
        let transform = pointOfView.transform
        let orientation = SCNVector3(-transform.m31, -transform.m32, -transform.m33)
        let location = SCNVector3(transform.m41, transform.m42, transform.m43)
        let position = orientation + location

Does anyone know how to solve it? Thanks a lot.


Solution

  • By looking at your constraints, the cross that you have set is not based on the Safe Area but instead on the whole view.

    The bullet that you have is currently at the center of the scene, but your cross is not. You can change the property of the second item to for both Center X and Center Y for your cross.

    Changing Constraint

    Of course, you can do otherwise by changing your sceneView constraint based on the whole View instead of Safe Area.

    enter image description here