3

Swift beginner struggling with moving a scene node in ARkit in response to the device motion.

What I want to achieve is: First detect the floor plane, then place a sphere on the floor. From that point onwards depending on the movement of the device, I want to move the sphere along its x and z axis to move it around the floor of the room. (The sphere once created needs to be in the center of the device screen and locked to that view)

So far I can detect the floor and place a node no problem. I can use device motion to obtain the device attitude (pitch, roll and yaw) but how to translate these values into meaningful x, y, z positions that I can update my node with?

Are there any formulas or methods that are used to calculate such information or is this the wrong approach? I would appreciate a link to some info or an explanation of how to go about this. Also I am unsure how to ensure the node would be always at the center of the device screen.

Andrew Bibby
  • 338
  • 5
  • 11
  • I think I understood your problem, but I'm confused about one part. If you want to keep the sphere locked in the center of the screen, why do you want to detect a plane? Is it to determine the size of the sphere? – Marcel Alves Mar 27 '20 at 14:13

1 Answers1

0

so, as far as I understood you want to have a following workflow:

Step 1. You create a sphere on a plane (which is already done)

Step 2. Move the sphere with respect to the camera's horizontal plane (i.e. along its x and z axis to move it around the floor of the room depending on the movement of the device)

Assuming that the Step 1 is done, what you can do:

  1. Get the position of the camera and the sphere

    This should be first called within the function that is invoked after sphere creation (be it a tapGestureRecognizer(), touchesBegan(), etc.).

    You can do it by calling position property of SCNNode for sphere and for camera position and/or orientation by calling sceneView.session.currentFrame's .camera.transform which contains all necessary parameters about current position of the camera

  2. Move the sphere as camera moves

    Having the sphere position on the Scene and the transformation matrix of the camera, you can find the distance relation between them. Here you can find a good explanation of how exactly you can do it

    After you get those things you should implement a proper logic within renderer(_:didUpdate:for:) to obtain continuous lock of the ball with respect to the camera position

If you are interested about the math behind it, you can kick off by reading more about transformation matrices which is a big part of Image Processing and many other areas

Hope that this will help!

Bolat Tleubayev
  • 1,765
  • 3
  • 14
  • 16