i'm using arkit and realitykit,first load a room ,then put the picture on the wall ,but how to let it stick on the wall? how to get the right rotation of the picture? There are four possibilities,wall's front: z+, z-, x+, x-,
1 Answers
SceneKit
Answering your first question, to make the node stick to a particular place you need to just keep a desired location and add the node, it should not disappear unless you remove it. It may move up and down a little when you change phone orientation and position, but not much
node.position = SCNVector3(x, y, z)
If you are using touches to define the location, I would recommend you considering sceneView.hitTest()
and touchesBegan() functions
As for rotation, you simply can use Euler angles on a node of your interest along any axis, and in any needed direction
node.eulerAngles.x = -.pi / 2
Also, I would highly recommend you “App Development with Swift” by Apple Education, 2019. In this book there is a whole chapter on ARKit, besides answering your questions, it has numerous useful techniques and ideas
Here you can find the implementation of the end-of-the-chapter guided project from the App Development with Swift book, but doing it yourself would be much more useful
RealityKit
As given here, you can use one of two options for rotation
let boxAnchor = try! Experience.loadBox()
boxAnchor.steelBox?.orientation = simd_quatf(angle: .pi/4, axis: [0, 0, 1])
For angle property you insert how much you want to rotate the object in radians, and for axis property you select the axis that you want to rotate around.
Another option is to use transform
boxAnchor.steelBox?.transform = Transform(pitch: 0, yaw: 0, roll: .pi/4)
Roll, pitch, and yaw represent rotation along a particular axis, more is written here.
To change the position you can again use transform's translation
steelBox.transform.translation = [0, 0, -0.5]
This will translate your object according to the given parameters. This function heavily relies on affine transforms.
Regarding transformations w.r.t. other objects
Nodes in SceneKit as well as Entities in RealityKit are transformed and rotated with respect to the parent Node or Entity.
So, in your case, you have a big model where you plan to put smaller objects.
You have two options, either use touches detection on the big model (house) and them manually calculate where the object should be placed, which may be pretty cumbersome.
Another option is to add small transparent planes at predefined positions and then adding touches detection on them. This way you can omit calculation of where the smaller object should be placed
Good luck!

- 1,765
- 3
- 14
- 16
-
@zhoujunhua I have added info for reality kit as well – Bolat Tleubayev Apr 28 '20 at 06:38
-
1thank you ,you answer is about "how to rotate object" . then how to automatic stick painting on the wall , make sure the direction of the painting is right. is there something about "make rotate from normal " in realitykit – zhou junhua Apr 28 '20 at 09:20
-
@zhoujunhua you are more than welcome) could you elaborate more on what exactly do you mean by "make rotate from normal"? Because rotating your object along those three axes can give you any desirable position – Bolat Tleubayev Apr 28 '20 at 17:27
-
I'm sorry I didn't explain it in enough detail,Something like this ,https://godotengine.org/qa/44439/how-to-rotate-3d-object-to-align-with-terrain;https://forum.derivative.ca/t/rotate-geometry-by-normal-vector/5856; – zhou junhua Apr 29 '20 at 01:35
-
This problem is consistent with my description: https://forum.unity.com/threads/ar-foundation-align-ar-object-pararell-to-floor.699614/ – zhou junhua Apr 29 '20 at 06:20