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; } // 右車輪の角度
void Update()
{
CalculateAckermannSteering(steeringAngle, tread, wheelBase);
}
private void CalculateAckermannSteering(float steeringAngle, float tread, float wheelBase)
{
// Ackermann steering geometryの計算
float radianSteeringAngle = Mathf.Deg2Rad * steeringAngle;
float turnRadius = wheelBase / Mathf.Tan(radianSteeringAngle);
float leftTurnRadius = turnRadius + (tread / 2);
float rightTurnRadius = turnRadius - (tread / 2);
// 左右の車輪の角度を設定
leftWheelAngle = Mathf.Rad2Deg * Mathf.Atan(wheelBase / leftTurnRadius);
rightWheelAngle = Mathf.Rad2Deg * Mathf.Atan(wheelBase / rightTurnRadius);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment