怎么用Unity计算出汽车前后轮倒车轨迹

发布时间:2021-09-13 18:50:08 作者:chen
来源:亿速云 阅读:342

这篇文章主要介绍“怎么用Unity计算出汽车前后轮倒车轨迹”,在日常操作中,相信很多人在怎么用Unity计算出汽车前后轮倒车轨迹问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”怎么用Unity计算出汽车前后轮倒车轨迹”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

汽车前后轮倒车轨迹计算附C#源码(Unity),供大家参考,具体内容如下

原理很简单, 都是高中的几何数学部分

怎么用Unity计算出汽车前后轮倒车轨迹

需要的参数有:

///<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计算出汽车前后轮倒车轨迹”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

推荐阅读:
  1. [unity3d]汽车的开关门动画播放
  2. 转职--汽车诊断-CAN

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

unity

上一篇:Docker的基本控制命令总结

下一篇:如何使用php foreach修改值

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》