Explorar o código

Integrating new Interface

Marcel Zickler %!s(int64=3) %!d(string=hai) anos
pai
achega
1452283513

+ 3 - 3
Assembly-CSharp.csproj

@@ -258,13 +258,13 @@
      <Compile Include="Assets\Scripts\Calibration\BikeReset.cs" />
      <Compile Include="Assets\Scripts\Calibration\MovePlayerPosition.cs" />
      <Compile Include="Assets\Scripts\Calibration\PersistatePlayerPosition.cs" />
-     <Compile Include="Assets\Scripts\Controller\BicycleController.cs" />
+     <Compile Include="Assets\Scripts\Controller\Bicycle\IBicycleController.cs" />
+     <Compile Include="Assets\Scripts\Controller\Bicycle\WcBicycleController.cs" />
+     <Compile Include="Assets\Scripts\Controller\Bicycle\WheelConfig.cs" />
      <Compile Include="Assets\Scripts\Controller\GamepadBikeController.cs" />
-     <Compile Include="Assets\Scripts\Controller\IBicycleController.cs" />
      <Compile Include="Assets\Scripts\Controller\KeyboardBikeController.cs" />
      <Compile Include="Assets\Scripts\Controller\SensorBikeController.cs" />
      <Compile Include="Assets\Scripts\Controller\ViveBikeController.cs" />
-     <Compile Include="Assets\Scripts\Controller\WheelConfig.cs" />
      <Compile Include="Assets\Scripts\Display\BikeDataDisplay.cs" />
      <Compile Include="Assets\Scripts\Display\DebugDisplay.cs" />
      <Compile Include="Assets\Scripts\Display\ViveTrackerDebugDisplay.cs" />

A diferenza do arquivo foi suprimida porque é demasiado grande
+ 201 - 123
Assets/Scenes/MainScene.unity


+ 6 - 5
Assets/Scripts/Animation/BikeAnimation.cs

@@ -1,5 +1,6 @@
 using System.Collections.Generic;
 using Controller;
+using Controller.Bicycle;
 using Sensors;
 using UnityEngine;
 
@@ -22,7 +23,7 @@ namespace Animation
         [Header("Parameters")] public float wheelCircumference = 2.096f;
         public float defaultCadence = 80;
 
-        private BicycleController bicycleController;
+        private WcBicycleController wcBicycleController;
 
         private readonly Dictionary<Rotatable, Vector3> initialPositions = new Dictionary<Rotatable, Vector3>();
         private readonly Dictionary<Rotatable, Quaternion> initialRotations = new Dictionary<Rotatable, Quaternion>();
@@ -33,7 +34,7 @@ namespace Animation
         // Start is called before the first frame update
         void Start()
         {
-            bicycleController = GetComponentInParent<BicycleController>();
+            wcBicycleController = GetComponentInParent<WcBicycleController>();
             initialPositions[crankSet] = crankSet.gameObject.transform.localPosition;
             initialPositions[fork] = fork.gameObject.transform.localPosition;
             initialRotations[crankSet] = crankSet.gameObject.transform.localRotation;
@@ -46,13 +47,13 @@ namespace Animation
             var cadenceAngle = cadence / 60 * 360 * Time.deltaTime; //rps * 360 deg for one round
 
             RotateWheels();
-            RotateAroundDefinedAxis(fork, bicycleController.CurrentSteerAngle);
+            RotateAroundDefinedAxis(fork, wcBicycleController.CurrentSteerAngle);
             RotateAroundDefinedAxis(crankSet, cadenceAngle, false);
         }
 
         private void RotateWheels()
         {
-            var speed = bicycleController.rigidBody.velocity.magnitude;
+            var speed = wcBicycleController.rigidBody.velocity.magnitude;
             var rps = speed / wheelCircumference;
             var angle = rps * Time.deltaTime;
         
@@ -85,7 +86,7 @@ namespace Animation
         void RotateObject(GameObject obj, float multiplier)
         {
             obj.transform.Rotate(
-                Time.deltaTime * bicycleController.rigidBody.velocity.magnitude * (360f / 12f) * multiplier, 0,
+                Time.deltaTime * wcBicycleController.rigidBody.velocity.magnitude * (360f / 12f) * multiplier, 0,
                 0);
             //obj.transform.Rotate(Time.deltaTime * rotSpeed * (360f / oneRotationSpeed) * multiplier, 0, 0);
         }

+ 3 - 0
Assets/Scripts/Controller/Bicycle.meta

@@ -0,0 +1,3 @@
+fileFormatVersion: 2
+guid: f392041232b34478a5b2921c21039da5
+timeCreated: 1607500363

+ 19 - 0
Assets/Scripts/Controller/Bicycle/IBicycleController.cs

@@ -0,0 +1,19 @@
+namespace Controller.Bicycle
+{
+    public enum BicycleControllerMode
+    {
+        Independent,
+        LeaningAngleDependentOnSteerAngle
+    }
+
+    internal interface IBicycleController
+    {
+        BicycleControllerMode ControllerMode { get; set; }
+
+        float CurrentSpeed { get; set; }
+
+        float CurrentLeaningAngle { get; set; }
+
+        float CurrentSteerAngle { get; set; }
+    }
+}

+ 0 - 0
Assets/Scripts/Controller/IBicycleController.cs.meta → Assets/Scripts/Controller/Bicycle/IBicycleController.cs.meta


+ 0 - 0
Assets/Scripts/Controller/BicycleController.cs.meta → Assets/Scripts/Controller/Bicycle/WCBicycleController.cs.meta


+ 41 - 23
Assets/Scripts/Controller/BicycleController.cs → Assets/Scripts/Controller/Bicycle/WcBicycleController.cs

@@ -1,15 +1,12 @@
-using System;
-using System.Collections;
-using System.Collections.Generic;
-using System.Linq;
+using System.Collections.Generic;
+using Controller.Bicycle;
 using UnityEngine;
-using UnityEngine.EventSystems;
 using UnityEngine.Serialization;
 using Valve.VR.InteractionSystem;
 
-namespace Controller
+namespace Controller.Bicycle
 {
-    public class BicycleController : MonoBehaviour
+    public class WcBicycleController : MonoBehaviour, IBicycleController
     {
         #region Variables
 
@@ -23,25 +20,24 @@ namespace Controller
         [Header("Values")] public float offsetCollidersFromWheel = 0.25f;
         public float maxLeaningAngle = 35f;
         public float maxSteeringAngle = 80f;
-        public float maxMotorTorque = 2000f;
+        [Range(0,15)]
+        [Tooltip("Speed in m/s")]
+        public float maxSpeed = 11.111f; //40 km/h
         public float maxBreakTorque = 2000f;
+        public float maxMotorTorque = 1000f;
 
-        public float CurrentSteerAngle
-        {
-            get => currentSteerAngle;
-            set => currentSteerAngle = Mathf.Clamp(value, -maxSteeringAngle, maxSteeringAngle);
-        }
+        public BicycleControllerMode ControllerMode { get; set; }
 
-        public float CurrentMotorTorque
+        public float CurrentSpeed
         {
-            get => currentMotorTorque;
-            set => currentMotorTorque = Mathf.Clamp(value, -maxMotorTorque, maxMotorTorque);
+            get => currentSpeed;
+            set => currentSpeed = Mathf.Clamp(value, 0, maxSpeed);
         }
 
-        public float CurrentBrakeTorque
+        public float CurrentSteerAngle
         {
-            get => currentBrakeTorque;
-            set => currentBrakeTorque = Mathf.Clamp(value, -maxBreakTorque, maxBreakTorque);
+            get => currentSteerAngle;
+            set => currentSteerAngle = Mathf.Clamp(value, -maxSteeringAngle, maxSteeringAngle);
         }
 
         public float CurrentLeaningAngle
@@ -53,9 +49,8 @@ namespace Controller
         private WheelCollider[] allWheelColliders;
         private float initialWheelColliderY;
         private float currentSteerAngle = 0f;
-        private float currentMotorTorque = 0f;
-        private float currentBrakeTorque = 0f;
         private float currentLeaningAngle = 0f;
+        private float currentSpeed;
 
         #endregion
 
@@ -94,10 +89,33 @@ namespace Controller
 
         private void ControlTorque(IEnumerable<WheelCollider> colliders)
         {
+            var rbSpeed = rigidBody.velocity.magnitude;
+            var speedDif = CurrentSpeed - rbSpeed;
+            var ratio = speedDif / maxSpeed;
+           
+
+            var brakeTorque = 0f;
+            var motorTorque = 0f;
+            
+            if (speedDif >= .1f) // 0.36 km/h
+            {
+                var torque = maxMotorTorque * ratio;
+                //Debug.Log($"SpeedDif = {speedDif} -> applying Torque {torque} (Ratio: {ratio})");
+                brakeTorque = 0;
+                motorTorque = torque;
+            }
+            else if (speedDif <= -.1f)
+            {
+                var torque = -maxBreakTorque * ratio;
+                //Debug.Log($"SpeedDif = {speedDif} -> applying brake Torque {torque} (Ratio: {ratio})");
+                motorTorque = 0;
+                brakeTorque = torque;
+            }
+
             foreach (var c in colliders)
             {
-                c.motorTorque = CurrentMotorTorque;
-                c.brakeTorque = CurrentBrakeTorque;
+                c.motorTorque = motorTorque;
+                c.brakeTorque = brakeTorque;
             }
         }
 

+ 1 - 1
Assets/Scripts/Controller/WheelConfig.cs → Assets/Scripts/Controller/Bicycle/WheelConfig.cs

@@ -1,6 +1,6 @@
 using UnityEngine;
 
-namespace Controller
+namespace Controller.Bicycle
 {
     [System.Serializable]
     public class WheelConfig

+ 0 - 0
Assets/Scripts/Controller/WheelConfig.cs.meta → Assets/Scripts/Controller/Bicycle/WheelConfig.cs.meta


+ 16 - 29
Assets/Scripts/Controller/GamepadBikeController.cs

@@ -1,11 +1,12 @@
-using JetBrains.Annotations;
+using Controller.Bicycle;
+using JetBrains.Annotations;
 using UnityEngine;
 using UnityEngine.InputSystem;
 
 namespace Controller
 {
     [RequireComponent(typeof(PlayerInput))]
-    [RequireComponent(typeof(BicycleController))]
+    [RequireComponent(typeof(WcBicycleController))]
     public class GamepadBikeController : MonoBehaviour
     {
         public bool useSpeed;
@@ -16,53 +17,39 @@ namespace Controller
         public float leanMultiplier = 20f;
         public float steerMultiplier = 15f;
 
-        private float speed;
+        private float acceleration;
         private float lean;
         private float steer;
-        private BicycleController bicycleController;
-        
+        private WcBicycleController wcBicycleController;
+
         private void Start()
         {
-            bicycleController = GetComponent<BicycleController>();
+            wcBicycleController = GetComponent<WcBicycleController>();
         }
-    
+
         private void Update()
         {
-            if(useSteer) bicycleController.CurrentSteerAngle = steer;
-            if(useLean) bicycleController.CurrentLeaningAngle = lean;
-            if (useSpeed)
-            {
-                if (speed < 0)
-                {
-                    bicycleController.CurrentMotorTorque = 0;
-                    bicycleController.CurrentBrakeTorque = -speed;
-                }
-                else
-                {
-                    bicycleController.CurrentBrakeTorque = 0;
-                    bicycleController.CurrentMotorTorque = speed;
-                }
-            }
+            if (useSteer) wcBicycleController.CurrentSteerAngle = steer;
+            if (useLean) wcBicycleController.CurrentLeaningAngle = lean;
+            if (useSpeed) wcBicycleController.CurrentSpeed += acceleration;
         }
-    
+
         [UsedImplicitly]
         public void OnSpeed(InputValue value)
         {
-            speed = value.Get<float>() * speedMultiplier;
+            acceleration = value.Get<float>() * speedMultiplier;
         }
-    
+
         [UsedImplicitly]
         public void OnLean(InputValue value)
         {
             lean = value.Get<float>() * leanMultiplier;
         }
-    
+
         [UsedImplicitly]
         public void OnSteer(InputValue value)
         {
             steer = value.Get<float>() * steerMultiplier;
         }
-
-
     }
-}
+}

+ 0 - 3
Assets/Scripts/Controller/IBicycleController.cs

@@ -1,3 +0,0 @@
-public interface IBicycleController
-{
-}

+ 19 - 26
Assets/Scripts/Controller/KeyboardBikeController.cs

@@ -1,28 +1,25 @@
-using UnityEngine;
+using Controller.Bicycle;
+using UnityEngine;
 
 namespace Controller
 {
     public class KeyboardBikeController : MonoBehaviour
     {
-        private BicycleController bicycleController;
+        private WcBicycleController wcBicycleController;
 
         public bool steer = true;
         public bool lean = true;
         public bool accelerate = true;
 
-        public float torqueIncreasePerSecond = 10f;
-        public float brakeTorqueIncreasePerSecond = 20f;
+        public float speedIncreasePerSecond = 3f;
+        public float speedDecreasePerSecond = 0.5f;
+        public float brakeIncreasePerSecond = 5f;
         public float leaningAngleIncreasePerSecond = 2f;
         public float steeringAngleIncreasePerSecond = 2.5f;
 
-        public float maxMotorTorque = 400f;
-        public float maxBrakeTorque = 600f;
-        public float maxLeaningAngle = 35f;
-        public float maxSteeringAngle = 70f;
-
         private void Start()
         {
-            bicycleController = GetComponent<BicycleController>();
+            wcBicycleController = GetComponent<WcBicycleController>();
         }
 
         private void Update()
@@ -31,22 +28,18 @@ namespace Controller
             {
                 if (Input.GetKey(KeyCode.T))
                 {
-                    bicycleController.CurrentBrakeTorque = 0f;
-                    bicycleController.CurrentMotorTorque += torqueIncreasePerSecond * Time.deltaTime;
+                    wcBicycleController.CurrentSpeed += speedIncreasePerSecond * Time.deltaTime;
                 }
-                else if (Input.GetKeyUp(KeyCode.T))
+                else if (wcBicycleController.CurrentSpeed > 0)
                 {
-                    bicycleController.CurrentMotorTorque = 0f;
+                    wcBicycleController.CurrentSpeed = Mathf.Max(0,
+                        wcBicycleController.CurrentSpeed - speedDecreasePerSecond * Time.deltaTime);
                 }
 
                 if (Input.GetKey(KeyCode.G))
                 {
-                    bicycleController.CurrentMotorTorque = 0f;
-                    bicycleController.CurrentBrakeTorque += brakeTorqueIncreasePerSecond * Time.deltaTime;
-                }
-                else if (Input.GetKeyUp(KeyCode.G))
-                {
-                    bicycleController.CurrentBrakeTorque = 0f;
+                    wcBicycleController.CurrentSpeed = Mathf.Max(0,
+                        wcBicycleController.CurrentSpeed - brakeIncreasePerSecond * Time.deltaTime);
                 }
             }
 
@@ -54,17 +47,17 @@ namespace Controller
             {
                 if (Input.GetKey(KeyCode.F))
                 {
-                    bicycleController.CurrentSteerAngle -= steeringAngleIncreasePerSecond * Time.deltaTime;
+                    wcBicycleController.CurrentSteerAngle -= steeringAngleIncreasePerSecond * Time.deltaTime;
                 }
 
                 if (Input.GetKey(KeyCode.H))
                 {
-                    bicycleController.CurrentSteerAngle += steeringAngleIncreasePerSecond * Time.deltaTime;
+                    wcBicycleController.CurrentSteerAngle += steeringAngleIncreasePerSecond * Time.deltaTime;
                 }
 
                 if (Input.GetKeyUp(KeyCode.F) || Input.GetKeyUp(KeyCode.H))
                 {
-                    bicycleController.CurrentSteerAngle = 0f;
+                    wcBicycleController.CurrentSteerAngle = 0f;
                 }
             }
 
@@ -72,17 +65,17 @@ namespace Controller
             {
                 if (Input.GetKey(KeyCode.R))
                 {
-                    bicycleController.CurrentLeaningAngle -= leaningAngleIncreasePerSecond * Time.deltaTime;
+                    wcBicycleController.CurrentLeaningAngle -= leaningAngleIncreasePerSecond * Time.deltaTime;
                 }
 
                 if (Input.GetKey(KeyCode.Z))
                 {
-                    bicycleController.CurrentLeaningAngle += leaningAngleIncreasePerSecond * Time.deltaTime;
+                    wcBicycleController.CurrentLeaningAngle += leaningAngleIncreasePerSecond * Time.deltaTime;
                 }
 
                 if (Input.GetKeyUp(KeyCode.R) || Input.GetKeyUp(KeyCode.Z))
                 {
-                    bicycleController.CurrentLeaningAngle = 0f;
+                    wcBicycleController.CurrentLeaningAngle = 0f;
                 }
             }
         }

+ 10 - 30
Assets/Scripts/Controller/SensorBikeController.cs

@@ -1,4 +1,5 @@
 using System;
+using Controller.Bicycle;
 using Sensors;
 using Sensors.ANT;
 using Sensors.Polar;
@@ -26,8 +27,6 @@ namespace Controller
 
     public class SensorBikeController : MonoBehaviour
     {
-        public float maxSpeed = 40f;
-        public float maxMotorTorque = 1000;
         public PolarRotationMapping polarRotationMapping;
 
         public SpeedSensorConfig speedSensorConfig;
@@ -38,7 +37,7 @@ namespace Controller
         public bool accelerate = true;
         public bool lean = true;
 
-        private BicycleController bicycleController;
+        private WcBicycleController wcBicycleController;
         private BikeSensorData sensorData;
         private float leanFactor;
         private bool isFrontWheelTrackerNotNull;
@@ -46,7 +45,7 @@ namespace Controller
         private async void Start()
         {
             isFrontWheelTrackerNotNull = frontWheelTrackerConfig.frontWheelTracker != null;
-            bicycleController = GetComponent<BicycleController>();
+            wcBicycleController = GetComponent<WcBicycleController>();
             sensorData = BikeSensorData.Instance;
             await sensorData.StartListening(polarSensorConfig: polarSensorConfig, speedSensorConfig: speedSensorConfig);
             leanFactor = 90f / (polarRotationMapping.maxRight - polarRotationMapping.center);
@@ -75,47 +74,28 @@ namespace Controller
 
         private void SetSteer()
         {
-            bicycleController.CurrentSteerAngle = frontWheelTrackerConfig.AdjustedRotation.y; //TODO: something a bit smarter
+            wcBicycleController.CurrentSteerAngle = frontWheelTrackerConfig.AdjustedRotation.y; //TODO: something a bit smarter
         }
 
         private void OnDestroy()
         {
-            sensorData.Dispose();
+            sensorData?.Dispose();
         }
 
         private void SetLeaningAngle(PolarSensorData polarData)
         {
             //don't lean while standing / walking to bike
-            if (bicycleController.rigidBody.velocity.magnitude > .5f)
+            if (wcBicycleController.rigidBody.velocity.magnitude > .5f)
             {
-                bicycleController.CurrentLeaningAngle = (polarData.Acc.y - polarRotationMapping.center) * leanFactor;
+                wcBicycleController.CurrentLeaningAngle = (polarData.Acc.y - polarRotationMapping.center) * leanFactor;
             }
         }
 
         private void SetSpeed(SpeedSensorData speedData)
         {
-            var currentSpeed = bicycleController.rigidBody.velocity.magnitude;
-            var speedDif = speedData.Speed - currentSpeed;
-            var ratio = speedDif / maxSpeed;
-            var torque = maxMotorTorque * ratio;
-            if (speedDif >= .1f) // 0.36 km/h
-            {
-                Debug.Log($"SpeedDif = {speedDif} -> applying Torque {torque} (Ratio: {ratio})");
-                bicycleController.CurrentBrakeTorque = 0;
-                bicycleController.CurrentMotorTorque = torque;
-            }
-            else if (speedDif <= -.1f)
-            {
-                Debug.Log($"SpeedDif = {speedDif} -> applying brake Torque {torque} (Ratio: {ratio})");
-                bicycleController.CurrentMotorTorque = 0;
-                bicycleController.CurrentBrakeTorque = -torque;
-            }
-            // without else the speed overshoots a bit, but is way closer to real speed
-            /*else
-        {
-            bicycleController.CurrentMotorTorque = 0;
-            bicycleController.CurrentBrakeTorque = 0;
-        }*/
+            wcBicycleController.CurrentSpeed = speedData.Speed;
+            
+            
         }
     }
 }

+ 5 - 4
Assets/Scripts/Controller/ViveBikeController.cs

@@ -1,4 +1,5 @@
-using UnityEngine;
+using Controller.Bicycle;
+using UnityEngine;
 using Valve.VR;
 
 namespace Controller
@@ -8,19 +9,19 @@ namespace Controller
         public SteamVR_Action_Pose steerPose;
         public float multiplier = 40f;
 
-        private BicycleController bicycleController;
+        private WcBicycleController wcBicycleController;
     
         // Start is called before the first frame update
         void Start()
         {
-            bicycleController = GetComponent<BicycleController>();
+            wcBicycleController = GetComponent<WcBicycleController>();
         }
 
         // Update is called once per frame
         void Update()
         {
             var rot = steerPose.localRotation.y;
-            bicycleController.CurrentSteerAngle = rot * multiplier;
+            wcBicycleController.CurrentSteerAngle = rot * multiplier;
         }
     }
 }

+ 8 - 5
Assets/Scripts/Display/DebugDisplay.cs

@@ -2,6 +2,7 @@
 using System.Net;
 using System.Text;
 using Controller;
+using Controller.Bicycle;
 using Sensors;
 using UnityEngine;
 
@@ -9,7 +10,7 @@ namespace Display
 {
     public class DebugDisplay : MonoBehaviour
     {
-        public BicycleController bicycleController;
+        public WcBicycleController wcBicycleController;
         private BikeSensorData sensorData = BikeSensorData.Instance;
 
         private string ip;
@@ -23,18 +24,20 @@ namespace Display
 
         private void Update()
         {
-            var rbSpeed = bicycleController.rigidBody.velocity.magnitude * 3.6;
+            var rbSpeed = wcBicycleController.rigidBody.velocity.magnitude * 3.6;
             var sensorSpeed = sensorData.SpeedData?.SpeedKmh ?? 0f;
             var sb = new StringBuilder();
-            sb.Append(
-                $"MotorTorque: {bicycleController.CurrentMotorTorque:n2}\nBrakeTorque: {bicycleController.CurrentBrakeTorque:n2}\nSteer: {bicycleController.CurrentSteerAngle}\nLean: {bicycleController.CurrentLeaningAngle:n4}\n");
+            //sb.Append(
+            //    $"MotorTorque: {bicycleController.CurrentMotorTorque:n2}\nBrakeTorque: {bicycleController.CurrentBrakeTorque:n2}\nSteer: {bicycleController.CurrentSteerAngle}\nLean: {bicycleController.CurrentLeaningAngle:n4}\n");
             sb.Append("-----Sensors------\n");
             sb.Append(
                 $"Speed: {(sensorData.SpeedData?.Speed ?? 0f):n4} m/s\nPolar Acc ({(sensorData.PolarData?.Acc.x ?? 0f):n4}; {(sensorData.PolarData?.Acc.y ?? 0f):n4}; {(sensorData.PolarData?.Acc.z ?? 0f):n4}) ");
             sb.Append("\n\n");
             sb.Append($"IP: {ip}\n");
             sb.Append("----BicycleController-----\n");
-            sb.Append($"Actual Speed {rbSpeed:n2} km/h - Dif: {(sensorSpeed - rbSpeed):n2}");
+            sb.Append($"Actual Speed {rbSpeed:n2} km/h - Dif: {(wcBicycleController.CurrentSpeed * 3.6 - rbSpeed):n2}\n");
+            sb.Append(
+                $"Current Speed = {wcBicycleController.CurrentSpeed:n2} m/s ({(wcBicycleController.CurrentSpeed * 3.6):n2} km/h)");
             text = sb.ToString();
         }
 

BIN=BIN
obj/Debug/Assembly-CSharp-Editor.csprojAssemblyReference.cache


BIN=BIN
obj/Debug/Assembly-CSharp.csprojAssemblyReference.cache


BIN=BIN
obj/Debug/SteamVR_Windows_EditorHelper.csprojAssemblyReference.cache


Algúns arquivos non se mostraron porque demasiados arquivos cambiaron neste cambio