您好,登錄后才能下訂單哦!
這篇文章主要介紹“怎么用Unity計算出汽車前后輪倒車軌跡”,在日常操作中,相信很多人在怎么用Unity計算出汽車前后輪倒車軌跡問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”怎么用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; }
到此,關于“怎么用Unity計算出汽車前后輪倒車軌跡”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。