Skip to content

Commit

Permalink
Refactor Ackermann, Brakes
Browse files Browse the repository at this point in the history
  • Loading branch information
Vatsal Ambastha committed Apr 18, 2020
1 parent fc6bc19 commit 006a8cb
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 71 deletions.
33 changes: 0 additions & 33 deletions Assets/Adrenak.Tork/Editor/AckermannDebug.cs

This file was deleted.

11 changes: 0 additions & 11 deletions Assets/Adrenak.Tork/Editor/AckermannDebug.cs.meta

This file was deleted.

4 changes: 1 addition & 3 deletions Assets/Adrenak.Tork/Runtime/Ackermann.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@ namespace Adrenak.Tork {
/// An implementation of Ackermann steering mechanism
/// </summary>
public class Ackermann : MonoBehaviour {
public DrawLevel drawLevel;

[SerializeField] Wheel m_FrontRight;
public Wheel FrontRightWheel{ get { return m_FrontRight; } }

[SerializeField] Wheel m_FrontLeft;
public Wheel FrontLeftWheel { get { return m_FrontLeft; } }

[SerializeField] Wheel m_RearRight;
public Wheel ReadRightWheel { get { return m_RearRight ; } }
public Wheel RearRightWheel { get { return m_RearRight ; } }

[SerializeField] Wheel m_RearLeft;
public Wheel RearLeftWheel { get { return m_RearLeft; } }
Expand Down
14 changes: 10 additions & 4 deletions Assets/Adrenak.Tork/Runtime/Aerodynamics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,24 @@ public class Aerodynamics : MonoBehaviour {
[SerializeField] Rigidbody m_Rigidbody;
[SerializeField] Wheel[] m_Wheels;

[Header("Down Force")]
public float downForce = 1000;

[Header("Mid Air Stabilization")]
public float stabilizationTorque = 15000;

[Header("Mid Air Steer")]
public float midAirSteerTorque = 1500;
public float midAirSteerInput;

[Header("Options")]
public bool doApplyDownForce;
public bool doStabilize;
public bool doSteerMidAir;

void FixedUpdate() {
if (doApplyDownForce)
DoApplyDownForce();
ApplyDownForce();

if (doStabilize)
Stabilize();
Expand All @@ -26,14 +32,14 @@ void FixedUpdate() {
SteerMidAir();
}

void DoApplyDownForce() {
void ApplyDownForce() {
if(downForce != 0)
m_Rigidbody.AddForce(-Vector3.up * downForce);
}

void Stabilize() {
var inAir = m_Wheels.Where(x => !x.IsGrounded);
if (inAir.Count() != 4) return;
var inAir = m_Wheels.Where(x => x.IsGrounded);
if (inAir.Count() == 4) return;

// Try to keep vehicle parallel to the ground while jumping
Vector3 locUp = transform.up;
Expand Down
30 changes: 10 additions & 20 deletions Assets/Adrenak.Tork/Runtime/Brakes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,20 @@
namespace Adrenak.Tork {
[RequireComponent(typeof(Ackermann))]
public class Brakes : MonoBehaviour {
public Ackermann ackermann;

[Tooltip("The maximum braking torque that can be applied")]
[SerializeField] float maxTorque = 5000;

[Tooltip("Multiplier to the maxTorque")]
[Tooltip("Multiplier to the maxTorque [0..1]")]
public float value;

public Wheel m_FrontRight;
public Wheel m_FrontLeft;
public Wheel m_RearRight;
public Wheel m_RearLeft;

public Ackermann m_Ackermann { get; private set; }

private void Awake() {
m_Ackermann = GetComponent<Ackermann>();
}

private void FixedUpdate() {
void FixedUpdate() {
float fr, fl, rr, rl;

// If we have Ackerman steering, we apply torque based on the steering radius of each wheel
if (m_Ackermann != null) {
var radii = Ackermann.GetRadii(m_Ackermann.Angle, m_Ackermann.AxleSeparation, m_Ackermann.AxleWidth);
if (ackermann != null) {
var radii = Ackermann.GetRadii(ackermann.Angle, ackermann.AxleSeparation, ackermann.AxleWidth);
var total = radii[0, 0] + radii[1, 0] + radii[0, 1] + radii[1, 1];
fl = radii[0, 0] / total;
fr = radii[1, 0] / total;
Expand All @@ -35,11 +26,10 @@ private void FixedUpdate() {
else
fr = fl = rr = rl = 1;

m_FrontLeft.BrakeTorque = value * maxTorque * fl;
m_FrontRight.BrakeTorque = value * maxTorque * fr;

m_RearLeft.BrakeTorque = value * maxTorque * rl;
m_RearRight.BrakeTorque = value * maxTorque * rr;
ackermann.FrontLeftWheel.BrakeTorque = value * maxTorque * fl;
ackermann.FrontRightWheel.BrakeTorque = value * maxTorque * fr;
ackermann.RearLeftWheel.BrakeTorque = value * maxTorque * rl;
ackermann.RearRightWheel.BrakeTorque = value * maxTorque * rr;
}
}
}

0 comments on commit 006a8cb

Please sign in to comment.