Skip to content

Commit

Permalink
Remove Aerodynamics:
Browse files Browse the repository at this point in the history
Split into MidAirStabilization, MidAirSteering
Vehicle.cs simplified as a result
  • Loading branch information
Vatsal Ambastha committed Apr 18, 2020
1 parent 9143c40 commit 0373497
Show file tree
Hide file tree
Showing 147 changed files with 4,502 additions and 898 deletions.
58 changes: 0 additions & 58 deletions Assets/Adrenak.Tork/Runtime/Aerodynamics.cs

This file was deleted.

This file was deleted.

150 changes: 75 additions & 75 deletions Assets/Adrenak.Tork/Runtime/Input/AIDriver.cs
Original file line number Diff line number Diff line change
@@ -1,91 +1,91 @@
using UnityEngine;
//using UnityEngine;

namespace Adrenak.Tork {
[RequireComponent(typeof(Steering))]
public class AIDriver : Driver {
public enum Direction {
Forward,
Reverse
}
//namespace Adrenak.Tork {
// [RequireComponent(typeof(Steering))]
// public class AIDriver : Driver {
// public enum Direction {
// Forward,
// Reverse
// }

public Transform destination;
public Direction m_Direction = Direction.Forward;
public float steerDamping = .5f;
// public Transform destination;
// public Direction m_Direction = Direction.Forward;
// public float steerDamping = .5f;

Vehicle m_Vehicle;
Steering m_Steering;
float lastSteer;
// Vehicle m_Vehicle;
// Steering m_Steering;
// float lastSteer;

bool m_IsInTurningCircle;
// bool m_IsInTurningCircle;

void Start() {
m_Vehicle = GetComponent<Vehicle>();
m_Steering = GetComponent<Steering>();
}
// void Start() {
// m_Vehicle = GetComponent<Vehicle>();
// m_Steering = GetComponent<Steering>();
// }

public bool isBehind;
public bool isInCircle;
// public bool isBehind;
// public bool isInCircle;

public override VehicleInput GetInput() {
float lastSteer = 0, newSteer = 0, damping = 0;
// public override VehicleInput GetInput() {
// float lastSteer = 0, newSteer = 0, damping = 0;

var towards = destination.position - transform.position;
var locTowards = transform.InverseTransformDirection(towards);
var angle = Vector3.Angle(transform.forward, towards) * Mathf.Sign(locTowards.x);
bool isTargetOnRight = Mathf.Sign(locTowards.x) > 0;
isBehind = Vector3.Dot(towards, transform.forward) < 0;
// var towards = destination.position - transform.position;
// var locTowards = transform.InverseTransformDirection(towards);
// var angle = Vector3.Angle(transform.forward, towards) * Mathf.Sign(locTowards.x);
// bool isTargetOnRight = Mathf.Sign(locTowards.x) > 0;
// isBehind = Vector3.Dot(towards, transform.forward) < 0;

// Get radii at the maximum steering angle
// 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
);
var avgRadius = (radii[0, 0] + radii[0, 1] + radii[1, 0] + radii[1, 1]) / 4;
avgRadius = Mathf.Abs(avgRadius);
// // Get radii at the maximum steering angle
// // 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
// );
// 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 localPivot = transform.InverseTransformPoint(pivot);
var isPivotOnRight = Mathf.Sign(localPivot.x) > 0;
// If the target and pivot are on opposite sides of the car
// we move the pivot along the local X axis so we can do a valid comparision
if (isPivotOnRight != isTargetOnRight)
localPivot.x *= -1;
pivot = transform.TransformPoint(localPivot);
isInCircle = (destination.position - pivot).magnitude < avgRadius;
// // Find out if the target is inside the turning circle
// 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
// // we move the pivot along the local X axis so we can do a valid comparision
// if (isPivotOnRight != isTargetOnRight)
// localPivot.x *= -1;
// pivot = transform.TransformPoint(localPivot);
// isInCircle = (destination.position - pivot).magnitude < avgRadius;

switch (m_Direction) {
case Direction.Forward:
Debug.DrawLine(transform.position, pivot, Color.green);
p_Input.acceleration = 1;
p_Input.brake = 0;
// switch (m_Direction) {
// case Direction.Forward:
// Debug.DrawLine(transform.position, pivot, Color.green);
// p_Input.acceleration = 1;
// p_Input.brake = 0;

lastSteer = p_Input.steering;
newSteer = Mathf.Clamp(angle / m_Steering.range, -1, 1);
damping = (newSteer - lastSteer) * steerDamping;
p_Input.steering = newSteer + damping;
// lastSteer = p_Input.steering;
// newSteer = Mathf.Clamp(angle / m_Steering.range, -1, 1);
// damping = (newSteer - lastSteer) * steerDamping;
// p_Input.steering = newSteer + damping;

if (isBehind && isInCircle)
m_Direction = Direction.Reverse;
break;
case Direction.Reverse:
Debug.DrawLine(transform.position, pivot, Color.red);
p_Input.acceleration = -1;
p_Input.brake = 0;
// if (isBehind && isInCircle)
// m_Direction = Direction.Reverse;
// break;
// case Direction.Reverse:
// Debug.DrawLine(transform.position, pivot, Color.red);
// p_Input.acceleration = -1;
// p_Input.brake = 0;

lastSteer = p_Input.steering;
newSteer = Mathf.Clamp(angle / m_Steering.range, -1, 1);
damping = (newSteer - lastSteer) * steerDamping;
p_Input.steering = newSteer + damping;
// lastSteer = p_Input.steering;
// newSteer = Mathf.Clamp(angle / m_Steering.range, -1, 1);
// damping = (newSteer - lastSteer) * steerDamping;
// p_Input.steering = newSteer + damping;

if (!isBehind && !isInCircle)
m_Direction = Direction.Forward;
break;
}
// if (!isBehind && !isInCircle)
// m_Direction = Direction.Forward;
// break;
// }

return p_Input;
}
}
}
// return p_Input;
// }
// }
//}
2 changes: 1 addition & 1 deletion Assets/Adrenak.Tork/Runtime/Input/Driver.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using UnityEngine;

namespace Adrenak.Tork {
public abstract class Driver : MonoBehaviour {
public abstract class VehicleDriver : MonoBehaviour {
protected VehicleInput p_Input = new VehicleInput();
public abstract VehicleInput GetInput();
}
Expand Down
2 changes: 1 addition & 1 deletion Assets/Adrenak.Tork/Runtime/Input/KeyboardDriver.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using UnityEngine;

namespace Adrenak.Tork{
public class KeyboardDriver : Driver {
public class KeyboardDriver : VehicleDriver {
public const string k_SteeringAxisName = "Horizontal";
public const string k_AccelerateAxisName = "Vertical";
public const string k_BrakeAxisName = "Jump";
Expand Down

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

File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine;

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

Steering steering;
new Rigidbody rigidbody;
[SerializeField] Steering steering;
[SerializeField] new Rigidbody rigidbody;

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

public float GetMaxSteerAtSpeed(float speed) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using UnityEngine;

namespace Adrenak.Tork {
[RequireComponent(typeof(Ackermann))]
public class Steering : MonoBehaviour {
public float range = 35;
public float value; // 0..1
Expand Down
25 changes: 25 additions & 0 deletions Assets/Adrenak.Tork/Runtime/Mechanics/Vehicle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using UnityEngine;

namespace Adrenak.Tork {
public class Vehicle : MonoBehaviour {
[SerializeField] VehicleDriver m_Player;

public Steering m_Steering;
public Motor m_Motor;
public Brakes m_Brake;

public void SetPlayer(VehicleDriver player) {
m_Player = player;
}

void Update() {
if (m_Player == null) return;

var input = m_Player.GetInput();

m_Steering.value = input.steering;
m_Motor.value = input.acceleration;
m_Brake.value = input.brake;
}
}
}
File renamed without changes.

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

File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using UnityEngine;

namespace Adrenak.Tork {
[RequireComponent(typeof(Ackermann))]
public class Brakes : MonoBehaviour {
public Ackermann ackermann;

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
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;
[SerializeField] new Rigidbody rigidbody;

void Update() {
m_Aerodynamics.downForce = GetDownForceAtSpeed(rigidbody.velocity.magnitude);
var downForce = GetDownForceAtSpeed(rigidbody.velocity.magnitude * 3.6f);
rigidbody.AddForce(-Vector3.up * downForce);
}

public float GetDownForceAtSpeed(float speed) {
Expand Down
30 changes: 30 additions & 0 deletions Assets/Adrenak.Tork/Runtime/Physics/MidAirStabilization.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.Linq;
using System.Collections.Generic;
using UnityEngine;

namespace Adrenak.Tork{
public class MidAirStabilization : MonoBehaviour {
[Header("Mid Air Stabilization")]
public float stabilizationTorque = 15000;

public Rigidbody m_Rigidbody;
public Wheel[] m_Wheels;

void FixedUpdate() {
Stabilize();
}

void Stabilize() {
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;
Vector3 wsUp = new Vector3(0.0f, 1.0f, 0.0f);
Vector3 axis = Vector3.Cross(locUp, wsUp);
float force = stabilizationTorque;

m_Rigidbody.AddTorque(axis * force);
}
}
}
Loading

0 comments on commit 0373497

Please sign in to comment.