How I can save/get a rotation from Hololens anchor?

I'm using anchor in my application to be the "center of the world" instead of hololens be the "center of the world". I instantiate a cube and turn this my reference to display my 3d objects.

I can get the position normally, but the rotation not. Always when I change of scene and open it again, the position reference of cube is ok. My 3d objects get the position reference and is be displayed ok, but the rotation is come from hololens. If I rotate my head (hololens), the 3d object get this "rotation" and is changed, but I want get the rotation of my cube anchor.

This is my code:

//Instantiate the cube
AnchorController anchorController = anchorManager.GetComponent<AnchorController>();    
referenceGameObject = GameObject.Find("Cube");    
referenceGameObject = anchorController.instance;

//Getting the position and rotation of cube
Vector3 referencePosition = referenceGameObject.transform.position;
Quaternion referenceRotation = referenceGameObject.transform.rotation;


//Instantiate a 3d object and inside her I instantiate my 3d objects     
GameObject parent = new GameObject("Obj3D");

Quaternion originalRot = parent.transform.rotation;

//step.anchorX/Y/Z is a coordinates that came from json file, are running perfectly.
//Here I get the position reference of my cube, works perfectly.
parent = Instantiate(parent, referencePosition, referenceRotation);
parent.transform.position = referencePosition;
parent.transform.position = parent.transform.position + (referenceGameObject.transform.right * step.anchorX);
parent.transform.position = parent.transform.position + (referenceGameObject.transform.forward * step.anchorY);
parent.transform.position = parent.transform.position + (referenceGameObject.transform.up * step.anchorZ);

//Here I'm getting the reference rotation of my cube and insert a rotation of my cube  
parent.transform.Rotate(referenceRotation.eulerAngles.x, referenceRotation.eulerAngles.y, referenceRotation.eulerAngles.z); 


//Here is my 3d objects 
childObject = Instantiate(SetPrefab(step), parent.transform);

childObject.transform.localPosition = Vector3.zero;
object3d = parent;

if (step.HLScaleX != 0f && step.HLScaleY != 0f && step.HLScaleZ != 0f){
    object3d.transform.localScale = new Vector3(step.HLScaleX, step.HLScaleY, step.HLScaleZ);
}
object3d.transform.localRotation = Quaternion.Euler(new Vector3(step.HLRotationX, step.HLRotationY, step.HLRotationZ));

In resume,

How I can save/get the rotation of my anchor in Hololens?

Answers

  • dbarrettdbarrett ✭✭✭
    edited January 2018

    Not sure if this will help but, if you're just trying to instantiate everything in reference to an anchor you can do something like:

    object.transform.position = anchor.transform.InverseTransformPoint(desiredWorldPoint);

    what this says is put my object at the desired world position relative to this anchor's local position. There is also an InverseTransformDirection method as well for dealing with direction vectors. Also there is a TransformPoint/Direction as well from converting to world space from local space.

    https://docs.unity3d.com/ScriptReference/Transform.InverseTransformPoint.html

    This is how most multiplayer HoloLens apps do it since they use a shared world coordinate and base everything off of that to get it relative to their position.

    AR Developer

  • I'm not sure if this will help either but what I've tended to do is to make the object that I apply a world anchor to be an empty game object and then use that empty object as the parent of my actual holograms (within a suitable distance as per the guidance in the documentation).

    If I do that then I can save/load the anchor using (e.g.) the WorldAnchorManager and its Load/Save functionality or I can it over the network using the WorldAnchorTransferBatch and its Export/Import functionality.

    The job of saving/recreating holograms then becomes the job of saving/restoring the local transform to the parent (anchor) once the anchor is 'live' in the app.

Sign In or Register to comment.