# Unity实现汽车前后轮倒车轨迹计算

• 车前后轴距;
• 车宽(左前轮与右前轮距离);
• 当前车轮角度(多数车33.5°);
• 是否要绘制前轮轨迹线;
```///
/// 获取行车轨迹预测index = 0 left轨迹
///
/// 方向盘角度
/// 汽车前后轴距
/// 车宽
/// 点位密度
/// 是否是前轮
/// 轨迹的最大转弯角度
/// 交叉数组,下标为0的是右边线, 下表为1的是左边线
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 trackLeft = new List();
List trackRight = new List();
if (theta == 0)
{
for (float i = 0; i < length; i++)
{
float x = i / length * 5;
if (isFront)
{
x *= 1;
}
else
{
x *= -1;
}
}
}
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;
}```