unity-game-enginevuforiamodel-view

How to record tracked position with Vuforia image target in Unity


I want to record tracked model view matrix per frame.
When tracking successfully, Vuforia would set camera and image target's gameobject position.
So I want to merge model view matrix of camera and gameobject.

Matrix4x4 cameraToWorld = perspCam.cameraToWorldMatrix;
Matrix4x4 worldToLocal = imgTarget.transform.worldToLocalMatrix;
Matrix4x4 cameraToLocal = cameraToWorld * worldToLocal;

Then, I set gameobject position, rotation and scale by cameraToLocal.
However, the image target's gameobject can't overlap on the correct place.
Where did I go wrong in my calculation?
OR is there better method to get one model view matrix from camera to gameobject?
Vuforia version : 9.8
World Center Mode : Deivece

Thanks in advance for any suggestions.


Solution

  • Without knowing too much of matrices you could achieve manually what you want using e.g.

    var relativePosition = perspCam.transform.InverseTransformPoint(imageTarget.transform.position);
    var relativeRotation = Quaternion.Inverse(perspCam.transform.rotation) * imageTarget.transform.rotation;
    

    So that later you would get the same results relative to the camera again using

    someObject.transform.position = perspCam.transform.TransformPoint(relativePosition);
    someone to.transform.rotation = perspCam.transform.rotation * relativeRotation;