Hello everyone.

We have decided to phase out the Mixed Reality Forums over the next few months in favor of other ways to connect with us.

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.

The plan between now and the beginning of May is to clean up old, unanswered questions that are no longer relevant. The forums will remain open and usable.

On May 1st we will be locking the forums to new posts and replies. They will remain available for another three months for the purposes of searching them, and then they will be closed altogether on August 1st.

So, where does that leave our awesome community to ask questions? Well, there are a few places we want to engage with you. 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. And always feel free to hit us up on Twitter @MxdRealityDev.

Creating an WorldAnchor always comes back with null!

I have had no comments on my question in Question and Answers or Microsoft Published Experiences on my question: Has anyone ever had success at creating WorldAnchor points?
I tried creating a WorldAnchor position on the HoloLens Emulator which did not work then I asked an American buddy to try it on the real thing and he could not get it to work. I am sure there must be a problem with this. Please look at this code which just sets up a WorldAnchor point on a sphere:-

     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;
        }

The result always give me some output like:-

I have never managed to get a WorldAnchor component which has been non-null! If this is not a bug, it needs some explanation as to why this happens.

Best Answer

Answers

  • AlexDAlexD ✭✭✭

    WorldAnchors are not compatible with RigidBody. Because an object that is locked with a world anchor needs to be fixed in space you cannot have a Rididbody attached to it.

    As a result, the way you need to work it out is to attach the rigidbody in script after you've saved/loaded the position from the anchor.

  • Alec_HAlec_H ✭✭
    edited July 2016

    @wheelchairman Your assignment of an anchor is setting it to null and is not creating a non null anchor.
    WorldAnchor anchor = gameObject.AddComponent<WorldAnchor>();
    That is how you should be using world anchor as referenced here.
    Basically, as it's written now you're setting it's value to null then never changing it from null. Hope this helps.

  • AlexDAlexD ✭✭✭
    edited July 2016

    @Alec_H said:
    @wheelchairman Your assignment of an anchor is setting it to null and is not creating a non null anchor.
    ...
    Basically, as it's written now you're setting it's value to null then never changing it from null. Hope this helps.

    What do you mean? He's assigning it in the next line

    WorldAnchor anchor = null;
    anchor = m.AddComponent();//ALWAYS RETURNS NULL !

    The AddComponent call returns null because the object has a Rigidbody attached to it

  • Alec_HAlec_H ✭✭

    @AlexD My bad you're right.

  • Thank you gentlemen for your comments. I appreciate it. AlexD suggested the idea of the rigidbody component upsetting the WorldAnchor component. I took your advice and commented out the RigidBody component addition to the GameObject. Here is my amended code:-

       static public GameObject create_sphere(Vector3 pos)
        {
            var m = GameObject.CreatePrimitive(PrimitiveType.Sphere);
    
            if (pos != Vector3.zero)
            {
                m.transform.position = pos;
                m.transform.localScale = Vector3.one * 0.04f;
            }
    
            WorldAnchor anchor = null;
            try
            {
                anchor = m.AddComponent<WorldAnchor>();//Something happens here as it does not return from this routine
                //it jumps to somewhere else!
                if (anchor != null)
                {
                    //save_anchor_point(anchor, gettagstring(pos));
                    addcomment.comment = "added world anchor";
                    if (anchor.isLocated)
                    {
                        //anchor is located
                        addcomment.comment = "world anchor is located";
                    }
                    else
                    {
                        addcomment.comment = "world anchor is NOT located!";
                    }
                }
                else
                {
                    addcomment.comment = "not added worldanchor";
                }
            }
            catch
            {
                addcomment.comment = "Exception caused for anchor = m.AddComponent<WorldAnchor>();";
                return null;
            }
    
            //var rigidbody = m.AddComponent<Rigidbody>();
            //rigidbody.velocity = Vector3.zero;
            //rigidbody.constraints = RigidbodyConstraints.FreezeAll;
            m.AddComponent<MeshFilter>();
            m.AddComponent<BoxCollider>();
            m.AddComponent<MeshRenderer>();
            if(m!=null) addcomment.comment = "Game Object valid";
            return m;
        }
    

    The idea of this code is to test where the routine fails. My addcomment widget ought to report to the HoloLens Emulator screen (this works on the HoloLens Hardware too) in the last label at the bottom of the screen. A typical screen I get is this:-

    Please note that all these comments are ignored and nothing is shown in the addcomment label box. It is set to its default state of showing "Comment phrase". Consequently, during the procedure:-

    anchor = m.AddComponent<WorldAnchor>();//Something happens here as it does not return from this routine

    Can anyone throw light on my error here or could it be something else perhaps?
    Grateful for useful suggestions.

Sign In or Register to comment.