Skip to content

Instantly share code, notes, and snippets.

@chrislo27
Created December 27, 2015 20:52
Show Gist options
  • Save chrislo27/ad3d720b5ea29b588a9f to your computer and use it in GitHub Desktop.
Save chrislo27/ad3d720b5ea29b588a9f to your computer and use it in GitHub Desktop.
Time/distance calculator for Cities: Skylines accounting for acceleration and deceleration
import java.text.MessageFormat;
public class Main {
public static void main(String[] args) {
double totalDistance = 1500;
double maxSpeed = (1 / 3D) * 100;
double acceleration = 8;
double deceleration = 16;
double waitTime = 10;
System.out.println(MessageFormat.format(
"Distance: {0} m\nMax Speed: {1} m/s\nAccel/Decel: {2}/{3} m/s^2", totalDistance,
maxSpeed, acceleration, deceleration));
// t = (vf - vi) / a
// d = (vi)(t) + (1 / 2)at^2
double accTime = maxSpeed / acceleration;
double accDistance = (0.5 * acceleration * Math.pow(accTime, 2));
double decTime = -maxSpeed / -deceleration;
double decDistance = (maxSpeed) * (decTime) + (0.5 * -deceleration * Math.pow(decTime, 2));
double middleDistance = (totalDistance - (accDistance + decDistance));
double middleTime = middleDistance / maxSpeed;
double totalTime = accTime + decTime + middleTime;
double targetGapSeconds = (totalTime - waitTime);
double targetGapDistance = targetGapSeconds * maxSpeed;
System.out.println();
System.out.println("Acceleration time/distance: " + accTime + " s | " + accDistance + " m");
System.out.println("Deceleration time/distance: " + decTime + " s | " + decDistance + " m");
System.out.println("Middle time/distance: " + middleTime + " s | " + middleDistance + " m");
System.out.println("Total time/distance: " + totalTime + " s | " + totalDistance + " m");
System.out.println();
System.out.println("Target gap with wait time of " + waitTime + " s: " + targetGapDistance
+ " m | " + targetGapSeconds + " s");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment