global proc float[] nearestApproach(float $positionX, float $positionZ,float $tempPosX, float $tempPosZ, float $velocity, float $tempVelocity, float $directionX, float $directionZ, float $tempDirX, float $tempDirZ) { float $dpx, $dpz, $dv; float $time; $dpx = $positionX - $tempPosX; $dpz = $positionZ - $tempPosZ; $ddx = $directionX - $tempDirX; $ddz = $directionZ - $tempDirZ; $dv = ($velocity + $tempVelocity)/ 2.0; if((($ddx + $ddz) * $dv) < 0.0001) { $time = 0.5; } else { $time = -1.0 * ($dpx + $dpz) / (($ddx + $ddz) * $dv); } if($time > 0) // don't care if nearest approach happens in the past. // now calculate the distance of the nearest approach $dpx += $time * $dv * $ddx; $dpz += $time * $dv * $ddz; float $distance = sqrt(($dpx * $dpx) + ($dpz * $dpz)); float $retVal[2]; $retVal[0] = $time; $retVal[1] = $distance; return $retVal; }