Skip to content

Commit

Permalink
Reduce allocations in RoadCars
Browse files Browse the repository at this point in the history
  • Loading branch information
pzgulyas committed Apr 10, 2023
1 parent 1c91c8f commit 0bb067a
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions Source/RunActivity/Viewer3D/RoadCars.cs
Expand Up @@ -255,13 +255,13 @@ public void Update(ElapsedTime elapsedTime)
}

// Calculate all the distances to items we need to stop at (level crossings, other cars).
var stopDistances = new List<float>();
var stopDistance = float.MaxValue;
for (var crossing = NextCrossingIndex; crossing < crossings.Count; crossing++)
{
if (crossings[crossing].Item.CrossingGroup != null && crossings[crossing].Item.CrossingGroup.HasTrain)
{
// TODO: Stopping distance for level crossings!
stopDistances.Add(crossings[crossing].Distance - RoadCarSpawner.StopDistance);
stopDistance = Math.Min(stopDistance, crossings[crossing].Distance - RoadCarSpawner.StopDistance);
break;
}
}
Expand All @@ -271,13 +271,13 @@ public void Update(ElapsedTime elapsedTime)
if (spawnerIndex > 0)
{
if (!cars[spawnerIndex - 1].CarriesCamera)
stopDistances.Add(cars[spawnerIndex - 1].Travelled - cars[spawnerIndex - 1].Length / 2);
stopDistance = Math.Min(stopDistance, cars[spawnerIndex - 1].Travelled - cars[spawnerIndex - 1].Length / 2);
else
stopDistances.Add(cars[spawnerIndex - 1].Travelled - cars[spawnerIndex - 1].Length * 0.65f - 4 - cars[spawnerIndex - 1].Speed * 0.5f);
}
stopDistance = Math.Min(stopDistance, cars[spawnerIndex - 1].Travelled - cars[spawnerIndex - 1].Length * 0.65f - 4 - cars[spawnerIndex - 1].Speed * 0.5f);
}

// Calculate whether we're too close to the minimum stopping distance (and need to slow down) or going too slowly (and need to speed up).
var stopDistance = stopDistances.Count > 0 ? stopDistances.Min() - Travelled - Length / 2 : float.MaxValue;
stopDistance = stopDistance - Travelled - Length / 2;
var slowingDistance = BrakingFactor * Length;
if (stopDistance < slowingDistance)
Speed = SpeedMax * (float)Math.Sin((Math.PI / 2) * (stopDistance / slowingDistance));
Expand Down

0 comments on commit 0bb067a

Please sign in to comment.