devNotes 8-04-16 Controller Interaction with Argos Sphere

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using Valve.VR;

public class GetSteamCtrl_Transform : MonoBehaviour
{
    private Text dbg_Text;
    public GameObject ctrl1;
    public GameObject ctrl2;

    public GameObject reticle_Prefab;

    private GameObject ret1;
    private GameObject ret2;

    private GameObject argosSphere;

    // Use this for initialization
	void Start ()
    {
        dbg_Text = GetComponent<Text>();

        argosSphere = GameObject.Find("ArgosSphere");

        ret1 = Instantiate(reticle_Prefab);
        ret2 = Instantiate(reticle_Prefab);
    }
	
	// Update is called once per frame
	void Update ()
    {
        dbg_Text.text = "C1 pos = " + ctrl1.transform.position.ToString() + "C2 pos = " + ctrl2.transform.position.ToString();

        PositionCursor(ret1, ctrl1.transform);
        PositionCursor(ret2, ctrl2.transform);
    }

    public void PositionCursor(GameObject ret, Transform trans)
    {
        float R = argosSphere.GetComponent<ArgosSphere_Indexing>().radius;
        Vector3 vPC = argosSphere.transform.position - mPose.position;
        Vector3 vBpos = trans.position + Vector3.Dot(vPC, trans.forward) * trans.forward;
        Vector3 vBC = vBpos - argosSphere.transform.position;

        float BClen = vBC.magnitude;
        float BDlen = Mathf.Sqrt(R * R - BClen * BClen);

        Vector3 D = vBpos + trans.forward * BDlen;

        ret.transform.position = D;

        Vector3 vR = D - argosSphere.transform.position;

        Quaternion rotation = Quaternion.LookRotation(vR.normalized, Vector3.up);
        ret.transform.rotation = rotation;

        positionInnerRet(ret, D, trans, vR);
    }

    public void positionInnerRet(GameObject ret, Vector3 pos, Transform mPose, Vector3 vR)
    {
        Transform inner = ret.transform.GetChild(0);
        inner.position = pos;

        Quaternion rotation = Quaternion.LookRotation(vR.normalized, mPose.up);
        inner.rotation = rotation;
    }
}

New Sketch_04


New Sketch_08


public class UserMovement : MonoBehaviour
{
    public GameObject reticle;
    GameObject ArgosSphere;
    public Text d_Position;
    public GameObject screen;

    //public Toggle togCurs;
    //public Toggle togMLook;

    //public GameObject reticleOrgGO;
    //public GameObject cursor1GO;

    GameObject netCam;

    public class CursorTran
    {
        public Vector3 pos;
        public Transform normProjTrans;
        public Transform screenProjTrans;
        public bool bValid = false;
    };

    private CursorTran _cursorTran = new CursorTran();

    public CursorTran cursorTran
    {
        get { return _cursorTran; }
    }

    private bool _cursorTrailModeOn;

    public bool bCursorTrailModeOn
    {
        set { _cursorTrailModeOn = value; }
        get { return _cursorTrailModeOn;  }
    }

    public bool isCursorValid()
    {
        return _cursorTran.bValid;
    }
       
	// Use this for initialization
	void Start ()
    {
        netCam = GameObject.Find("Argos_Camera");
        ArgosSphere = GameObject.Find("Argos_Sphere");
        //StartCoroutine(StartCursor(1.5F, "1"));
    }

    IEnumerator StartCursor(float waitTime, string jomo)
    {
        yield return new WaitForSeconds(waitTime);
        //togCurs.isOn = true;
    }

    // Update is called once per frame
    void Update ()
    {
        //if(netCam == null)
        //{
        //    netCam = GameObject.Find("Argos_Camera");
        //}
        //else
        //{
        //    OnPoseUpdated(netCam.transform);
        //}
    }

    public Vector3 getCursorPostiion()
    {
        return cursorTran.pos - ArgosSphere.transform.position;
    }

    public void MoveScreenIndicator(Vector3 pos, Transform mPose, Vector3 vR)
    {
        screen.transform.position = pos;
        //vPaddleVel = (pos - vPaddlePos_Last)/Time.deltaTime;
        //vPaddlePos_Last = pos;
        Quaternion rotation = Quaternion.LookRotation(vR.normalized, mPose.up);
        screen.transform.rotation = rotation;
        _cursorTran.screenProjTrans = screen.transform;
    }

    private bool IsRayIntersecting(Transform mPose)
    {
        //Let s be the start point of the ray, and d a unit vector in the direction of the ray.
        //Let c be the center point of the sphere, and r its radius.
        Vector3 s = mPose.transform.position;
        Vector3 d = mPose.transform.forward;
        Vector3 c = ArgosSphere.transform.position;
        float r = ArgosSphere.GetComponent<ArgosSphere_Indexing>().radius;

        // Calculate ray's start offset from the sphere center
        Vector3 p = s - c;

        float rSquared = r * r;
        float p_d = Vector3.Dot(p, d);

        // The sphere is behind or surrounding the start point.
        //if (p_d < 0 || Vector3.Dot(p, p) < rSquared)
        //    return false;

        // Flatten p into the plane passing through c perpendicular to the ray.
        // This gives the closest approach of the ray to the center.
        Vector3 a = p - p_d * d;

        float aSquared = Vector3.Dot(a, a);

        // Closest approach is outside the sphere.
        if (aSquared > rSquared)
            return false;
        return true;
    }


    public void OnPoseUpdated(Transform mPose)
    {
        _cursorTran.bValid = IsRayIntersecting(mPose);

        if (_cursorTran.bValid)
        {
            float R = ArgosSphere.GetComponent<ArgosSphere_Indexing>().radius;
            Vector3 vPC = ArgosSphere.transform.position - mPose.position;
            Vector3 vBpos = mPose.position + Vector3.Dot(vPC, mPose.forward) * mPose.forward;
            Vector3 vBC = vBpos - ArgosSphere.transform.position;

            float BClen = vBC.magnitude;
            float BDlen = Mathf.Sqrt(R * R - BClen * BClen);

            Vector3 D = vBpos + mPose.forward * BDlen;
            //d_Position.text = D.ToString();

            reticle.transform.position = D;
            _cursorTran.pos = D;

            Vector3 vR = D - ArgosSphere.transform.position;

            Quaternion rotation = Quaternion.LookRotation(vR.normalized, Vector3.up);
            reticle.transform.rotation = rotation;
            _cursorTran.normProjTrans = reticle.transform;

            MoveScreenIndicator(D, mPose, vR);
        }
        //MoveBall();
    }
}

//======= Copyright (c) Valve Corporation, All rights reserved. ===============
//
// Purpose: For controlling in-game objects with tracked devices.
//
//=============================================================================

using UnityEngine;
using Valve.VR;

public class SteamVR_TrackedObject : MonoBehaviour
{
	public enum EIndex
	{
		None = -1,
		Hmd = (int)OpenVR.k_unTrackedDeviceIndex_Hmd,
		Device1,
		Device2,
		Device3,
		Device4,
		Device5,
		Device6,
		Device7,
		Device8,
		Device9,
		Device10,
		Device11,
		Device12,
		Device13,
		Device14,
		Device15
	}

	public EIndex index;
	public Transform origin; // if not set, relative to parent
    public bool isValid = false;

	private void OnNewPoses(params object[] args)
	{
		if (index == EIndex.None)
			return;

		var i = (int)index;

        isValid = false;
		var poses = (Valve.VR.TrackedDevicePose_t[])args[0];
		if (poses.Length <= i)
			return;

		if (!poses[i].bDeviceIsConnected)
			return;

		if (!poses[i].bPoseIsValid)
			return;

        isValid = true;

		var pose = new SteamVR_Utils.RigidTransform(poses[i].mDeviceToAbsoluteTracking);

		if (origin != null)
		{
			pose = new SteamVR_Utils.RigidTransform(origin) * pose;
			pose.pos.x *= origin.localScale.x;
			pose.pos.y *= origin.localScale.y;
			pose.pos.z *= origin.localScale.z;
			transform.position = pose.pos;
			transform.rotation = pose.rot;
		}
		else
		{
			transform.localPosition = pose.pos;
			transform.localRotation = pose.rot;
		}
	}

	void OnEnable()
	{
		var render = SteamVR_Render.instance;
		if (render == null)
		{
			enabled = false;
			return;
		}

		SteamVR_Utils.Event.Listen("new_poses", OnNewPoses);
	}

	void OnDisable()
	{
		SteamVR_Utils.Event.Remove("new_poses", OnNewPoses);
		isValid = false;
	}

	public void SetDeviceIndex(int index)
	{
		if (System.Enum.IsDefined(typeof(EIndex), index))
			this.index = (EIndex)index;
	}
}


using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections;
using System.Collections.Generic;

public class PanelManager : MonoBehaviour {

	public Animator initiallyOpen;

	private int m_OpenParameterId;
	private Animator m_Open;
	private GameObject m_PreviouslySelected;

	const string k_OpenTransitionName = "Open";
	const string k_ClosedStateName = "Closed";

	public void OnEnable()
	{
		m_OpenParameterId = Animator.StringToHash (k_OpenTransitionName);

		if (initiallyOpen == null)
			return;

		OpenPanel(initiallyOpen);
	}

	public void OpenPanel (Animator anim)
	{
		if (m_Open == anim)
			return;

		anim.gameObject.SetActive(true);
		var newPreviouslySelected = EventSystem.current.currentSelectedGameObject;

		anim.transform.SetAsLastSibling();

		CloseCurrent();

		m_PreviouslySelected = newPreviouslySelected;

		m_Open = anim;
		m_Open.SetBool(m_OpenParameterId, true);

		GameObject go = FindFirstEnabledSelectable(anim.gameObject);

		SetSelected(go);
	}

	static GameObject FindFirstEnabledSelectable (GameObject gameObject)
	{
		GameObject go = null;
		var selectables = gameObject.GetComponentsInChildren<Selectable> (true);
		foreach (var selectable in selectables) {
			if (selectable.IsActive () && selectable.IsInteractable ()) {
				go = selectable.gameObject;
				break;
			}
		}
		return go;
	}

	public void CloseCurrent()
	{
		if (m_Open == null)
			return;

		m_Open.SetBool(m_OpenParameterId, false);
		SetSelected(m_PreviouslySelected);
		StartCoroutine(DisablePanelDeleyed(m_Open));
		m_Open = null;
	}

	IEnumerator DisablePanelDeleyed(Animator anim)
	{
		bool closedStateReached = false;
		bool wantToClose = true;
		while (!closedStateReached && wantToClose)
		{
			if (!anim.IsInTransition(0))
				closedStateReached = anim.GetCurrentAnimatorStateInfo(0).IsName(k_ClosedStateName);

			wantToClose = !anim.GetBool(m_OpenParameterId);

			yield return new WaitForEndOfFrame();
		}

		if (wantToClose)
			anim.gameObject.SetActive(false);
	}

	private void SetSelected(GameObject go)
	{
		EventSystem.current.SetSelectedGameObject(go);

		var standaloneInputModule = EventSystem.current.currentInputModule as StandaloneInputModule;
		if (standaloneInputModule != null && standaloneInputModule.inputMode == StandaloneInputModule.InputMode.Buttons)
			return;

		EventSystem.current.SetSelectedGameObject(null);
	}
}

sdfbfdb2