The Mixed Reality Forums here are no longer being used or maintained.
There are a few other places we would like to direct you to for support, both from Microsoft and from the community.
The first way we want to connect with you is our mixed reality developer program, which you can sign up for at https://aka.ms/IWantMR.
For technical questions, please use Stack Overflow, and tag your questions using either hololens or windows-mixed-reality.
If you want to join in discussions, please do so in the HoloDevelopers Slack, which you can join by going to https://aka.ms/holodevelopers, or in our Microsoft Tech Communities forums at https://techcommunity.microsoft.com/t5/mixed-reality/ct-p/MicrosoftMixedReality.
And always feel free to hit us up on Twitter @MxdRealityDev.
Has anyone ever had success at creating WorldAnchor points?
I have had no success at adding a WorldAnchor component to a GameObject that worked for the HoloLens Emulator. For instance this code snippet:-
static public GameObject create_sphere(Vector3 pos) { var m = GameObject.CreatePrimitive(PrimitiveType.Sphere); var rigidbody = m.AddComponent<Rigidbody>(); if (pos!=Vector3.zero)m.transform.position = pos; WorldAnchor anchor = null; anchor = m.AddComponent<WorldAnchor>();//ALWAYS RETURNS NULL ! if(anchor !=null) { addcomment.comment = "added world anchor"; if(anchor.isLocated) { //anchor is located addcomment.comment = "world anchor is located"; } } else { addcomment.comment = "not added worldanchor"; } rigidbody.velocity = Vector3.zero; rigidbody.constraints = RigidbodyConstraints.FreezeAll; m.AddComponent<MeshFilter>(); m.AddComponent<BoxCollider>(); m.AddComponent<MeshRenderer>(); return m; }
creates spheres on the triangulated background fake mesh like in the following clip:
As you can see the spheres are created but I failed in every instance to create a WorldAnchor associated with each one of them. I have also set up a WorldAnchorStore but of course it does not effect this problem.
I would like to hear from anyone who has had any success in creating WorldAnchors and if there was anything special you had to do.
Best Answer
-
I have now been able to create WorldAnchor (Spatial Anchor positions) in my project. Getting rid of the RigidBody component was one issue. Thank you for that comment (AlexD I think - sorry if I have that wrong). The code snippet looks like this
static public GameObject create_sphere(Vector3 pos) { string comment = ""; if (anchor_now) { addcomment.comment = "try freezing"; } var m = GameObject.CreatePrimitive(PrimitiveType.Sphere); if (pos != Vector3.zero) { m.transform.position = pos; m.transform.localScale = Vector3.one * 0.04f; } WorldAnchor anchor = null; if (posmarker.anchor_now) { try { anchor = m.AddComponent<WorldAnchor>();//Always returns a non-null value if (anchor != null) { //save_anchor_point(anchor, gettagstring(pos)); anchor.transform.position = pos; if (anchor.isLocated) { //anchor is located comment = "world anchor is located"; } else { comment = "world anchor is NOT located!";//Always returns this comment! } } else { comment = "not added worldanchor"; } } catch { comment = "Exception caused for anchor = m.AddComponent<WorldAnchor>();"; return null; } } m.AddComponent<MeshFilter>(); m.AddComponent<BoxCollider>(); m.AddComponent<MeshRenderer>(); if (m != null && !anchor_now) addcomment.comment = "Game Object valid"; else if (m != null && anchor_now) addcomment.comment = comment; return m; }
A sample showing Spheres with anchor points is show below.
I have not attempted to save this yet. However I do have a linked issue about locating a Spatial Anchor position which I think I have to asked in a separate question. Thank you to all for the helpful comments you made.
5
Answers
@wheelchairman Your assignment of an anchor is setting it to null and is not creating a non null anchor.
WorldAnchor anchor = gameObject.AddComponent();
That is how you should be using world anchor as referenced here.
@wheelchairman please don't open the same question in 3 different places. I've answered the one in Discussion but this is causing unnecessary clutter on the forums.
Maybe a mod (@Patrick, @ahillier @neerajwadhwa ) can close and consolidate the threads. Thanks.
Toronto-HoloLens | Blog | @alexdrenea
I'll try not to clutter up the forums, I just wanted an answer and it still is elusive. I shall keep to the Discussion Forum for the topic from now on - Sorry
did you at least see my proper answer in one of your topics ? I think that would help you
Toronto-HoloLens | Blog | @alexdrenea
Sorry AlexD I answered it in the Discussion forum. Even commenting out all the RigidBody component references did not give me a resolution to this problem. I still need to be convinced the WorldAnchor component addtion works properly.
This program line does not return and it looks like perhaps there might be a stack or a thread problem with it. Have you an example you could show me where adding this component works? I would be delighted to try it. Thanks for bearing with me I do appreciate it.
I have now been able to create WorldAnchor (Spatial Anchor positions) in my project. Getting rid of the RigidBody component was one issue. Thank you for that comment (AlexD I think - sorry if I have that wrong). The code snippet looks like this
A sample showing Spheres with anchor points is show below.
I have not attempted to save this yet. However I do have a linked issue about locating a Spatial Anchor position which I think I have to asked in a separate question. Thank you to all for the helpful comments you made.