123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- using System;
- using Controller.Bicycle;
- using Controller.Lean;
- using Sensors;
- using Sensors.ANT;
- using Tracking;
- using UnityEngine;
- namespace Controller
- {
- [Serializable]
- public struct FrontWheelTrackerConfig
- {
- public FrontWheelTracker frontWheelTracker;
- public float multiplicator;
- public float AdjustedRotation => frontWheelTracker.SteerRotation * multiplicator;
- }
- [Serializable]
- public struct HMDTrackerConfig
- {
- public HMDTracker cameraTracker;
- public float multiplicator;
- public float AdjustedRotation => cameraTracker.SteerRotation * multiplicator;
- }
- [RequireComponent(typeof(IBicycleController))]
- public class SensorBikeController : MonoBehaviour
- {
- public enum SteeringMode { frontWheel, Leaning, HMD };
- public PolarRotationMapping polarRotationMapping;
- public FrontWheelTrackerConfig frontWheelTrackerConfig;
- public HMDTrackerConfig hmdTrackerConfig;
- private float leanFactor;
- public bool steer = true;
- public bool accelerate = true;
- public bool lean = true;
- public SteeringMode steeringSelection;
- private IBicycleController bicycleController;
- private bool isFrontWheelTrackerNotNull;
- private BikeSensorData sensorData;
- private GameObject player;
- private void Start()
- {
- isFrontWheelTrackerNotNull = frontWheelTrackerConfig.frontWheelTracker != null;
- bicycleController = GetComponent<IBicycleController>();
- sensorData = BikeSensorData.Instance;
- player = GameObject.FindGameObjectWithTag("HMDTracker");
- // Dummy assignment
- steeringSelection = SteeringMode.frontWheel;
- leanFactor = 90f / (polarRotationMapping.maxRight - polarRotationMapping.center);
- }
- private void Update()
- {
- Debug.Log("Bike Sensor Controller called");
- var speedData = sensorData.SpeedData;
- if (speedData != null && accelerate) SetSpeed(speedData.Value);
- if (steer) SetSteer();
- if (lean) SetLeaningAngle();
- }
- private void SetSteer()
- {
- Debug.Log("Updating Steering");
- switch (steeringSelection) {
- case SteeringMode.frontWheel:
- if (isFrontWheelTrackerNotNull) {
- bicycleController.CurrentSteerAngle = frontWheelTrackerConfig.AdjustedRotation * 2f;
- }
- break;
- case SteeringMode.Leaning:
- var polarData = sensorData.BleData;
- if (polarData != null)
- {
- bicycleController.CurrentSteerAngle = (polarData.Value.Acc.y - polarRotationMapping.center) * leanFactor;
- }
- break;
- case SteeringMode.HMD:
- // TODO: Convert steering relative to bike
- Debug.Log("HMD Rotation " + player.transform.eulerAngles);
- //Lisa:
- //var rot = player.transform.localRotation.eulerAngles.z; // https://answers.unity.com/questions/149801/transformrotation-in-degrees-instead-of-0-to-1.html
- //var yaw = rot *3f; // da kopfdrehung relativ gering im vergleich zu einer lenkraddrehung, faktor ggf. erhöhen
- Debug.Log("Local Euler Angles " + player.transform.localRotation.eulerAngles);
- //Julia:
- // https://wirewhiz.com/vr-body-tracking-tutorial-pt-1/
- // Bestimme Winkel zwischen Kopf und Fahrrad
- /*var rot_player = player.transform.rotation;
- var rot_bike = GameObject.FindGameObjectWithTag("bike").transform.rotation;
- var yaw = Quaternion.Angle(Quaternion.Euler(0, rot_bike.eulerAngles.y, 0),
- Quaternion.Euler(0, rot_player.eulerAngles.y, 0));*/
- //Debug.Log("Calculated Yaw " + yaw);
- bicycleController.CurrentSteerAngle = hmdTrackerConfig.AdjustedRotation - 5f;
- break;
- }
- Debug.Log("Updating Steering Angle to " + bicycleController.CurrentSteerAngle);
- }
- private void SetLeaningAngle()
- {
- //bicycleController.CurrentLeaningAngle =
- }
- private void SetSpeed(SpeedSensorData speedData)
- {
- bicycleController.CurrentSpeed = speedData.Speed;
- }
- }
- }
|