Browse Source

Calculating Road Slope from RaycastHit

Marcel Zickler 3 years ago
parent
commit
5c85e84b78

+ 2 - 0
Assembly-CSharp.csproj

@@ -250,6 +250,8 @@
      <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" />

+ 6 - 0
Assets/Axis.cs

@@ -0,0 +1,6 @@
+public enum Axis
+{
+    X,
+    Y,
+    Z
+}

+ 3 - 0
Assets/Axis.cs.meta

@@ -0,0 +1,3 @@
+fileFormatVersion: 2
+guid: eaf24827c05944cf97917d9c55833950
+timeCreated: 1607686805

+ 92 - 0
Assets/ColliderAddSlopeAdjustment.cs

@@ -0,0 +1,92 @@
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+
+
+public class ColliderAddSlopeAdjustment : MonoBehaviour
+{
+    [Header("Steering Direction Config")]
+    [Tooltip("A Transform that has an Axis that always shows into steering direction. It is assumed, that this transform is always on the floor!")]
+    public Transform steerTransform;
+    public Axis steerTransformForward = Axis.Z;
+    public bool backwardIsForward = false;
+    public int collisionLayer = 11;
+
+    private const float RAYCAST_DIST = 0.2f;
+    private float rayLength = 10f; //TODO: calculate -> front wheel
+
+
+    // Start is called before the first frame update
+    void Start()
+    {
+        
+    }
+
+    void FixedUpdate()
+    {
+        bool hit = AdjustSlope(); //Check above ground -> ascending
+        if(!hit) AdjustSlope(underground: true); //Check underground -> descending
+    }
+
+    private bool AdjustSlope(bool underground = false)
+    {
+        var hitDistLower = CastRay(underground: underground);
+        var hitDistUpper = CastRay(dist: RAYCAST_DIST, underground:underground);
+
+        if (hitDistLower != null && hitDistUpper != null)
+        {
+            var slope = CalculateHitSlope(hitDistLower.Value, hitDistUpper.Value);
+            Debug.Log($"Hit - Slope = {(underground ? -slope : slope )}");
+            return true;
+        }
+
+        return false;
+    }
+
+    //TODO: does only work for going up! We also need rays underground
+    private float CalculateHitSlope(float hitDistLower, float hitDistUpper)
+    {
+        return Mathf.Atan(RAYCAST_DIST / Mathf.Abs(hitDistUpper - hitDistLower)) * Mathf.Rad2Deg;
+    }
+
+    private float? CastRay(float dist = 0f, bool underground = false)
+    {
+        var layerMask = 1 << collisionLayer;
+        RaycastHit hit;
+        var forward = CalculateForward();
+        var position = steerTransform.position + Vector3.up * (dist - (underground ? 1 : 0)); // TODO: probably needs to be transformed to local axes
+        // Does the ray intersect any objects excluding the player layer
+        if (Physics.Raycast(position,forward , out hit, rayLength, layerMask))
+        {
+            Debug.DrawRay(position, forward * hit.distance, Color.yellow);
+            return hit.distance;
+            //Debug.Log("Did Hit: ");
+        }
+        else
+        {
+            Debug.DrawRay(position, forward * rayLength, Color.white);
+            //Debug.Log("Did not Hit");
+        }
+
+        return null;
+    }
+
+    private Vector3 CalculateForward()
+    {
+        Vector3 forward;
+        switch (steerTransformForward)
+        {
+            case Axis.X:
+                forward = steerTransform.right;
+                break;
+            case Axis.Y:
+                forward = steerTransform.up;
+                break;
+            default:
+                forward = steerTransform.forward;
+                break;
+        }
+
+        return backwardIsForward ? -forward : forward;
+    }
+}

+ 11 - 0
Assets/ColliderAddSlopeAdjustment.cs.meta

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

File diff suppressed because it is too large
+ 681 - 316
Assets/Scenes/MainScene.unity


+ 4 - 10
Assets/Scripts/Animation/BikeAnimation.cs

@@ -8,13 +8,7 @@ using UnityEngine;
 namespace Animation
 {
     #region Helpers
-
-    public enum WheelRotationAxis
-    {
-        X,
-        Y,
-        Z
-    }
+    
 
     public enum WheelRotationDirection
     {
@@ -26,7 +20,7 @@ namespace Animation
     public struct WheelAnimationConfig
     {
         public GameObject wheel;
-        public WheelRotationAxis rotationAxis;
+        public Axis rotationAxis;
         public WheelRotationDirection rotationDirection;
     }
 
@@ -97,11 +91,11 @@ namespace Animation
             var directedAngle = backwards ? -angle : angle;
 
             Vector3 rotateAxis;
-            if (axis == WheelRotationAxis.X)
+            if (axis == Axis.X)
             {
                 rotateAxis = t.right;
             }
-            else if (axis == WheelRotationAxis.Y)
+            else if (axis == Axis.Y)
             {
                 rotateAxis = t.up;
             }

+ 1 - 1
ProjectSettings/QualitySettings.asset

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

+ 4 - 2
ProjectSettings/TagManager.asset

@@ -3,7 +3,9 @@
 --- !u!78 &1
 TagManager:
   serializedVersion: 2
-  tags: []
+  tags:
+  - projectile
+  - FxTemporaire
   layers:
   - Default
   - TransparentFX
@@ -16,7 +18,7 @@ TagManager:
   - VR
   - Bike
   - Post Procesing
-  - 
+  - Street
   - 
   - 
   - 

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


Some files were not shown because too many files changed in this diff