这篇文章主要为大家详细介绍了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实现汽车前后轮倒车轨迹计算
基础教程推荐
猜你喜欢
- 一个读写csv文件的C#类 2022-11-06
- C# windows语音识别与朗读实例 2023-04-27
- linux – 如何在Debian Jessie中安装dotnet core sdk 2023-09-26
- C# 调用WebService的方法 2023-03-09
- C#控制台实现飞行棋小游戏 2023-04-22
- winform把Office转成PDF文件 2023-06-14
- C# List实现行转列的通用方案 2022-11-02
- ZooKeeper的安装及部署教程 2023-01-22
- C#类和结构详解 2023-05-30
- unity实现动态排行榜 2023-04-27