Log in or register to post comments

Vuforia Two Markers Triggering a Action

March 12, 2014 - 5:40pm #1
Hi everybody.
I am working on an augmented reality app with the Vuforia Plugin and want to achieve the following.
 
I have two markers, both have a model attached to them.
 
Model 1: bert
 
Model 2: henk
 
When both "bert" and "henk" are being tracked I want to show a console message for now called "bert AND henk".
Weird part is, when one of them is tracked it changes the bool state of the other to false.
I hope you can help me as I am at a loss. I am fairly new to C# so it may be an obvious error.
Thanks in advance!
using UnityEngine;

public class ImageTargetTrackableEventHandler : MonoBehaviour,
ITrackableEventHandler
{
	#region PUBLIC_MEMBER_VARIABLES
	public bool isBeingTracked;
	#endregion PUBLIC_MEMBER_VARIABLES
	
	#region PRIVATE_MEMBER_VARIABLES
	private TrackableBehaviour mTrackableBehaviour;
	private bool hashenk;
	private bool hasbert;
	#endregion // PRIVATE_MEMBER_VARIABLES
	
	
	#region PUBLIC_METODS
	void Start()
	{
		mTrackableBehaviour = GetComponent<TrackableBehaviour>();
		if (mTrackableBehaviour)
		{
			mTrackableBehaviour.RegisterTrackableEventHandler(this);
		};
	}

	/// <summary>
	/// Implementation of the ITrackableEventHandler function called when the
	/// tracking state changes.
	/// </summary>
	public void OnTrackableStateChanged(
		TrackableBehaviour.Status previousStatus,
		TrackableBehaviour.Status newStatus)
	{
		if (newStatus == TrackableBehaviour.Status.DETECTED ||
		    newStatus == TrackableBehaviour.Status.TRACKED)
		{
			OnTrackingFound();
			Debug.Log ("Show me the State--------bert is:"+hasbert+" and henk is:"+hashenk);
		}
		else
		{
			OnTrackingLost();
			Debug.Log ("Show me the State-------bert is:"+hasbert+" and henk is:"+hashenk);
		}
	}
	
	#endregion // PUBLIC_METHODS
	
	
	
	#region PRIVATE_METHODS
	private void OnTrackingFound()
	{
		isBeingTracked = true;
		Renderer[] rendererComponents = GetComponentsInChildren<Renderer>(true);
		Collider[] colliderComponents = GetComponentsInChildren<Collider>(true);
		//Watch the console, the foreach below runs without the Debug above being printed... wha?
		
		
		
		// Enable rendering:
		foreach (Renderer component in rendererComponents)
		{
			if(component.gameObject.name == "bert")
			{
				if(hashenk)
				{
					component.enabled = true;
					hasbert=true;
					Debug.Log ("--------bert AND henk");
				}
				else
				{
					component.enabled = true;
					hasbert=true;
					Debug.Log ("----------just bert");
				}
			}
			else if(component.gameObject.name == "henk")
			{
				component.enabled = true;
				hashenk=true;
				Debug.Log ("------------- just henk");
			}
		}

		// Enable colliders:
		foreach (Collider component in colliderComponents)
		{
			if(component.gameObject.name == "bert")
			{
				component.enabled = true;
			}

			if(component.gameObject.name == "henk")
			{
				component.enabled = true;
			}
		}	
	}
	
	
	private void OnTrackingLost()
	{
		isBeingTracked = false;
		Renderer[] rendererComponents = GetComponentsInChildren<Renderer>(true);
		Collider[] colliderComponents = GetComponentsInChildren<Collider>(true);


		// Disable rendering:
		foreach (Renderer component in rendererComponents)
		{
			component.enabled = false;
			Debug.Log ("---------------Lost all!");
			hashenk=false;
			hasbert=false;
		}
		
		// Disable colliders:
		foreach (Collider component in colliderComponents)
		{
			component.enabled = false;
		}
		
		// Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " lost");
	}
	
	#endregion // PRIVATE_METHODS
}

Vuforia Two Markers Triggering a Action

March 13, 2014 - 1:23am #2

Hi,

 

this is just a programming logic issue, nothing relevant for this Forum.

However, one better (and easier) way to detect if 2 targets are simultaneously detected is to query the StateManager for the list of currently tracked TrackableBehaviours, and verify if the 2 objects are in the list at the same time, something like:

IEnumerable<TrackableBehaviour> trackedTrackables = 
    TrackerManager.Instance.GetStateManager().GetActiveTrackableBehaviours();

bool target1_found = false;
bool target2_found = false;

foreach (TrackableBehaviour tb in trackedTrackables)
{
    if (tb.Name.equals( "target_01" ) 
        target1_found = true;

    if (tb.Name.equals( "target_02" ) 
        target2_found = true;
} 

if (target1_found && target2_found)
{
    Debug.Log("Target 1 and 2 are tracked simultaneously...");
}

 

You can put that code in the Update() method of your own custom script, and attach the script to the ARCamera for instance.

I hope this helps.

 

Log in or register to post comments