123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- using System;
- using Display;
- using UnityEngine;
- using UnityEngine.Serialization;
- using Valve.VR;
- namespace Tracking
- {
- public enum CalibrationMode
- {
- None,
- MatchOnly,
- AutoImmediate,
- AutoCountdown
- }
- public class MatchPlayerPositionAndCalibrate : MonoBehaviour
- {
- public Transform player;
- public CalibrationMode calibrationMode;
- public CountdownDisplay countdownHud;
- [Header("Front Wheel")] public Transform frontWheelDesiredPosition;
- public FrontWheelTracker frontWheelTracker;
- [Header("Trainer Leg")] public Transform legDesiredPosition;
- public KineticLegTracker legTracker;
- [Header("HMD")] public bool calibrateHmd = true;
- public LookStraightDisplay lookStraightHud;
- public CameraTracker hmdTracker;
- public Transform bikeTransform;
- private Transform fwtTransform;
- private Transform legTransform;
- private Transform playerTransform;
- private SteamVR_TrackedObject fwTrackedObject;
- private SteamVR_TrackedObject legTrackedObject;
- private int matching;
- private bool autoSet;
- private void Start()
- {
- fwtTransform = frontWheelTracker.transform;
- legTransform = legTracker.transform;
- playerTransform = player.transform;
- fwTrackedObject = legTracker.GetComponent<SteamVR_TrackedObject>();
- legTrackedObject = frontWheelTracker.GetComponent<SteamVR_TrackedObject>();
- }
- public void Match()
- {
- matching = 1;
- }
- public void setDesiredToReal()
- {
- //TODO
- }
- private void MatchFrontWheel()
- {
- var dif = frontWheelDesiredPosition.position - fwtTransform.position;
- playerTransform.position += dif;
- }
- private void MatchLeg()
- {
- //The only way to match it now is by rotating
- var fwtPosition = fwtTransform.position;
- var fwtToDesiredLeg = legDesiredPosition.position - fwtPosition;
- var fwtToRealLeg = legTransform.position - fwtPosition;
- var dl2d = new Vector2(fwtToDesiredLeg.x, fwtToDesiredLeg.z);
- var dr2d = new Vector2(fwtToRealLeg.x, fwtToRealLeg.z);
- var angle = Vector2.Angle(dl2d, dr2d);
- playerTransform.RotateAround(fwtPosition, playerTransform.up, -angle);
- }
- private void Update()
- {
- AutoCalibration();
- DoMatching();
- }
- private void AutoCalibration()
- {
- if (calibrationMode == CalibrationMode.None || autoSet || !fwTrackedObject.isValid ||
- !legTrackedObject.isValid) return;
- //TODO: show tracker status
- if (calibrationMode == CalibrationMode.AutoCountdown)
- {
- var display = Instantiate(countdownHud);
- display.OnCountdownDone = () =>
- {
- Match();
- return "Calibrated!";
- };
- autoSet = true;
- }
- else
- {
- Match();
- autoSet = true;
- }
- }
- private void DoMatching()
- {
- if (matching == 1)
- {
- MatchFrontWheel();
- matching++;
- }
- else if (matching == 2)
- {
- MatchLeg();
- if (calibrationMode == CalibrationMode.MatchOnly || calibrationMode == CalibrationMode.None)
- {
- matching = 0;
- }
- else
- {
- matching++;
- }
- }
- else if (matching == 3)
- {
- legTracker.Calibrate();
- frontWheelTracker.Calibrate();
- matching = 0;
- DoLookStraightIfWanted();
- }
- }
- private void DoLookStraightIfWanted()
- {
- if (!calibrateHmd) return;
- var lookStraightDisplay = Instantiate(lookStraightHud);
- lookStraightDisplay.OnDone += OnLookStraightDone;
- var lTransform = lookStraightDisplay.transform;
- var newPos = bikeTransform.position + bikeTransform.forward * 1.4f;
- newPos.y = hmdTracker.transform.position.y;
- lTransform.position = newPos;
- }
- private void OnLookStraightDone()
- {
- hmdTracker.Calibrate();
- }
- }
- }
|