Skip to content

Commit

Permalink
Get decent results with Uncharted 4 approach
Browse files Browse the repository at this point in the history
  • Loading branch information
Vatsal Ambastha committed Sep 27, 2020
1 parent 15154cd commit 967ee8e
Showing 1 changed file with 30 additions and 32 deletions.
62 changes: 30 additions & 32 deletions Assets/Tork/Runtime/Core/TorkWheel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ ratio would still hold. Or maybe it won't. Please change the value as you prefer

using System.Net.NetworkInformation;
using UnityEngine;
using UnityEngine.Assertions.Must;

namespace Adrenak.Tork {
public class TorkWheel : MonoBehaviour {
Expand Down Expand Up @@ -132,6 +133,8 @@ public class TorkWheel : MonoBehaviour {
/// </summary>
public Vector3 SuspensionForce { get; private set; }

public float frictionCoeff;

Ray m_Ray;
new Rigidbody rigidbody;
public const float k_ExtraRayLegnth = 1;
Expand Down Expand Up @@ -209,46 +212,41 @@ bool WheelRaycast(float maxDistance, ref RaycastHit hit) {
return false;
}

Vector3 lastVelocity = Vector3.zero;
void CalculateFriction() {
if (!IsGrounded) return;

// Contact basis (can be different from wheel basis)
Vector3 right = transform.right;
right = Vector3.ProjectOnPlane(right, Hit.normal).normalized;
Vector3 forward = transform.forward;
forward = Vector3.ProjectOnPlane(forward, Hit.normal).normalized;

Vector3 Velocity = rigidbody.GetPointVelocity(Hit.point);
Velocity = Vector3.ProjectOnPlane(Velocity, Hit.normal);

Vector3 SideVelocity = Vector3.Project(Velocity, right);

Vector3 momentum = SideVelocity * rigidbody.mass / 4; //* SuspensionForce.magnitude / 9.8f;
Vector3 frictionForce = -momentum * sidewaysGrip;

Vector3 grip = -SideVelocity * SuspensionForce.magnitude * sidewaysGrip;

rigidbody.AddForceAtPosition(frictionForce + grip, Hit.point);

Vector3 forwardVel = forward * Vector3.Dot(Velocity, forward);
float availableFriction = SuspensionForce.magnitude;
if (forwardVel.magnitude > .1f) {
var _rollingFriction = availableFriction * rollingFriction;
rigidbody.AddForceAtPosition(-forwardVel.normalized * _rollingFriction, Hit.point);
availableFriction -= _rollingFriction;
}

//var sideForce = rigidbody.mass / 4 * (sideVel.magnitude - lastSideVelMag)/ Time.fixedDeltaTime;
//lastSideVelMag = sideVel.magnitude;
//var friction = -sideVel.normalized * Mathf.Abs(sideForce);
//rigidbody.AddForceAtPosition(friction, Hit.point);

var motorForce = MotorTorque / radius * engineShaftToWheelRatio;
float latFrictionMultiplier = Mathf.Abs(motorForce) < availableFriction ? 1 : forwardGrip;
var forwardFriction = motorForce * latFrictionMultiplier;
rigidbody.AddForceAtPosition(forward * forwardFriction, Hit.point);

var Vf = Vector3.Project(Velocity, forward).magnitude;
var Vl = Vector3.Project(Velocity, right).magnitude;
var N = SuspensionForce.magnitude;
var f = frictionCoeff;

var Favail = N * f;
var Ff = MotorTorque / radius;
var Fl = Vl / Vf * N;
var Ft = Mathf.Sqrt(Ff * Ff + Fl * Fl);

var Fs = Favail / Ft;
var Fmax = Ft * Fs;

var Flapplied = Fl * Fs;
var Ffmax = Ff * Fs;
var Ffa = Mathf.Clamp(Ff, 0, Ffmax);

rigidbody.AddForceAtPosition(-Vector3.Project(Velocity, right).normalized * Flapplied, Hit.point);
//-Vector3.Project(Velocity, right).normalized * Flapplied, Hit.point);
Debug.Log(Ffa);
rigidbody.AddForceAtPosition(forward * Ffa * engineShaftToWheelRatio, Hit.point);
//-Vector3.Project(Velocity, forward).normalized * Fla, Hit.point);

//if (Fld > 0)
// Debug.Log("Spinning");
//else
// Debug.Log("Breaking");
}
}
}

0 comments on commit 967ee8e

Please sign in to comment.