Skip to content

Commit

Permalink
Refactor Motor.cs and move artificial constraints into separate classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Vatsal Ambastha committed Apr 18, 2020
1 parent 006a8cb commit 9143c40
Show file tree
Hide file tree
Showing 13 changed files with 155 additions and 46 deletions.
8 changes: 8 additions & 0 deletions Assets/Adrenak.Tork/Runtime/Decorator.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions Assets/Adrenak.Tork/Runtime/Decorator/DownForceVsSpeed.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Adrenak.Tork {
public class DownForceVsSpeed : MonoBehaviour {
[Tooltip("The down force based on the speed (KMPH)")]
[SerializeField] AnimationCurve curve = AnimationCurve.Linear(0, 0, 250, 2500);

Aerodynamics m_Aerodynamics;
new Rigidbody rigidbody;

void Update() {
m_Aerodynamics.downForce = GetDownForceAtSpeed(rigidbody.velocity.magnitude);
}

public float GetDownForceAtSpeed(float speed) {
return curve.Evaluate(speed);
}
}
}

11 changes: 11 additions & 0 deletions Assets/Adrenak.Tork/Runtime/Decorator/DownForceVsSpeed.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Adrenak.Tork {
public class MaxSteerAngleVsSpeedConstraint : MonoBehaviour {
[Tooltip("The steering angle based on the speed (KMPH)")]
[SerializeField] AnimationCurve curve = AnimationCurve.Linear(0, 35, 250, 5);

Steering steering;
new Rigidbody rigidbody;

void Update() {
steering.range = GetMaxSteerAtSpeed(rigidbody.velocity.magnitude);
}

public float GetMaxSteerAtSpeed(float speed) {
return curve.Evaluate(speed);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using UnityEngine;

namespace Adrenak.Tork{
public class MotorTorqueVsSpeedConstraint : MonoBehaviour {
[Tooltip("The maximum motor torque available based on the speed (KMPH)")]
[SerializeField] AnimationCurve curve = AnimationCurve.Linear(0, 10000, 250, 0);

[SerializeField] Motor motor;
[SerializeField] new Rigidbody rigidbody;

void Update(){
motor.maxTorque = GetMotorTorqueAtSpeed(rigidbody.velocity.magnitude);
}

public float GetMotorTorqueAtSpeed(float speed) {
return curve.Evaluate(speed);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Assets/Adrenak.Tork/Runtime/Input/AIDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ public override VehicleInput GetInput() {
// This gives is the smallest turning radius the car can have
var radii = Ackermann.GetRadii(
m_Vehicle.GetMaxSteerAtSpeed(m_Vehicle.Velocity.magnitude),
m_Steering.Ackermann.AxleSeparation,
m_Steering.Ackermann.AxleWidth
m_Steering.ackermann.AxleSeparation,
m_Steering.ackermann.AxleWidth
);
var avgRadius = (radii[0, 0] + radii[0, 1] + radii[1, 0] + radii[1, 1]) / 4;
avgRadius = Mathf.Abs(avgRadius);

// Find out if the target is inside the turning circle
var pivot = m_Steering.Ackermann.GetPivot();
var pivot = m_Steering.ackermann.GetPivot();
var localPivot = transform.InverseTransformPoint(pivot);
var isPivotOnRight = Mathf.Sign(localPivot.x) > 0;
// If the target and pivot are on opposite sides of the car
Expand Down
33 changes: 11 additions & 22 deletions Assets/Adrenak.Tork/Runtime/Motor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,38 +12,27 @@ public class Motor : MonoBehaviour {

public float m_MaxReverseInput = -.5f;

public Wheel m_FrontRight;
public Wheel m_FrontLeft;
public Wheel m_RearRight;
public Wheel m_RearLeft;
public Ackermann ackermann;

public Ackermann m_Ackermann { get; private set; }

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

private void Update() {
void FixedUpdate() {
ApplyMotorTorque();
}

void ApplyMotorTorque() {
value = Mathf.Clamp(value, m_MaxReverseInput, 1);
float fr, fl, rr, rl;

// If we have Ackerman steering, we apply torque based on the steering radius of each wheel
var radii = Ackermann.GetRadii(m_Ackermann.Angle, m_Ackermann.AxleSeparation, m_Ackermann.AxleWidth);
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;
rl = radii[0, 1] / total;
rr = radii[1, 1] / total;
var fl = radii[0, 0] / total;
var fr = radii[1, 0] / total;
var rl = radii[0, 1] / total;
var rr = radii[1, 1] / total;

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

m_RearLeft.MotorTorque = value * maxTorque * rl;
m_RearRight.MotorTorque = value * maxTorque * rr;
ackermann.FrontLeftWheel.MotorTorque = value * maxTorque * fl;
ackermann.FrontRightWheel.MotorTorque = value * maxTorque * fr;
ackermann.RearLeftWheel.MotorTorque = value * maxTorque * rl;
ackermann.RearRightWheel.MotorTorque = value * maxTorque * rr;
}
}
}
10 changes: 3 additions & 7 deletions Assets/Adrenak.Tork/Runtime/Steering.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,14 @@ public class Steering : MonoBehaviour {
public float value; // 0..1
public float rate = 45;

public Ackermann Ackermann { get; private set; }
public Ackermann ackermann;
float m_CurrAngle;

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

private void Update() {
void Update() {
var destination = value * range;
m_CurrAngle = Mathf.MoveTowards(m_CurrAngle, destination, Time.deltaTime * rate);
m_CurrAngle = Mathf.Clamp(m_CurrAngle, -range, range);
Ackermann.SetAngle(m_CurrAngle);
ackermann.SetAngle(m_CurrAngle);
}
}
}
10 changes: 5 additions & 5 deletions Assets/Adrenak.Tork/Runtime/Vehicle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,16 @@ void Update() {
m_Aerodynamics.midAirSteerInput = input.steering;
}

public float GetMotorTorqueAtSpeed(float speed) {
public float GetDownForceAtSpeed(float speed) {
return m_DownForceVsSpeed.Evaluate(speed);
}

public float GetMotorTorqueAtSpeed(float speed) {
return m_MotorTorqueVsSpeed.Evaluate(speed);
}

public float GetMaxSteerAtSpeed(float speed) {
return m_MaxSteeringAngleVsSpeed.Evaluate(speed);
}

public float GetDownForceAtSpeed(float speed) {
return m_DownForceVsSpeed.Evaluate(speed);
}
}
}
12 changes: 3 additions & 9 deletions Assets/Adrenak.Tork/Samples/Assets/Prefabs/Tocus.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,6 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 53113b735cef849498b153d0a11363e1, type: 3}
m_Name:
m_EditorClassIdentifier:
drawLevel: 1
m_FrontRight: {fileID: 114236843947743228}
m_FrontLeft: {fileID: 114058669265167066}
m_RearRight: {fileID: 114500219053648900}
Expand All @@ -554,12 +553,9 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 91301881b4e44114090ead2d22e0fa15, type: 3}
m_Name:
m_EditorClassIdentifier:
ackermann: {fileID: 0}
maxTorque: 20000
value: 0
m_FrontRight: {fileID: 114236843947743228}
m_FrontLeft: {fileID: 114058669265167066}
m_RearRight: {fileID: 114500219053648900}
m_RearLeft: {fileID: 114578490075378740}
--- !u!114 &114600315714616560
MonoBehaviour:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -600,10 +596,7 @@ MonoBehaviour:
maxTorque: 20000
value: 0
m_MaxReverseInput: -0.5
m_FrontRight: {fileID: 114236843947743228}
m_FrontLeft: {fileID: 114058669265167066}
m_RearRight: {fileID: 114500219053648900}
m_RearLeft: {fileID: 114578490075378740}
ackermann: {fileID: 0}
--- !u!114 &114592843161099890
MonoBehaviour:
m_ObjectHideFlags: 0
Expand All @@ -619,6 +612,7 @@ MonoBehaviour:
range: 0
value: 0
rate: 45
ackermann: {fileID: 0}
--- !u!114 &114436233003325660
MonoBehaviour:
m_ObjectHideFlags: 0
Expand Down
27 changes: 27 additions & 0 deletions Assets/Adrenak.Tork/Samples/Demo-KeyboardDriver/PhysicsDemo.unity
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,21 @@ PrefabInstance:
propertyPath: m_LocalPosition.y
value: -0.24999999
objectReference: {fileID: 0}
- target: {fileID: 114138417106533648, guid: 58b5a3e507c7b68449674674e18cab44,
type: 3}
propertyPath: ackermann
value:
objectReference: {fileID: 1861522226}
- target: {fileID: 114266373984357696, guid: 58b5a3e507c7b68449674674e18cab44,
type: 3}
propertyPath: ackermann
value:
objectReference: {fileID: 1861522226}
- target: {fileID: 114592843161099890, guid: 58b5a3e507c7b68449674674e18cab44,
type: 3}
propertyPath: ackermann
value:
objectReference: {fileID: 1861522226}
m_RemovedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: 58b5a3e507c7b68449674674e18cab44, type: 3}
--- !u!114 &6872752 stripped
Expand Down Expand Up @@ -4031,6 +4046,18 @@ MonoBehaviour:
height: 1.5
rotationDamping: 8
heightDamping: 4
--- !u!114 &1861522226 stripped
MonoBehaviour:
m_CorrespondingSourceObject: {fileID: 114468476234289850, guid: 58b5a3e507c7b68449674674e18cab44,
type: 3}
m_PrefabInstance: {fileID: 6872751}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 53113b735cef849498b153d0a11363e1, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!1 &1977217265
GameObject:
m_ObjectHideFlags: 0
Expand Down

0 comments on commit 9143c40

Please sign in to comment.