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.

Am I creating a WorldAnchor position correctly?

I am exploring using Spatial Anchors known in Unity as WorldAnchors. I use the HoloToolKit to develop code using the HoloLens Emulator. Perhaps I am doing something fundamentally wrong but I want to store some WorldAnchor positions so that I could successfully load them in later using WorldAnchorStore. My relevant code snippet looks like this:-

    static public GameObject create_sphere()
    {
        var m = GameObject.CreatePrimitive(PrimitiveType.Sphere);

        var rigidbody = m.AddComponent<Rigidbody>();
        var x=m.AddComponent<WorldAnchor>();
        if(x as WorldAnchor)
        {
            addcomment.comment = "added world anchor";
        }
        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;
    }

What have I done wrong here?

Best Answer

Answers

  • DanglingNeuronDanglingNeuron ✭✭✭
    edited July 2016

    You are not saving the world anchor.

    You first need to get the World Anchor Store
    Then you create your anchor, if its located, you save it with an anchor id

    WorldAnchorStore anchorStore = null;
    WorldAnchor anchor = null;
    const string AnchorID = "MyFunkyAnchor";
    void Start()
    {
    WorldAnchorStore.GetAsync(AnchorStoreReady);
    }

        void AnchorStoreReady(WorldAnchorStore store)
        {
            anchorStore = store;
        }
    

    CreateandSaveAnchor()
    {
    anchor = GameObject.AddComponent();
    if(anchor.isLocated)
    anchorStore.Save(AnchorID, anchor);
    }

    Healthcare IT professional by day - Indie GameDev for UWP and mobile platforms by night

  • Sorry I should have added that my comment output box comes up with "not added worldanchor" every time!

  • Oh thank you for that very prompt reply DanglingNeuron. I have not tested what you said yet but I will do and report if it was successful for me.

  • I had already started up a WorldAnchorStore but could not see how I could put a WorldAnchor into this store without it being validated. Before you can test it with isLocated property, Just testing if it was a non-null object is a good thing to do. In your example DanglingNeuron you suggested:-

                        WorldAnchor anchor = null;
                        anchor = GameObject.AddComponent();
                        if(anchor.isLocated)
                        anchorStore.Save(AnchorID, anchor);
    

    On my machine the second line just does not compile up. Even if 'anchor' is declared as a WorldAnchor, don't we have to make clear the type of Component we are adding?

            var m = GameObject.CreatePrimitive(PrimitiveType.Sphere);
            var rigidbody = m.AddComponent<Rigidbody>();
            WorldAnchor anchor = null;
            anchor=m.AddComponent<WorldAnchor>();
            if(anchor as WorldAnchor)
            {
                addcomment.comment = "added world anchor";
                if(anchor.isLocated)
                {
                    //anchor is located
                    addcomment.comment = "world anchor is located";
                }
            }
            else
            {
                addcomment.comment = "not added worldanchor";
            }
    

    My comment box always reports 'Not added worldanchor".

    I apologize in advance if I am being ultra stupid here. Please be patient and try to explain my error.

  • DanglingNeuronDanglingNeuron ✭✭✭
    edited July 2016

    sorry that was a typo...
    anchor = yourGameObject.AddComponent< WorldAnchor >();

    but you should only check for null dont do a "cast as"

    if(anchor != null and anchor.isLocated)
    {
    we are good to go
    }

    Healthcare IT professional by day - Indie GameDev for UWP and mobile platforms by night

  • Thank you for your comment about not casting it. It tried it with the amended code

            var m = GameObject.CreatePrimitive(PrimitiveType.Sphere);
            var rigidbody = m.AddComponent<Rigidbody>();
            WorldAnchor anchor = null;
            anchor=m.AddComponent<WorldAnchor>();
            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";
            }
    

    but I still got the unwelcome info. I have never posted up a screen snippet before, but here's a try:-

    The units are in millimetres here. The white spheres are sample positions that are a result of going through this code body.

    Any further suggestions perhaps?

  • AlexDAlexD ✭✭✭

    @wheelchairman please check my answer I provided in one of the other sections to the same problem.
    You cannot set a WorldAnchor on an object that has a Rigidbody component.

  • Thanks AlexD I replied to you in the other section with a new comment thank you.

Sign In or Register to comment.