/// <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(); } } } }
// Update is called once per frame void Update() { Vector3 pos = Poser.GetLocalPosition(source); Quaternion rot = Poser.GetLocalRotation(source); // Move rocket when thrust is active float thrust = Engine_manager.g_ThrustLevel; if (thrust > 0.0f) { pos += (transform.localRotation * Vector3.forward) * 2.0f * thrust; } float rotSpeed = 0.6f; transform.localRotation = Quaternion.Slerp(transform.localRotation, rot, rotSpeed * Time.deltaTime); float movSpeed = 0.8f; transform.localPosition = Vector3.Slerp(transform.localPosition, pos, movSpeed * Time.deltaTime); }
public Quaternion GetHandRot(MyHand hand) { return(flyDirection.GetLocalRotation(hand.handType)); }
public Quaternion DifferenceLocalRotation() { return(pose.GetLocalRotation(handType) * Quaternion.Inverse(pose.GetLastLocalRotation(handType))); }
// Update is called once per frame void Update() { // Adapted from: http://3dcognition.com/unity-flight-simulator-phase-2/ // and http://wiki.unity3d.com/index.php/SmoothMouseLook if (ResetButton.GetState(SteamVR_Input_Sources.Any)) { Reset(); } float altitude = transform.position.magnitude; float rotateSpeed = 30.0f; // degrees/second float speed = 0.5f * (altitude - 0.99f); // WASD movement, earth radius/second float mouseSpeed = 140.0f; // degrees rotation per pixel of mouse movement / second float transAmount = speed * Time.deltaTime; float rotateAmount = rotateSpeed * Time.deltaTime; // Update time zooming if (Input.GetKeyDown(".")) { TimeControl.ui_timelapse *= 4.0f; } if (Input.GetKeyDown(",")) { TimeControl.ui_timelapse /= 4.0f; } if (Input.GetKeyDown("/")) { TimeControl.ui_timelapse = 1.0f; } // Debug.Log("Orbital camera vive running"); float zoom = TimeZoomAxis.GetAxis(TimeHand); if (zoom > 0.0f) { Debug.Log(" time axis active: " + zoom); TimeControl.ui_timelapse = 16.0f * Mathf.Pow(4.0f, 1.0f + 2.0f * zoom); } else { TimeControl.ui_timelapse = 1.0f; } TimeControl.Update(); float rotX = 0.0f; float rotY = 0.0f; if (Input.GetMouseButton(0)) { rotX += Input.GetAxis("Mouse X") * mouseSpeed * Time.deltaTime; rotY -= Input.GetAxis("Mouse Y") * mouseSpeed * Time.deltaTime; } if (Input.GetKey("up")) { rotY += rotateAmount; } if (Input.GetKey("down")) { rotY -= rotateAmount; } if (Input.GetKey("left")) { rotX -= rotateAmount; } if (Input.GetKey("right")) { rotX += rotateAmount; } Vector3 rocket = new Vector3(0.0f, 0.0f, 0.0f); float rocketAccel = 50.0f; // m/s^2 acceleration in vacuum if (Input.GetKey("a")) { rocket.x = -rocketAccel; } if (Input.GetKey("d")) { rocket.x = +rocketAccel; } if (Input.GetKey("z")) { rocket.y = -rocketAccel; } if (Input.GetKey("q")) { rocket.y = +rocketAccel; } if (Input.GetKey("s")) { rocket.z = -rocketAccel; } if (Input.GetKey("w")) { rocket.z = +rocketAccel; } // Rotate keyboard rocket thrust to match local motion frame rocket = rocket.x * transform.right + rocket.y * transform.up + rocket.z * transform.forward; float thrust = RocketThrustAxis.GetAxis(RocketHand); Quaternion rot = RocketPose.GetLocalRotation(RocketHand); rot = transform.rotation * rot; // local to world rotation Vector3 rocketForward = rot * Vector3.forward; if (thrust > 0.0f) { Debug.Log(" thrust axis active: " + thrust + " direction " + rocketForward); rocket += thrust * rocketForward * rocketAccel; // FIXME: rotate to match controller orientation Engine_manager.g_ThrustLevel = thrust; } else { Engine_manager.g_ThrustLevel = 0.0f; } float me = 5.972e24f; // mass of Earth, in kilograms float G = 6.67408e-11f; // gravitational constant, MKS units float r = P.magnitude; // distance to spacecraft, in meters float accel = -G * me / (r * r); // scalar acceleration due to gravity (m/s^2) Vector3 A = P * (accel / P.magnitude); // vector acceleration due to gravity A += rocket; // acceleration due to rocket float dt = Time.deltaTime * TimeControl.timelapse; V = V + dt * A; // Euler update for velocity P = P + dt * V; // Euler update for position float height = (P.magnitude - Re) / (km); // kilometers altitude float airdrag = 0.0f; if (height < 60.0f) { float air_density = Mathf.Exp(-height / 8.0f); float dragfactor = 0.01f + 0.2f * Vector3.Cross(rocketForward.normalized, V.normalized).magnitude; airdrag = dragfactor * air_density; float dragloss = (1.0f - airdrag * dt); if (dragloss < 0.5f) { dragloss = 0.5f; } V = V * dragloss; airdrag *= V.magnitude; } float min_altitude = 1.00001f * Re; // stay outside of the planet if (P.magnitude < min_altitude) { P = P * (min_altitude / P.magnitude); } float max_altitude = 100.0f * Re; // stay fairly near the planet if (P.magnitude > max_altitude) { P = P * (max_altitude / P.magnitude); V = V * 0.001f; } // Bake output camera position Vector3 pos = P * (1.0f / km);// copy out simulated position to GUI position (in Earth radii) //pos.y-=1.8f; // make up for user height transform.position = pos; transform.Rotate(rotY, rotX, 0); // Update the text readout gizmo var TimeReadout = GameObject.FindWithTag("TimeReadout"); if (TimeReadout) { TimeReadout.transform.localPosition = TimePose.GetLocalPosition(TimeHand); TimeReadout.transform.localRotation = TimePose.GetLocalRotation(TimeHand); } var TextReadout = GameObject.FindWithTag("TimeReadoutText"); if (TextReadout) { float alt = (P.magnitude - Re) / km; float vel = V.magnitude / km; string pre = ""; if (airdrag > 0.0) { pre = "Air drag: " + string.Format("{0:F1}", airdrag) + " m/s/s\n"; } string text = pre + "Time: x" + string.Format("{0:F1}", TimeControl.timelapse) + "\n" + "Thrust: " + string.Format("{0:F0}", rocket.magnitude) + " m/s/s\n" + "Speed: " + string.Format("{0:F2}", vel) + " km/s\n" + "Vertical: " + string.Format("{0:F2}", Vector3.Dot(P.normalized, V) / km) + " km/s\n" + "Altitude: " + (int)alt + " km"; TextReadout.GetComponent <TextMesh>().text = text; } if (Input.GetKey("x") || Input.GetKey("escape")) { Application.Quit(); } }
// Update is called once per frame void FixedUpdate() { if (vehicle) { // Apply keyboard motor and steering controls throttle = throttleAction.GetAxis(throttleSource); brake = brakeAction.GetAxis(brakeSource); vehicle.complementary_filter(0.1f, ref vehicle.cur_motor_power, throttle - brake); Vector3 headPos = headPoser.GetLocalPosition(headSource); localHead = headPos; Quaternion headRot = headPoser.GetLocalRotation(headSource); // Slightly bank the user's head during turns, // so the real gravity vector lines up with apparent acceleration. // Disadvantage: mismatch in user's roll gyros. vehicle.complementary_filter(0.1f, ref bank, vehicle.centripital); if (BankingTurns) // Apply the turn // Reset full transform { BankingTurns.transform.localRotation = Quaternion.Euler(0.0f, 0.0f, 0.0f); BankingTurns.transform.localPosition = new Vector3(0.0f, 0.0f, 0.0f); // Rotate around the user's head position worldHead = BankingTurns.transform.TransformPoint(headPos); Vector3 axis = BankingTurns.transform.forward; // vehicle.last_velocity; axis.y = 0.0f; BankingTurns.transform.RotateAround(worldHead, axis, -30.0f * bank); } Vector3 framePos = framePoser.GetLocalPosition(frameSource); Quaternion frameRot = framePoser.GetLocalRotation(frameSource); if (VRAreaOffset && !setupVRArea && framePos.x != 0.0f) { // Locate VR camera relative to the physical frame: // SteamVR does wacky things with the initial camera position, // putting 0,0,0 as the center of the Steam VR room area. VRAreaOffset.transform.localPosition = new Vector3(-framePos.x, -framePos.y + 1.05f - 0.4f, -framePos.z + 0.7f); setupVRArea = true; } // Plant rollcage relative to the physical frame rollcage.transform.localPosition = framePos + new Vector3(0.0f, -0.5f, -1.0f); rollcage.transform.localRotation = frameRot * Quaternion.Euler(-60.0f, 180.0f, 0.0f); handlebars.transform.localPosition = framePos + new Vector3(0.0f, -0.2f, -0.10f); Vector3 steerPos = steerPoser.GetLocalPosition(steerSource); Quaternion steerRot = steerPoser.GetLocalRotation(steerSource); steerQuat = steerRot * Quaternion.Inverse(frameRot); float angle = 0.0f; steerQuat.ToAngleAxis(out angle, out steerAxis); if (Mathf.Abs(steerAxis.y) > 0.7f) // we're in a reasonable steering configuration { steer = angle; if (steerQuat.w > 0.0f) { steer = -angle; } if (steer > 180.0f) { steer -= 360.0f; // rotation is around zero } steer = -steer; steer -= 30.0f; // baked-in rotation offset (steering trim) vehicle.complementary_filter(0.1f, ref vehicle.cur_steer, steer / 45.0f); handlebars.transform.localRotation = frameRot * Quaternion.Euler(-90.0f, 180.0f, 0.0f) * Quaternion.Euler(0.0f, steer, 0.0f); } } }
private void OnPoseChanged(SteamVR_Action_Pose actionIn, SteamVR_Input_Sources hand) { transform.position = actionIn.GetLocalPosition(hand); transform.rotation = actionIn.GetLocalRotation(hand); }