Unity實現汽車前後輪倒車軌跡計算
汽車前後輪倒車軌跡計算附C#源碼(Unity),供大傢參考,具體內容如下
原理很簡單, 都是高中的幾何數學部分
需要的參數有:
- 車前後軸距;
- 車寬(左前輪與右前輪距離);
- 當前車輪角度(多數車33.5°);
- 是否要繪制前輪軌跡線;
///<summary> /// 獲取行車軌跡預測index = 0 left軌跡 /// </summary> /// <param name="steeringAngle">方向盤角度</param> /// <param name="carWheelbase">汽車前後軸距</param> /// <param name="carWidth">車寬</param> /// <param name="length">點位密度</param> /// <param name="isFront">是否是前輪</param> /// <param name="maxAngle">軌跡的最大轉彎角度</param> /// <returns>交叉數組,下標為0的是右邊線, 下表為1的是左邊線</returns> public Vector3[][] GetCarTrack(float steeringAngle, float carWheelbase, float carWidth, float length, bool isFront, float maxAngle = 90f) { float maxSteerAngle = _carControl._vehicleController.steering.maxSteerAngle; float theta = Mathf.Abs(steeringAngle / 180 * Mathf.PI); Vector3[][] track = new Vector3[2][]; List<Vector3> trackLeft = new List<Vector3>(); List<Vector3> trackRight = new List<Vector3>(); if (theta == 0) { for (float i = 0; i < length; i++) { float x = i / length * 5; if (isFront) { x *= 1; trackLeft.Add(new Vector3(x, 0f, carWidth)); trackRight.Add(new Vector3(x, 0f, 0f)); } else { x *= -1; trackLeft.Add(new Vector3(x, 0, carWidth)); trackRight.Add(new Vector3(x, 0, 0)); } } } else { if (isFront) { float r = (carWheelbase / Mathf.Tan(theta) + carWidth / 2) / Mathf.Cos(theta); float rMin = Mathf.Cos(theta) * r - carWidth; float theta1 = Mathf.Atan(carWheelbase / rMin); rMin = rMin / Mathf.Cos(theta1); float rMax = rMin + carWidth; float lineAngle = carWheelbase / (rMax * 2f * Mathf.PI) * 360f; for (int i = 0; i <= length; i++) { if (i / length >= (maxAngle - lineAngle) / maxAngle) { float x = rMin * Mathf.Cos(maxAngle / length * i * Mathf.Deg2Rad); float z = rMin * Mathf.Sin(maxAngle / length * i * Mathf.Deg2Rad); if (x >= carWheelbase) { if (steeringAngle > 0) { trackRight.Add(new Vector3(x - carWheelbase, 0, z - rMin + steeringAngle / maxSteerAngle * (carWidth / 2))); } else { trackRight.Add(new Vector3(x - carWheelbase, 0, -(z - rMin) + carWidth + steeringAngle / maxSteerAngle * (carWidth / 2))); } } x = rMax * Mathf.Cos(maxAngle / length * i * Mathf.Deg2Rad); z = rMax * Mathf.Sin(maxAngle / length * i * Mathf.Deg2Rad); if (x >= carWheelbase) { if (steeringAngle > 0) { trackLeft.Add(new Vector3(x - carWheelbase, 0, z - rMin + steeringAngle / maxSteerAngle * (carWidth / 2))); } else { trackLeft.Add(new Vector3(x - carWheelbase, 0, -(z - rMin) + carWidth + steeringAngle / maxSteerAngle * (carWidth / 2))); } } } } trackRight[trackRight.Count - 1] = Vector3.forward * trackRight[trackRight.Count - 1].z; trackLeft[trackLeft.Count - 1] = Vector3.forward * trackLeft[trackLeft.Count - 1].z; } else { float r = (carWheelbase / Mathf.Tan(theta) + carWidth / 2) / Mathf.Cos(theta); float rMin = Mathf.Cos(theta) * r - carWidth; float rMax = rMin + carWidth; float lineAngle = carWheelbase / (rMin * 2f * Mathf.PI) * 360f; for (int i = 0; i <= length; i++) { if (i / length >= (maxAngle - lineAngle) / maxAngle) { float x = -rMin * Mathf.Cos(maxAngle / length * i * Mathf.Deg2Rad); float z = rMin * Mathf.Sin(maxAngle / length * i * Mathf.Deg2Rad); if (steeringAngle > 0) { trackRight.Add(new Vector3(x, 0, z - rMin)); } else { trackRight.Add(new Vector3(x, 0, -(z - rMin) + carWidth)); } x = -rMax * Mathf.Cos(maxAngle / length * i * Mathf.Deg2Rad); z = rMax * Mathf.Sin(maxAngle / length * i * Mathf.Deg2Rad); if (steeringAngle > 0) { trackLeft.Add(new Vector3(x, 0, z - rMin)); } else { trackLeft.Add(new Vector3(x, 0, -(z - rMin) + carWidth)); } } } trackRight[trackRight.Count - 1] = Vector3.forward * trackRight[trackRight.Count - 1].z; trackLeft[trackLeft.Count - 1] = Vector3.forward * trackLeft[trackLeft.Count - 1].z; } } track[0] = trackLeft.ToArray(); track[1] = trackRight.ToArray(); trackLeft = trackRight = null; return track; }
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。