We have a series of VG onboarding tasks to show how to tackle different practical use cases using VirtualGrasp in a VR application. In VirtualGrasp SDK, they are packed in VirtualGrasp\Scenes\onboarding.
Task Description
Interaction behaviors wanted
- We want to implement a button without using any physical components, but only use VirtualGrasp’s kinematic joints.
- Push once a button stay in lowered position, light turn on.
- Push another time button come back to original position, light turns off.
Tips for VR developers
- Which joint type should be assigned to the button?
- Which state affordance to use to allow button be switch between these two states?
- How to determine when light should be on or off (use GetObjectJointState function)?
- More systemtic understanding can be obtained in push interaction.
Solution
In VirtualGrasp SDK, we packed the solution of this task in VirtualGrasp\Scenes\onboarding.
VirtualGrasp\Scenes\onboarding\VG_Onboarding.unity
//VirtualGrasp\Scenes\onboarding\Scripts\ToggleLight.cs:
using UnityEngine;
using VirtualGrasp;
/**
* ToggleLight shows as a tutorial on a non-physical two-stage button setup
* through VG_Articulation and how to use VG_Controller.GetObjectJointState to toggle light on and off.
*/
[LIBVIRTUALGRASP_UNITY_SCRIPT]
[HelpURL("https://docs.virtualgrasp.com/unity_vgonboarding_task1.html")]
public class ToggleLight : MonoBehaviour
{
public GameObject m_light = null;
public GameObject m_light2 = null;
private VG_Articulation m_articulation = null;
public AudioSource m_audioSourceOn;
public AudioSource m_audioSourceOff;
private float m_state;
private bool m_binState = false;
void Start()
{
if (TryGetComponent<VG_Articulation>(out m_articulation))
{
VG_Controller.GetObjectJointState(transform, out m_state);
EvaluateState(m_state);
}
}
void Update()
{
VG_Controller.GetObjectJointState(transform, out float newState);
if (newState != m_state)
EvaluateState(newState);
m_state = newState;
}
void EvaluateState(float state)
{
if (state == m_articulation.m_min && m_binState)
{
m_binState = false;
m_light.SetActive(false);
m_light2.SetActive(false);
m_audioSourceOff.Play();
}
else if (state == m_articulation.m_max && !m_binState)
{
m_binState = true;
m_light.SetActive(true);
m_light2.SetActive(true);
m_audioSourceOn.Play();
}
}
}
is the script showing how to use API function GetObjectJointState to get the object’s joint state in order to determine when the light is on or off.