Unity-Technologies / arfoundation-samples

Example content for Unity projects based on AR Foundation
Other
3.03k stars 1.13k forks source link

Keep a GameObject stable in a specific position at all times with no drift #646

Closed lstimac closed 3 years ago

lstimac commented 3 years ago

Hi, I'm currently struggling to keep an instantiated object stable in a specific position. I'm using a marker (marker size is set to real size and it works great) to instantiate an object, however when I turn away from the marker, gain a distance of only a few steps, and then return to the instantiated object I notice it has changed its position a little bit (it is now positioned to the side, or above the marker and it may or may not regain its initial position). I'm wondering if there is any way to keep the object stable in it's initial position at all times?

Things I have tried so far:

One other thing I have noticed is object drifting a bit if I get too close to the marker with my device (to check the ground texture for example). This happens even when it's not continuously image tracking but only instantiating once, which means it shouldn't be connected to image tracking.

The reason I need to keep the object stable on marker position is because I use that position as a reference to set a target to which objects should fly and ultimately land (landing zone is marked in the real space which is why position offset breaks everything). I'm using iPad Pro with A12X processor, so it should be able to keep things as stable as it gets. I hope there is something I missed or done wrong and would appreciate any suggestions. :)

tdmowrer commented 3 years ago

I'm using a marker

"marker" is not a term we use in ARFoundation. Could you explain what you mean by this?

lstimac commented 3 years ago

I'm using a marker

"marker" is not a term we use in ARFoundation. Could you explain what you mean by this?

Sorry, I meant a reference image (tracked image) - a specific image used to track a world position and instantiate an object there.

tdmowrer commented 3 years ago

Adding an anchor to the marker position and setting it as a parent to the object (I notice no real difference with or without anchor, maybe I missed something)

Anchors are generally intended to solve this problem. Just to clarify: you are adding the anchor to your instantiated object, not the ARTrackedImage, right?

World tracking is up to the underlying AR framework (ARKit in this case). Poor performance or frame hitches can negatively impact world tracking, which can lead to drift. A couple things to look try:

  1. Use the Unity profiler to look for long frames. Are you able to maintain a smooth 60 fps, or are there performance spikes, e.g,. from garbage collection?
  2. Review the console output in Xcode. If ARKit is struggling to maintain world tracking there are sometimes warning messages, e.g., "World tracking performance is being affected by resource constraints".
lstimac commented 3 years ago

Anchors are generally intended to solve this problem. Just to clarify: you are adding the anchor to your instantiated object, not the ARTrackedImage, right?

Correct, I use ARTrackedImage only to get the position, and then I add the anchor to that specific position and set it as a parent to the instantiated object.

I tried your suggestions and the results are:

  1. There are some longer frames, but this is mostly connected to Profiler (Waiting for Target FPS and Player Connection are mostly causing longer frames)

  2. There are no warnings in Xcode present in console output except for one at the start saying that Meshing is not available (I'm not using it and this model of iPad doesn't even support it if I'm not mistaken)

I've been testing object stability using "Anchors" scene in AR Foundation Samples to see if I've done something wrong in my project, but unfortunately same thing happens with changes to position (about 10cm to either side) after looking away from object, walking a couple of steps further and then returning to it.

The only difference I have noticed in "Anchors" example is that I can go extremely close to the object and almost touch the surface (desk) with iPad without any drifting (when doing the same on a more complex object in my project it starts to move/drift a bit and I don't understand why).

I really appreciate your time and suggestions :D

tdmowrer commented 3 years ago

I'm not sure there's much more you can do here. AR based off a color camera and accelerometer presents several challenges that make it difficult to be precise. Newer devices with LiDAR might improve the situation.

lstimac commented 3 years ago

I understand, I will try to make it as good as it can be with all its limitations. Thank you very much for your responsiveness and assistance :)