AR Claw Machine

Project

An AR claw machine prototype with image tracking integration to allow for different types of claw machines in an AR environment.


Project Details

Team Size: 1

Year: 2023

Length: 2 days

Engine: Unity

Source Control: GitHub

Notable Work

Overview

  • Functioning physics-based claw machine
  • Utilized XRToolkit's ARTrackedImageManager & ReferenceImageLibrary for handling unique model loading
  • Different claw machine models based on tracked images
  • Fine tweaked physics-based rewards to better emulate a real-world claw machine

Tracked Images

A custom class to handle unique object instantiating based on tracked images. The custom class works in tangent with XRToolkit's ARTrackedImageManager at runtime.

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.ARFoundation;
using UnityEngine.XR.ARSubsystems;

public class ImageTrackingManager : MonoBehaviour
{
	[Header("XR References")]
	[SerializeField] private ARTrackedImageManager trackedImageManager;
	[SerializeField] private XRReferenceImageLibrary referenceImageLibrary;

	[Header("Claw Machines")]
	[SerializeField] private GameObject[] clawMachines;

	private static Dictionary trackingList = new Dictionary();

	private void OnEnable()
	{
		/// Setup tracking Dictionary
		for (int i = 0; i < clawMachines.Length; i++)
		{
			trackingList.Add(referenceImageLibrary[i].guid, clawMachines[i]);
		}
		
		trackedImageManager.trackedImagesChanged += OnTrackedImageChanged;
	}

	private void OnDisable()
	{
		trackedImageManager.trackedImagesChanged -= OnTrackedImageChanged;
	}

	private void OnTrackedImageChanged(ARTrackedImagesChangedEventArgs obj)
	{
		/// Handle object instantiating
		foreach (ARTrackedImage image in obj.added)
		{
			if (trackingList.ContainsKey(image.referenceImage.guid))
			{
				/// Rotate claw machine on Y axis toward the camera
				Vector3 cameraToImage = Camera.main.transform.position - image.transform.position;
				Quaternion initialRotation = trackingList[image.referenceImage.guid].transform.rotation;
				Quaternion lookRotation = Quaternion.LookRotation(cameraToImage, Vector3.up);
				
				float angle = Quaternion.Angle(Quaternion.Euler(0, lookRotation.eulerAngles.y, 0), initialRotation);
				
				Quaternion finalRotation = Quaternion.Euler(initialRotation.eulerAngles.x, angle, initialRotation.eulerAngles.z);
				
				Instantiate(trackingList[image.referenceImage.guid], image.transform.position, finalRotation);
			}
		}
	}
}													
										


Links