1

I want to put an anchor with mesh on the floor. Sometimes it does not pricesly set on the ground. Few inches of space is there between object and ground. How to change the Y of the object so it will be grounded on the floor.

I am thinking of detecting a horizontal plane below the object. But how to convert the position of an object? I do not want to put the object on the plane (It may move if plane moves)

enter image description here

Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
indrajit
  • 303
  • 1
  • 14

1 Answers1

2

Whether you're using RealityKit or ARKit, you definitely need to use plane detection feature. If your app detects a plane in RealityKit, it will automatically track a plane target. And I should say that a detected plane doesn't move (you're using World Tracking, aren't you), but it may be extended.

AnchorEntity(.plane([.any], classification: [.any], minimumBounds: [0.5, 0.5]))

In case you're using a plane detection feature in ARKit/SceneKit, you must additionally implement session(_:didAdd:) or renderer(_:didAdd:for:) delegate method (because ARKit can't do a job automatically):

let config = ARWorldTrackingConfiguration()
config.planeDetection = [.horizontal, .vertical]
sceneView.session.run(config)

func session(_ session: ARSession, didAdd anchors: [ARAnchor]) {
    guard let planeAnchor = anchors.first as? ARPlaneAnchor else { return }
}

For manual object placement use raycasting.

Also, you have to correctly position model's pivot point in 3D authoring app.

Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
  • 1
    thanks for the answers @andy fedoroff. I am still confused, How I can add the box on the floor ? User have decided the x and z but y should be of detected plane. – indrajit Jun 15 '21 at 07:54
  • Use raycasting methods. https://stackoverflow.com/questions/60294367/how-to-use-raycast-methods-in-realitykit/60304963#60304963, https://stackoverflow.com/questions/56866726/is-it-possible-to-place-objects-with-hit-test-without-plane-detection/56869569#56869569 – Andy Jazz Jun 15 '21 at 08:00
  • This may happen that the targeted location will not have a plane, In this case will raycast give me no result? What I am planning is : I will keep already detected floor plan, Now any object user adds i need to change the vertical position of the object in parrelel to plane that means I need to change Y only so the object will look just above the floor. – indrajit Jun 15 '21 at 08:04
  • You can visualize detected planes and turn them on/off. – Andy Jazz Jun 15 '21 at 08:05
  • However, if you're using a gadget with a LiDAR – there are no problems at all. – Andy Jazz Jun 15 '21 at 08:06
  • I have a small dot which is the center of the screen (Position is obtaining from raycast result by giving center CGPoint of the main view.). I am guiding the user to tap on the screen to put the object and we are assuming that users will tap while the camera is focusing on the floor. Sometimes it happens that the object is a little upside from the floor. Now I want to keep the object grounded. – indrajit Jun 15 '21 at 08:09
  • 1
    I am going to try raycast. I think it will help me, I will let you know the result :) – indrajit Jun 15 '21 at 08:14
  • Millions of people use raycast in AR every day. Believe me, it works fine)) – Andy Jazz Jun 15 '21 at 08:20
  • Yes, I am also using the raycast: Let me explain how am I doing it and what I want to achieve. I am putting the box on the floor, Position of the box is coming from the c++ classes. We are detecting the biggest corner of the current frame and the position of the box will get converted from corner 2D points. Sometimes it happens that c++ returns the wrong Y position which is a little upside from the floor. So when I convert x,y of c++ to 3D using ray cast, the converted position is a little upside. Now I was thinking that how I can ground it?. – indrajit Jun 15 '21 at 08:33
  • It came in my mind that a horizontal plane in an entire room can be useful, I will bring my object in parallel to the plane. That means removing vertical space only bw my object and plane, not position – indrajit Jun 15 '21 at 08:33
  • Move cube's pivot point to a desired position using C++. https://stackoverflow.com/questions/60401740/issue-with-adding-node-to-scene-view/60407242#60407242 – Andy Jazz Jun 15 '21 at 09:06
  • To change the pivot point I need to know the value or gap between the floor and up the lifted object. I was thinking to get vertical space between the plane and object And will move downside the object's Y position using matrix multiplication. What do you think? – indrajit Jun 15 '21 at 10:14
  • I can't 100% imagine how you can control your model using C++... – Andy Jazz Jun 15 '21 at 10:19
  • Could you get it by seeing the image? https://ibb.co/0Fn1tKQ – indrajit Jun 15 '21 at 10:26
  • I am passing the image to c++ from ARFrame. Now c++ class will return me 2d coordinates which need to be corrected in order to make the object grounded on the floor. I am not controlling the c++. Once c++ returned me the 2d x,y its job is finished. Most of the time there is space(bw visible floor and object) after converting the 2d to 3d using raycast. I want to remove that space. – indrajit Jun 15 '21 at 10:30
  • 1
    This the screenshot from the app: https://ibb.co/9bL6TLG – indrajit Jun 15 '21 at 10:35
  • I think I need to find the vertical gap between the h-plane and need to push the corner marker/box down (y position - gap). Here I will this code to find distanced position : – indrajit Jun 16 '21 at 03:59
  • func distance(y: Float) -> Position { let referenceNodeTransform = transform.matrix var translationMatrix = matrix_identity_float4x4 translationMatrix.columns.3.y = y let updatedTransform = matrix_multiply(referenceNodeTransform, translationMatrix) return .init(updatedTransform.columns.3.x, updatedTransform.columns.3.y, updatedTransform.columns.3.z) } – indrajit Jun 16 '21 at 04:00
  • but the question is how can I find the vertical gap? Would you like to help me with this? Any better idea? thank you in advance. – indrajit Jun 16 '21 at 04:01
  • Could you generate your model and send it over sharing service? Like https://dropmefiles.com – Andy Jazz Jun 16 '21 at 05:23
  • I am not generating the model. I am using Harris Corner Detection from OpenCV and this code written in c++. That means I pass image to Harris Corner and it returns the 2D cordinates. – indrajit Jun 16 '21 at 06:16
  • It seems to me I can't help you with this app)) Sorry – Andy Jazz Jun 16 '21 at 06:22
  • 1
    Np Andy, Thank you for your effort – indrajit Jun 16 '21 at 06:25