1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- 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;
- }
- [RequireComponent(typeof(IBicycleController))]
- public class SensorBikeController : MonoBehaviour
- {
- public enum SteeringMode { frontWheel, Leaning, HMD };
- public PolarRotationMapping polarRotationMapping;
- public FrontWheelTrackerConfig frontWheelTrackerConfig;
- 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);
- var rot = player.transform.rotation;
- var yaw = Mathf.Asin(2 * rot.x * rot.y + 2 * rot.z * rot.w);
- bicycleController.CurrentSteerAngle = yaw;
- break;
- }
- Debug.Log("Updating Steering Angle to " + bicycleController.CurrentSteerAngle);
- }
- private void SetLeaningAngle()
- {
- //bicycleController.CurrentLeaningAngle =
- }
- private void SetSpeed(SpeedSensorData speedData)
- {
- bicycleController.CurrentSpeed = speedData.Speed;
- }
- }
- }
|