Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

trackedImage position and rotation are not correct #1186

Open
gerardobevacqua opened this issue Jul 19, 2024 · 0 comments
Open

trackedImage position and rotation are not correct #1186

gerardobevacqua opened this issue Jul 19, 2024 · 0 comments
Labels
how to Explains how to accomplish something

Comments

@gerardobevacqua
Copy link

I am using Unity version 2022.3.11f1 and ARFoundation version 5.1.5, and I have a problem. When an image is tracked, its position is always (0, 0, 0). This is the code I am using:

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.XR.ARFoundation;
using UnityEngine.XR.ARSubsystems;
using TMPro;
public class CalibrationApp : MonoBehaviour
{

[SerializeField] private ARTrackedImageManager aRTrackedImageManager;


[SerializeField] private List<Vector3> globalPositionList = new List<Vector3>();
[SerializeField] private List<Vector3> localPositionList = new List<Vector3>();
[SerializeField] private List<Quaternion> globalRotationList = new List<Quaternion>();
[SerializeField] private List<Quaternion> localRotationList = new List<Quaternion>();
[SerializeField] private Vector3 firstMarkerPosition;
[SerializeField] private Quaternion firstMarkerRotation;
[SerializeField] private bool isFirstMarkerTracked = false;
[SerializeField] private TextMeshProUGUI debugLog;
[SerializeField] private TextMeshProUGUI pathName;
[SerializeField] private TextMeshProUGUI jobState;

MarkerCollection myCollection;
// Start is called before the first frame update
void Start()
{
    myCollection = new MarkerCollection();
}

// Update is called once per frame
void Update()
{
    
}

private void Awake()
{
    aRTrackedImageManager = GetComponent<ARTrackedImageManager>();
}

private void OnEnable()
{
    aRTrackedImageManager.trackedImagesChanged += ARTrackedImageManager_trackedImagesChanged;
   
}

private void OnDisable()
{
    aRTrackedImageManager.trackedImagesChanged-=ARTrackedImageManager_trackedImagesChanged;
}

private void ARTrackedImageManager_trackedImagesChanged(ARTrackedImagesChangedEventArgs obj)
{
    foreach(ARTrackedImage trackedImage in obj.added)
    {

        if(trackedImage.trackingState == TrackingState.None)
        {
            StartCoroutine(WaitForTrackingState(trackedImage)); 
        }

        jobState.text = trackedImage.referenceImage.name;
        if(!isFirstMarkerTracked)
        {
            firstMarkerPosition=trackedImage.transform.position;
            firstMarkerRotation=trackedImage.transform.rotation;
            isFirstMarkerTracked=true;
        }
        Debug.Log(trackedImage.transform.position.x + "\t" + trackedImage.transform.rotation.x + "\t" + trackedImage.trackingState);

        /*
        globalPositionList.Add(trackedImage.transform.position);
        globalRotationList.Add(trackedImage.transform.rotation);
        localPositionList.Add(trackedImage.transform.InverseTransformPoint(firstMarkerPosition));
        localRotationList.Add(Quaternion.Inverse(firstMarkerRotation) * trackedImage.transform.rotation);
        */
        

        Marker marker = new Marker();

        marker.name = trackedImage.referenceImage.name;
        marker.globalPosition = trackedImage.transform.position;
        marker.globalRotation = trackedImage.transform.rotation;
        marker.localPosition = trackedImage.transform.InverseTransformPoint(firstMarkerPosition);
        marker.localRotation = Quaternion.Inverse(firstMarkerRotation) * trackedImage.transform.rotation;

        myCollection.markers.Add(marker);


    }

    foreach (ARTrackedImage trackedImage in obj.updated)
    {

    }

    foreach (ARTrackedImage trackedImage in obj.removed)
    {

    }
}

IEnumerator WaitForTrackingState(ARTrackedImage trackedImage)
{
    // Aspetta fin quando il valore di trackedImage.trackingState non diventa TrackingState.Tracking
    while (trackedImage.trackingState != TrackingState.Tracking)
    {
        Debug.Log("Waiting for tracking state...");
        yield return null; // Aspetta un frame prima di controllare di nuovo
    }

    // Una volta che lo stato di tracciamento è TrackingState.Tracking, esegui il codice desiderato
    Debug.Log("Tracked Image is now being tracked!");
}


public void ExportData()
{
    //Path combine ha la stessa funzione del "+", ma con il vantaggio di non dover mettere gli "slash"
    string fileName = Path.Combine(Application.persistentDataPath, "calibrationData.json");

    //serializzo la mia lista in formato Json
    string jsonString = JsonUtility.ToJson(myCollection);
    debugLog.text = jsonString;
    pathName.text = fileName;
    //Creo un file di testo nel percorso fileName con jsonString come contenuto
    File.WriteAllText(fileName, jsonString);

    Debug.Log(fileName);
}

}

[Serializable]
public class Marker
{
public string name;
public Vector3 globalPosition;
public Vector3 localPosition;
public Quaternion globalRotation;
public Quaternion localRotation;

}

[Serializable]
public class MarkerCollection
{
public List markers;

public MarkerCollection()
{
    this.markers = new List<Marker>();
}

}
Could someone help me? In my application, I need to read the position of the markers in space so that I can print them later.
We also tried with a sample project, and the prefabs are still always instantiated at (0, 0, 0) and not at the marker's position.

@gerardobevacqua gerardobevacqua added the how to Explains how to accomplish something label Jul 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
how to Explains how to accomplish something
Projects
None yet
Development

No branches or pull requests

1 participant