/// <summary> /// Checks the Tracker-Status and Updates the Position, Rotation and Velocity. /// </summary> protected override void UpdateTracker() { // Setting 'trackingOK' to false (In case we have an Error and return before the end of the method). trackingOK = false; // Check if OpenVR-System is found correctly. if (OpenVR.System == null) { Debug.LogError("<b>DHUI</b> | DroneTracking_Vive | OpenVR-System was not found."); return; } // If we didn't set the Action Pose. if (_VRTrackerActionPose == null) { // Check if there is an Active Action called "VRTracker". SteamVR_Action_Pose ap = SteamVR_Input.GetAction <SteamVR_Action_Pose>("VRTracker"); if (ap.GetActive(SteamVR_Input_Sources.Any)) { _VRTrackerActionPose = ap; droneTrackerIndex = _VRTrackerActionPose.GetDeviceIndex(SteamVR_Input_Sources.Any); } else { Debug.LogError("<b>DHUI</b> | DroneTracking_Vive | No Active VR-Tracker was found."); return; } } // Copy Position and Rotation to the 'trackedTransform' and save the Velocity. // If 'trackTransformAsLocal' is true: Copy the real tracked pose to 'trackedTransform' as localPosition & localRotation if (trackTransformAsLocal) { trackedTransform.localPosition = _VRTrackerActionPose.GetLocalPosition(SteamVR_Input_Sources.Any) - trackerToDroneOffset_Position; trackedTransform.localRotation = _VRTrackerActionPose.GetLocalRotation(SteamVR_Input_Sources.Any) * Quaternion.Euler(trackerToDroneOffset_Rotation); } // If 'trackTransformAsLocal' is false (= default): Copy the real tracked pose to 'trackedTransform' as (global) position & rotation else { trackedTransform.position = _VRTrackerActionPose.GetLocalPosition(SteamVR_Input_Sources.Any) - trackerToDroneOffset_Position; trackedTransform.rotation = _VRTrackerActionPose.GetLocalRotation(SteamVR_Input_Sources.Any) * Quaternion.Euler(trackerToDroneOffset_Rotation); } velocity = _VRTrackerActionPose.GetVelocity(SteamVR_Input_Sources.Any); // Calculate the current pose for our device. [See https://github.com/ValveSoftware/openvr/wiki/IVRSystem::GetDeviceToAbsoluteTrackingPose] TrackedDevicePose_t[] trackedDevicePoses = new TrackedDevicePose_t[OpenVR.k_unMaxTrackedDeviceCount]; OpenVR.System.GetDeviceToAbsoluteTrackingPose(ETrackingUniverseOrigin.TrackingUniverseStanding, 0.0f, trackedDevicePoses); TrackedDevicePose_t droneTrackedDevicePose = trackedDevicePoses[droneTrackerIndex]; // If the 'TrackedDevicePose' of the drone is valid, connected and running ok, we can set the 'trackingOK' to true. if (droneTrackedDevicePose.bPoseIsValid && droneTrackedDevicePose.bDeviceIsConnected && droneTrackedDevicePose.eTrackingResult == ETrackingResult.Running_OK) { trackingOK = true; } }
private void handlePose() { if (debugPoseMarkerStart != null && poseAction.GetActive(_controller)) { Vector3 posePos = poseAction.GetLocalPosition(_controller); debugPoseMarkerStart.transform.localPosition = posePos; LineRenderer line = debugPoseMarkerStart.GetComponent <LineRenderer>(); line.transform.position = debugPoseMarkerStart.transform.position; if (line != null) { line.SetPosition(0, debugPoseMarkerStart.transform.position); line.transform.localRotation = poseAction.GetLocalRotation(_controller); RaycastHit hit; // Shorten the default magnitude of the beam by 0.3 Vector3 direction = poseAction.GetLocalRotation(_controller) * vectorFoward * 0.3f; Vector3 endPoint = line.transform.position + direction * 10000; line.SetPosition(1, endPoint); debugPoseMarkerEnd.transform.position = endPoint; Ray ray = new Ray(line.transform.position, direction); if (Physics.Raycast(ray, out hit)) { //Debug.Log("Hit!"); debugPoseMarkerEnd.transform.position = hit.point; line.SetPosition(1, hit.point); handleHit(hit); } else { //Debug.Log("Missed!"); //line.SetPosition(1, line.transform.position + direction * 10000); //debugPoseMarkerEnd.transform.position = line.transform.position + direction * 10000; handleMiss(); } } } }