2

When I add a new node with ARKit (ARSKView), the object is positioned base on the device camera. So if your phone is facing down or tilted, the object will be in that direction as well. How can I instead place the object base on the horizon?

enter image description here

Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
pierre23
  • 3,846
  • 1
  • 28
  • 28

1 Answers1

1

For that, right after a new node's creation, use a worldOrientation instance property that controls the node's orientation relative to the scene's world coordinate space.

var worldOrientation: SCNQuaternion { get set }

This quaternion isolates the rotational aspect of the node's worldTransform matrix, which in turn is the conversion of the node's transform from local space to the scene's world coordinate space. That is, it expresses the difference in axis and angle of rotation between the node and the scene's rootNode.

let worldOrientation = sceneView.scene.rootNode.worldOrientation

yourNode.rotation = worldOrientation    /*  X, Y, Z, W components  */

P.S. (as you updated your question) :

If you're using SpriteKit, 2D sprites you spawn in ARSKView are always face the camera. So, if the camera moves around a definite point of real scene, all the sprites must be rotated about their pivot point, still facing a camera.

Nothing can prevent you from using SceneKit and SpriteKit together.

Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
  • Hi ARGeo, thank you for your answer. I'm actually using ARSKView and not ARSCNView so I do not have the scene property. – pierre23 Jun 14 '19 at 15:18
  • I currently use SpriteKit because I want the objects to always face the camera and I want the objects to be in be 2D. I tried using SceneKit and rotate the node as I move the camera but it was not looking good. With my current code, all I need is to fix the initial position of the node. – pierre23 Jun 14 '19 at 16:00
  • SpriteKit is very limited in 3D settings. If you use SceneKit framework, try exploit `SCNBillboardConstraint` which automatically adjusts a node's orientation so that its local z-axis always points toward the `pointOfView` of a node it's applied for. https://stackoverflow.com/questions/49194597/adding-a-scnbillboardconstraint-makes-the-node-dissapear – Andy Jazz Jun 14 '19 at 17:44
  • @AndyJazz How do i get the orientation of a node relative to an another node? Could you please help me? – Ryan Aluvihare Apr 09 '23 at 14:45
  • 1
    @AndyJazz Done. https://stackoverflow.com/questions/75952160/how-to-get-orientation-of-an-arimageanchor-in-relation-to-a-given-scnnode-in-ark – Ryan Aluvihare Apr 10 '23 at 04:45
  • Ryan, I don't quite understand what exactly you want – for what purpose do you measure the orientation's deviation of the image anchor relative to the node? – Andy Jazz Apr 10 '23 at 06:49
  • 1
    @AndyJazz For now, It will be logged to a csv file to be used in a different program/task. Let's say I put a node in the scene. which we will call as 'originNode'. then scan the scene or images. When an image is detected, I want to know which orientation the image is in. (tilt, pan and roll) – Ryan Aluvihare Apr 10 '23 at 08:36
  • Yep, I already understood this. Why is it necessary to determine the deviation angle? What do you want to do based on this data? – Andy Jazz Apr 10 '23 at 08:42
  • 1
    Actually, this is a part of an ongoing project, so I'm also exactly not clear what it is going to be used for. I'm assuming it is to determine which orientation an object should be in the scene we scan with the iPhone, so the scene can be recreated in a different program/environment – Ryan Aluvihare Apr 10 '23 at 08:50
  • So the divine )) purpose is model's orientation, right? – Andy Jazz Apr 10 '23 at 08:52
  • 1
    Yes. That should be it. – Ryan Aluvihare Apr 10 '23 at 08:57