Skip to content

Commit

Permalink
Less frequent overtake attempts
Browse files Browse the repository at this point in the history
minor fixes
  • Loading branch information
NomarCub committed Dec 12, 2019
1 parent 52762e4 commit c84fa9d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
24 changes: 16 additions & 8 deletions Assets/Scripts/Cars/CarController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public class CarController : MonoBehaviour
private void Awake()
{
ID = Interlocked.Increment(ref currentID);
maxVelocity = ID % 3 == 0 ? FastestCarVelocity : SlowestCarVelocity;
}

public float maxVelocity = 4.33f;
Expand Down Expand Up @@ -80,6 +79,7 @@ private void Start()
sensorTransform = transform.Find(Strings.distanceSensor);
sensor = gameObject.GetComponentInChildren<DistanceSensor>();
shortestPath = Dijkstra.Instance.CalculateShortestPath(Graph.Instance, source, destination);
maxVelocity = ID % 3 == 0 ? FastestCarVelocity : SlowestCarVelocity;
speedLimit = maxVelocity;
}

Expand Down Expand Up @@ -183,7 +183,12 @@ void steerTowards(Vector3 position)

internal bool TryOvertake(CarController otherCar)
{
if (isInJunction || cantGo() || overtakeInfo.state != OvertakeInfo.State.None)
if (maxVelocity < otherCar.maxVelocity + 4
|| otherCar.velocity < SlowestCarVelocity * 0.5
|| isInJunction
|| cantGo()
|| overtakeInfo.state != OvertakeInfo.State.None
|| overtakeInfo.lastTried + 0.5f > Time.fixedTime)
return false;

Node currentNode = shortestPath[currentNodeIndex];
Expand All @@ -205,7 +210,7 @@ internal bool TryOvertake(CarController otherCar)
var rayFrom = gameObject.transform.position + leftDir * laneDistance;
rayFrom.y = rayHeight;
RaycastHit hit;
float rayDist = Mathf.Min(minOvertakeDist + FastestCarVelocity * (minOvertakeDist / maxVelocity), distanceToJunction);
float rayDist = minOvertakeDist + FastestCarVelocity * (minOvertakeDist / maxVelocity);
Debug.DrawRay(rayFrom - leftDir * 0.1f, forwardDir * rayDist, Color.red, 0.5f, false);
if (Physics.Raycast(rayFrom, forwardDir, out hit, rayDist))
{
Expand All @@ -221,14 +226,15 @@ internal bool TryOvertake(CarController otherCar)

overtakeInfo = new OvertakeInfo()
{
state = OvertakeInfo.State.KeepLeft,
otherCar = otherCar,
car = this,
otherCar = otherCar,
forwardDir = forwardDir,
rightDir = rightDir,
leftDir = leftDir,
rightLane = rightLane,
leftLane = leftLane
leftLane = leftLane,
lastTried = Time.fixedTime,
state = OvertakeInfo.State.KeepLeft
};

return true;
Expand All @@ -249,6 +255,8 @@ public State state
}
public CarController car;
public CarController otherCar;

public float lastTried = 0;
public Vector3 rightLane;
public Vector3 leftLane;
public Vector3 forwardDir;
Expand All @@ -271,7 +279,7 @@ void Overtake()
+ overtakeInfo.forwardDir * 6;
Debug.DrawRay(transform.position, dest - transform.position, Color.blue, 0.5f);
steerTowards(dest);
if (PhysicsCalc.IsBehind(
if (overtakeInfo.otherCar == null || PhysicsCalc.IsBehind(
transform.position,
overtakeInfo.forwardDir,
overtakeInfo.otherCar.transform.position + overtakeInfo.forwardDir * 2))
Expand All @@ -288,7 +296,7 @@ void Overtake()
Debug.DrawRay(transform.position, dest - transform.position, Color.blue, 0.5f);
steerTowards(dest);

if (PhysicsCalc.IsToRight(
if (overtakeInfo.otherCar == null || PhysicsCalc.IsToRight(
transform.position,
overtakeInfo.forwardDir,
overtakeInfo.leftLane + overtakeInfo.rightDir * laneDistance * 0.8f))
Expand Down
11 changes: 4 additions & 7 deletions Assets/Scripts/Cars/DistanceSensor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,12 @@ private void Update()
{
carIsInBrakingDistance = carRigidbody.velocity.magnitude > otherCar.GetComponent<Rigidbody>().velocity.magnitude
|| Vector3.Distance(car.transform.position, otherCar.transform.position) < 4;

var otherController = otherCar.GetComponent<CarController>();
if (carController.maxVelocity > otherController.maxVelocity + 4
&& otherController.maxVelocity > CarController.SlowestCarVelocity * 0.5)
if (carController.TryOvertake(otherController))
{
if (carController.TryOvertake(otherController))
{
otherCar = null;
return;
}
otherCar = null;
return;
}
}

Expand Down
2 changes: 1 addition & 1 deletion Assets/Scripts/PhysicsCalc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public static float SlowDownDistance(float from, float to)
return 0;

float deltaV = from - to;
const float brakeDeceleration = 4.5f;
const float brakeDeceleration = 4f;
float deltaT = deltaV / brakeDeceleration;
return to * deltaT + deltaT * deltaV / 2;
}
Expand Down

0 comments on commit c84fa9d

Please sign in to comment.