Skip to content

Instantly share code, notes, and snippets.

@comoc
Last active September 29, 2024 13:27
Show Gist options
  • Save comoc/7a3adb6b878084d1530210128c4c027b to your computer and use it in GitHub Desktop.
Save comoc/7a3adb6b878084d1530210128c4c027b to your computer and use it in GitHub Desktop.
Ackermann Steering Geometory for Unity in C#
using UnityEngine;
public class AckermannSteering : MonoBehaviour
{
[SerializeField] private float steeringAngle; // ステアリングの切れ角
[SerializeField] private float tread; // 左右の車輪間の距離 (トレッド)
[SerializeField] private float wheelBase; // 前後の車輪間の距離 (ホイールベース)
public float leftWheelAngle { get; private set; } // 左車輪の角度
public float rightWheelAngle { get; private set; } // 右車輪の角度
public float turningRadius { get; private set; } // 回転半径
public Vector3 turningCenter { get; private set; } // 回転中心位置
void Update()
{
CalculateAckermannSteering(steeringAngle, tread, wheelBase);
}
private void CalculateAckermannSteering(float steeringAngle, float tread, float wheelBase)
{
// Ackermann steering geometryの計算
float radianSteeringAngle = Mathf.Deg2Rad * steeringAngle;
float innerWheelAngle = Mathf.Rad2Deg * Mathf.Atan(wheelBase / (wheelBase / Mathf.Tan(radianSteeringAngle) - tread / 2));
float outerWheelAngle = Mathf.Rad2Deg * Mathf.Atan(wheelBase / (wheelBase / Mathf.Tan(radianSteeringAngle) + tread / 2));
// 左右の車輪の角度を設定
leftWheelAngle = outerWheelAngle;
rightWheelAngle = innerWheelAngle;
// 回転半径の計算
turningRadius = wheelBase / Mathf.Sin(Mathf.Abs(radianSteeringAngle));
// 回転中心位置の計算
float turningCenterX = wheelBase / Mathf.Tan(radianSteeringAngle);
turningCenter = new Vector3(turningCenterX, 0, -wheelBase);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment