溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么用Unity計(jì)算出汽車前后輪倒車軌跡

發(fā)布時(shí)間:2021-09-13 18:50:08 來源:億速云 閱讀:345 作者:chen 欄目:開發(fā)技術(shù)

這篇文章主要介紹“怎么用Unity計(jì)算出汽車前后輪倒車軌跡”,在日常操作中,相信很多人在怎么用Unity計(jì)算出汽車前后輪倒車軌跡問題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”怎么用Unity計(jì)算出汽車前后輪倒車軌跡”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!

汽車前后輪倒車軌跡計(jì)算附C#源碼(Unity),供大家參考,具體內(nèi)容如下

原理很簡(jiǎn)單, 都是高中的幾何數(shù)學(xué)部分

怎么用Unity計(jì)算出汽車前后輪倒車軌跡

需要的參數(shù)有:

  • 車前后軸距;

  • 車寬(左前輪與右前輪距離);

  • 當(dāng)前車輪角度(多數(shù)車33.5°);

  • 是否要繪制前輪軌跡線;

///<summary>
/// 獲取行車軌跡預(yù)測(cè)index = 0 left軌跡
/// </summary>
/// <param name="steeringAngle">方向盤角度</param>
/// <param name="carWheelbase">汽車前后軸距</param>
/// <param name="carWidth">車寬</param>
/// <param name="length">點(diǎn)位密度</param>
/// <param name="isFront">是否是前輪</param>
/// <param name="maxAngle">軌跡的最大轉(zhuǎn)彎角度</param>
/// <returns>交叉數(shù)組,下標(biāo)為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;
}

到此,關(guān)于“怎么用Unity計(jì)算出汽車前后輪倒車軌跡”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI