Browse Source

WIP: Rotation based on CustomWheelCollider

Marcel Zickler 3 years ago
parent
commit
1d98181742

+ 1 - 0
Assembly-CSharp-Editor.csproj

@@ -58,6 +58,7 @@
     </Reference>
   </ItemGroup>
   <ItemGroup>
+     <Compile Include="Assets\Editor\CustomWheelColliderEditor.cs" />
      <Compile Include="Assets\Editor\FrontWheelTrackerEditor.cs" />
      <Compile Include="Assets\TutorialInfo\Scripts\Editor\ReadmeEditor.cs" />
  <Reference Include="UnityEditor.TestRunner">

+ 4 - 2
Assembly-CSharp.csproj

@@ -250,16 +250,16 @@
      <Compile Include="Assets\AdvancedAnt\Scripts\PowerMeterDisplay.cs" />
      <Compile Include="Assets\AdvancedAnt\Scripts\SpeedCadenceDisplay.cs" />
      <Compile Include="Assets\AdvancedAnt\Scripts\SpeedDisplay.cs" />
-     <Compile Include="Assets\Axis.cs" />
-     <Compile Include="Assets\ColliderAddSlopeAdjustment.cs" />
      <Compile Include="Assets\InputActions\InputMaster.cs" />
      <Compile Include="Assets\Plotting\DebugPlot.cs" />
      <Compile Include="Assets\Plotting\PlotFileWriter.cs" />
      <Compile Include="Assets\Scripts\Animation\BikeAnimation.cs" />
      <Compile Include="Assets\Scripts\Animation\Rotatable.cs" />
+     <Compile Include="Assets\Scripts\Axis.cs" />
      <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\ColliderAddSlopeAdjustment.cs" />
      <Compile Include="Assets\Scripts\Controller\Bicycle\BicycleControllerBaseBehaviour.cs" />
      <Compile Include="Assets\Scripts\Controller\Bicycle\DummyBicycleController.cs" />
      <Compile Include="Assets\Scripts\Controller\Bicycle\IBicycleController.cs" />
@@ -270,6 +270,8 @@
      <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\CustomWheelCollider.cs" />
+     <Compile Include="Assets\Scripts\CustomWheelColliderSlopeAdjustment.cs" />
      <Compile Include="Assets\Scripts\Display\BikeDataDisplay.cs" />
      <Compile Include="Assets\Scripts\Display\DebugDisplay.cs" />
      <Compile Include="Assets\Scripts\Display\RbDebugDisplay.cs" />

+ 29 - 0
Assets/Editor/CustomWheelColliderEditor.cs

@@ -0,0 +1,29 @@
+using UnityEditor;
+using UnityEngine;
+
+
+[CustomEditor(typeof(CustomWheelCollider))]
+// ReSharper disable once CheckNamespace
+public class CustomWheelColliderEditor : Editor
+{
+    private CustomWheelCollider c;
+
+    public void OnSceneGUI()
+    {
+        c = target as CustomWheelCollider;
+        if (c == null) return;
+        Handles.color = Color.magenta;
+        var transform = c.transform;
+        var position = transform.position;
+        Handles.DrawWireDisc(position
+            , transform.right // normal
+            , c.radius); // radius
+        var directions = c.CalculateRayDirections();
+        foreach (var d in directions)
+        {
+            var direction = transform.TransformDirection(d);
+            Handles.DrawLine(position, position + direction * c.radius);
+        }
+       
+    }
+}

+ 3 - 0
Assets/Editor/CustomWheelColliderEditor.cs.meta

@@ -0,0 +1,3 @@
+fileFormatVersion: 2
+guid: 8877be40d9b94aed9070f8a0622c077f
+timeCreated: 1607766590

+ 64 - 3
Assets/Scenes/MainScene.unity

@@ -241603,8 +241603,8 @@ SphereCollider:
   m_IsTrigger: 0
   m_Enabled: 1
   serializedVersion: 2
-  m_Radius: 1.0988452
-  m_Center: {x: 0, y: 0.008054733, z: 0}
+  m_Radius: 1.07
+  m_Center: {x: 0, y: 0.025737524, z: 0}
 --- !u!114 &1689915701
 MonoBehaviour:
   m_ObjectHideFlags: 0
@@ -241612,7 +241612,7 @@ MonoBehaviour:
   m_PrefabInstance: {fileID: 0}
   m_PrefabAsset: {fileID: 0}
   m_GameObject: {fileID: 1689915694}
-  m_Enabled: 1
+  m_Enabled: 0
   m_EditorHideFlags: 0
   m_Script: {fileID: 11500000, guid: 64051406810b5a447be2c489e1f47281, type: 3}
   m_Name: 
@@ -241621,6 +241621,22 @@ MonoBehaviour:
   steerTransformForward: 2
   backwardIsForward: 1
   collisionLayer: 11
+--- !u!114 &1689915702
+MonoBehaviour:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  m_GameObject: {fileID: 1689915694}
+  m_Enabled: 0
+  m_EditorHideFlags: 0
+  m_Script: {fileID: 11500000, guid: 4eb59bc0bf1db89469a465ebea232d16, type: 3}
+  m_Name: 
+  m_EditorClassIdentifier: 
+  frontWheel: {fileID: 1788657551}
+  rearWheel: {fileID: 1877071838}
+  timeDifferenceTolerance: 0.1
+  dif: {x: 0, y: 0, z: 0}
 --- !u!1001 &1690811636
 PrefabInstance:
   m_ObjectHideFlags: 0
@@ -255576,6 +255592,21 @@ GameObject:
     type: 3}
   m_PrefabInstance: {fileID: 1929752190}
   m_PrefabAsset: {fileID: 0}
+--- !u!114 &1788657551
+MonoBehaviour:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  m_GameObject: {fileID: 1788657547}
+  m_Enabled: 1
+  m_EditorHideFlags: 0
+  m_Script: {fileID: 11500000, guid: 84ffd0d37e9640d4895a42aba5ef88d9, type: 3}
+  m_Name: 
+  m_EditorClassIdentifier: 
+  radius: 0.325
+  numberOfRays: 12
+  collisionLayer: 11
 --- !u!1001 &1788701980
 PrefabInstance:
   m_ObjectHideFlags: 0
@@ -269527,6 +269558,21 @@ GameObject:
     type: 3}
   m_PrefabInstance: {fileID: 1929752190}
   m_PrefabAsset: {fileID: 0}
+--- !u!114 &1877071838
+MonoBehaviour:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  m_GameObject: {fileID: 1877071834}
+  m_Enabled: 1
+  m_EditorHideFlags: 0
+  m_Script: {fileID: 11500000, guid: 84ffd0d37e9640d4895a42aba5ef88d9, type: 3}
+  m_Name: 
+  m_EditorClassIdentifier: 
+  radius: 0.325
+  numberOfRays: 12
+  collisionLayer: 11
 --- !u!1001 &1877335240
 PrefabInstance:
   m_ObjectHideFlags: 0
@@ -276990,6 +277036,11 @@ PrefabInstance:
       propertyPath: m_LocalEulerAnglesHint.z
       value: 180
       objectReference: {fileID: 0}
+    - target: {fileID: 4988978072139883316, guid: 3b07d6594636ab04d9c1fac04c1cde7e,
+        type: 3}
+      propertyPath: m_LocalPosition.y
+      value: -0.3948611
+      objectReference: {fileID: 0}
     - target: {fileID: 4988978072291817853, guid: 3b07d6594636ab04d9c1fac04c1cde7e,
         type: 3}
       propertyPath: m_LocalRotation.x
@@ -277106,6 +277157,16 @@ PrefabInstance:
       propertyPath: m_LocalEulerAnglesHint.y
       value: 0
       objectReference: {fileID: 0}
+    - target: {fileID: 4988978073311274402, guid: 3b07d6594636ab04d9c1fac04c1cde7e,
+        type: 3}
+      propertyPath: m_LocalRotation.x
+      value: 0
+      objectReference: {fileID: 0}
+    - target: {fileID: 4988978073311274402, guid: 3b07d6594636ab04d9c1fac04c1cde7e,
+        type: 3}
+      propertyPath: m_LocalEulerAnglesHint.x
+      value: 0
+      objectReference: {fileID: 0}
     - target: {fileID: 4988978073311274404, guid: 3b07d6594636ab04d9c1fac04c1cde7e,
         type: 3}
       propertyPath: frontWheel.wheel

+ 0 - 0
Assets/Axis.cs → Assets/Scripts/Axis.cs


+ 0 - 0
Assets/Axis.cs.meta → Assets/Scripts/Axis.cs.meta


+ 0 - 0
Assets/ColliderAddSlopeAdjustment.cs → Assets/Scripts/ColliderAddSlopeAdjustment.cs


+ 0 - 0
Assets/ColliderAddSlopeAdjustment.cs.meta → Assets/Scripts/ColliderAddSlopeAdjustment.cs.meta


+ 97 - 0
Assets/Scripts/CustomWheelCollider.cs

@@ -0,0 +1,97 @@
+using System;
+using UnityEngine;
+
+public struct HitInfo
+{
+    public readonly RaycastHit Hit;
+    public readonly float Timestamp;
+
+    public HitInfo(float timestamp, RaycastHit hit)
+    {
+        this.Timestamp = timestamp;
+        this.Hit = hit;
+    }
+}
+
+public class CustomWheelCollider : MonoBehaviour
+{
+    public float radius;
+    public int numberOfRays;
+
+    public int collisionLayer = 11;
+    //[Range(10f,90f)]
+    //public float maxDetectableAngle = 45f;
+
+    private Transform t;
+    private Vector3[] rayDirections;
+
+    public HitInfo? CurrentHit { private set; get; } = null;
+
+    private void Start()
+    {
+        t = transform;
+        rayDirections = CalculateRayDirections();
+    }
+
+    private void FixedUpdate()
+    {
+        for (var i = 0; i < numberOfRays; i++)
+        {
+            CastRay(i);
+        }
+    }
+
+    public Vector3[] CalculateRayDirections()
+    {
+        //var startAngle = 90f - maxDetectableAngle;
+        //var stopAngle = 90f + maxDetectableAngle;
+
+        var anglePerRot = 360f / numberOfRays;
+        var directions = new Vector3[numberOfRays];
+
+        for (var i = 0; i < numberOfRays; i++)
+        {
+            directions[i] = CalculateRayDirection(i * anglePerRot * Mathf.Deg2Rad);
+        }
+
+        return directions;
+    }
+
+    private Vector3 CalculateRayDirection(float angle)
+    {
+        //(1,0,0) rotated by angle
+        var x = 1f;
+        var y = 0f;
+        return new Vector3(0, Mathf.Sin(angle) * x + Mathf.Cos(angle) * y, Mathf.Cos(angle) * x - Mathf.Sin(angle) * y);
+    }
+
+    /*private Vector3 RotateOnYZAxis(Vector3 vector, float angle)
+    {
+        var y = vector.y;
+        var z = vector.z;
+        return new Vector3(0, Mathf.Sin(angle) * y + Mathf.Cos(angle) * z, Mathf.Cos(angle) * y - Mathf.Sin(angle) * z);
+    }*/
+
+
+    private void CastRay(int index)
+    {
+        var direction = t.TransformDirection(rayDirections[index]);
+        var layerMask = 1 << collisionLayer;
+        RaycastHit hit;
+        //var direction = t.forward;
+        //direction = new Vector3(direction.x, direction.z * Mathf.Sin(angle) + direction.y * Mathf.Cos(angle),
+        //    direction.z * Mathf.Cos(angle) - direction.y * Mathf.Sin(angle));
+
+        if (Physics.Raycast(t.position, direction, out hit, radius, layerMask))
+        {
+            Debug.DrawRay(t.position, direction * hit.distance, Color.yellow);
+            //Debug.Log("Did Hit: ");
+            CurrentHit = new HitInfo(Time.fixedTime, hit);
+        }
+        else
+        {
+            Debug.DrawRay(t.position, direction * radius, Color.white);
+            //Debug.Log("Did not Hit");
+        }
+    }
+}

+ 3 - 0
Assets/Scripts/CustomWheelCollider.cs.meta

@@ -0,0 +1,3 @@
+fileFormatVersion: 2
+guid: 84ffd0d37e9640d4895a42aba5ef88d9
+timeCreated: 1607698508

+ 40 - 0
Assets/Scripts/CustomWheelColliderSlopeAdjustment.cs

@@ -0,0 +1,40 @@
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+
+public class CustomWheelColliderSlopeAdjustment : MonoBehaviour
+{
+    public CustomWheelCollider frontWheel;
+    public CustomWheelCollider rearWheel;
+    public float timeDifferenceTolerance = 0.1f;
+
+    public Vector3 dif; //TODO: remove
+
+    private Transform t;
+    
+    private void Start()
+    {
+        t = transform;
+    }
+
+    private void FixedUpdate()
+    {
+        if (frontWheel.CurrentHit == null || rearWheel.CurrentHit == null) return;
+            
+        var fwHit = frontWheel.CurrentHit.Value;
+        var rwHit = rearWheel.CurrentHit.Value;
+
+        if (fwHit.Timestamp - rwHit.Timestamp <= timeDifferenceTolerance)
+        {
+            UpdateTransformRotation(fwHit, rwHit);
+        }
+    }
+
+    private void UpdateTransformRotation(HitInfo fwHit, HitInfo rwHit)
+    {
+        dif = t.TransformPoint(fwHit.Hit.point) - t.TransformPoint(rwHit.Hit.point);
+        var angle = -Mathf.Atan(dif.y / dif.z) * Mathf.Rad2Deg; //TODO: is it that easy?
+        t.localRotation = Quaternion.Euler(t.localRotation.eulerAngles + Vector3.right * angle);
+    }
+}

+ 11 - 0
Assets/Scripts/CustomWheelColliderSlopeAdjustment.cs.meta

@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: 4eb59bc0bf1db89469a465ebea232d16
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 1 - 1
ProjectSettings/QualitySettings.asset

@@ -95,7 +95,7 @@ QualitySettings:
     skinWeights: 2
     textureQuality: 0
     anisotropicTextures: 1
-    antiAliasing: 0
+    antiAliasing: 2
     softParticles: 0
     softVegetation: 1
     realtimeReflectionProbes: 1

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


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