#!/usr/bin/perl
#
# Compute position and velocity of two cars
#   which skid to a stop.
#
# MWR 9/3/2019

$debug = 1;

# initial velocity of car A (m/s)
$va0 = 24.58;
# initial velocity of car B (m/s)
$vb0 = 24.58;

# time at which each car hits the brakes (s)
$tbrake_a = 0.5;
$tbrake_b = 0.0;

# acceleration of each car when brakes are applied (m/s^2)
$aa = -3.0;
$ab = -5.0;

# compute time it takes for car A to slide to a halt
$tstop_a = $tbrake_a + (0.0 - ($va0/$aa));
# compute time it takes for car B to slide to a halt
$tstop_b = 0.0 - ($vb0/$ab);

# now, for all times up to tstop_a, compute the position,
#      and velocity of each car.
#      Ignore for now the offset between A and B at the start.
$t_start = 0.0;
$t_end = $tstop_a;
$d_t = 0.02;

$xa = 0.0;
$xb = 0.0;
for ($t = $t_start; $t <= $t_end; $t += $d_t) {

  if ($t < $tbrake_a) {
    $xa = $va0*$t;
    $va = $va0;
  }
  else {
    $tt = $t - $tbrake_a;
    $xa = ($va0*$tbrake_a) + ( $va0*$tt + 0.5*$aa*($tt**2.0) );
    $va = $va0 + $aa*$tt;
  }

  if ($t < $tbrake_b) {
    $xb += $vb0*$d_t;
    $vb = $vb0;
  }
  else {
    $tt = $t - $tbrake_b;
    $vb = $vb0 + $ab*$tt;
    if ($vb < 0.0) {
      $vb = 0.0;
    }
    else {
      $xb = ($vb0*$tbrake_b) + ( $vb0*$tt + 0.5*$ab*($tt**2.0) );
    }
  }

  printf " %6.3f %8.3f %8.3f  %8.3f %8.3f \n",
         $t, $xa, $va, $xb, $vb;

}







exit 0;
