Unity HTC vive根据手柄的位移 手势进行交互

实现了将切割磁感线的demo在VR环境中实现。由于手柄的交互与鼠标点击区别很大,在pc端的设计中,直接将几个坐标位置投影到摄像机平面上,只需要观察在二维平面内,鼠标点击的位移与目标物体的夹角关系,并通过角度控制目标物体的旋转交互。

但在VR环境中,视角绑定在头显上进行实时变化的,而且手柄的交互方式与鼠标有较大差别。通过世界坐标系下的物体属性进行修改。

继承了VRTK_InteractableObject,即为了实现目标物体与手柄的交互。为了检测到手柄与物体的碰撞,抓取,使用等操作。

声明SteamVR_TrackedObject对象,根据Steam vr sdk中拓展代码,构造手柄按钮事件。调用这个类中的手柄对象的方法,来实现左右手柄的监听功能,获取手柄位置等信息。

通过VRTK_DeviceFinder类,寻找有手柄,返回位置等信息,具体实现:

RightController = VRTK_DeviceFinder.GetControllerRightHand ();

通过SteamVR_Controller类获取手柄相关信息,

Device类:最重要的类,封装了跟踪设备的全部信息。

Update()函数:被SteamVR_Render脚本调用,更新各跟踪设备的信息。

Input()函数:根据Index初始化Device[]数组,并实例化所有16个Device。

var device = SteamVR_Controller.Input((int)trackedObjec.index);

下面进行判断,当我按下手柄的tigger时,记录手柄的位置。按住tigger时,实时刷新手柄的位置,并计算手柄的手势变换与目标物体位置的夹角。

夹角变换时,通过rotate方法,实现模型的旋转,随手柄手势变换速度的旋转。

代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using VRTK;
using VRTK.Highlighters;

public class dynamoControl : VRTK_InteractableObject {

	public SteamVR_TrackedObject trackedObjec;

	private GameObject RightController;
	public lightControl ll;

	public Camera ca;
	private bool IsSelect = false;
	public Transform target1;

	private Vector3 preConPosition;
	private Vector3 ConPosition;
	private Vector3 modelPos;
	private Vector3 localEluer;
	private float angle;

	private float RotateAngle;

	private Quaternion q;

	// Use this for initialization
	void Start () {

		//trackedObjec = GameObject.Find("Controller (right)").GetComponent<SteamVR_TrackedObject> ();

		RightController = VRTK_DeviceFinder.GetControllerRightHand ();

		modelPos = ca.WorldToScreenPoint(target1.transform.position);
		angle = localEluer.z;
		target1.transform.localEulerAngles = localEluer;

	}
	
	// Update is called once per frame
	void FixedUpdate () {
		preConPosition = ConPosition = RightController.transform.position;


		var device = SteamVR_Controller.Input((int)trackedObjec.index);

		if(device.GetTouchDown(SteamVR_Controller.ButtonMask.Trigger)){
			IsSelect = true;
			preConPosition = ConPosition = RightController.transform.position;
		}
		if(device.GetTouch(SteamVR_Controller.ButtonMask.Trigger)  && IsSelect){
			IsSelect = true;
			ConPosition = RightController.transform.position;

			Vector3 preConPosition2 = new Vector3 (preConPosition.x,preConPosition.y,0);
			Vector3 ConPosition2 = new Vector3 (ConPosition.x,ConPosition.y,0);
			Vector3 modelPos2 = new Vector3 (modelPos.x,modelPos.y,0);

			RotateAngle = Vector3.Angle(preConPosition2 - modelPos2,ConPosition2 - modelPos2);

			

			if (RotateAngle == 0) {
				preConPosition = ConPosition;
			}
			else {

				preConPosition2 = new Vector3 (preConPosition.x,preConPosition.y,0);
				ConPosition2 = new Vector3 (ConPosition.x,ConPosition.y,0);
				modelPos2 = new Vector3 (modelPos.x,modelPos.y,0);


				float inangle = Vector3.Angle(preConPosition2 - modelPos2,ConPosition2 - modelPos2);
				
				float k = q.z > 0 ? 1 : -1;
				localEluer.z += k * RotateAngle;

				angle = localEluer.z = Mathf.Clamp (localEluer.z,-36000,36000);


				localEluer.z = inangle*500;
				Debug.Log (localEluer);
				ll.duration = localEluer.z / 4;
						target1.transform.Rotate(localEluer);

				preConPosition = ConPosition;
			}
		}
		if(device.GetTouchUp(SteamVR_Controller.ButtonMask.Trigger)){
			IsSelect = false;
		}
	}
}

猜你喜欢

转载自blog.csdn.net/qq_34272906/article/details/80283410