-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split into MidAirStabilization, MidAirSteering Vehicle.cs simplified as a result
- Loading branch information
Vatsal Ambastha
committed
Apr 18, 2020
1 parent
9143c40
commit 0373497
Showing
147 changed files
with
4,502 additions
and
898 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
19 changes: 0 additions & 19 deletions
19
Assets/Adrenak.Tork/Runtime/Decorator/MotorTorqueVsSpeedConstraint.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
// } | ||
// } | ||
//} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
Assets/Adrenak.Tork/Runtime/Decorator.meta → Assets/Adrenak.Tork/Runtime/Mechanics.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
12 changes: 5 additions & 7 deletions
12
...corator/MaxSteerAngleVsSpeedConstraint.cs → ...Runtime/Mechanics/MaxSteerAngleVsSpeed.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
1 change: 0 additions & 1 deletion
1
Assets/Adrenak.Tork/Runtime/Steering.cs → ...drenak.Tork/Runtime/Mechanics/Steering.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
Assets/Adrenak.Tork/Runtime/Utils.meta → Assets/Adrenak.Tork/Runtime/Physics.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
1 change: 0 additions & 1 deletion
1
Assets/Adrenak.Tork/Runtime/Brakes.cs → ...ts/Adrenak.Tork/Runtime/Physics/Brakes.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
10 changes: 4 additions & 6 deletions
10
...ork/Runtime/Decorator/DownForceVsSpeed.cs → ....Tork/Runtime/Physics/DownForceVsSpeed.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
30 changes: 30 additions & 0 deletions
30
Assets/Adrenak.Tork/Runtime/Physics/MidAirStabilization.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
Oops, something went wrong.