Here you can find the source of calcDistanceIfAccel(double startVelocity, double accel, double targetVelocity, double duration)
Parameter | Description |
---|---|
startVelocity | the initial velocity of the Vehicle |
accel | the acceleration of the Vehicle during this time |
targetVelocity | the velocity at which the Vehicle will stop accelerating |
duration | the duration for which this all takes place |
public static double calcDistanceIfAccel(double startVelocity, double accel, double targetVelocity, double duration)
//package com.java2s; public class Main { /**//from w w w . ja v a 2 s .co m * Determine how far the Vehicle will go in the given duration, if * it starts at the given starting velocity and accelerates at the given * acceleration toward the provided target velocity. * * @param startVelocity the initial velocity of the Vehicle * @param accel the acceleration of the Vehicle during this * time * @param targetVelocity the velocity at which the Vehicle will stop * accelerating * @param duration the duration for which this all takes place * @return how far the Vehicle will travel in this time */ public static double calcDistanceIfAccel(double startVelocity, double accel, double targetVelocity, double duration) { // If we're speeding up if (accel >= 0.0) { // If we're already at or above the target velocity if (startVelocity >= targetVelocity) { // We won't setMaxAccelWithMaxTargetVelocity, so just use current velocity return startVelocity * duration; } // Otherwise we need to figure out how much accelerating will be done double maxChange = accel * duration; double requestedChange = targetVelocity - startVelocity; // If our requested change is at least our maxChange, we'll be // accelerating the whole time if (requestedChange >= maxChange) { return (startVelocity + (maxChange / 2.0)) * duration; } else { // Otherwise, we will setMaxAccelWithMaxTargetVelocity for part of it double accelDuration = (targetVelocity - startVelocity) / accel; // Find our average velocity during this time double avgAccelVelocity = (targetVelocity + startVelocity) / 2; // The distance is how far we go during acceleration plus how far // we go after that return avgAccelVelocity * accelDuration + targetVelocity * (duration - accelDuration); } } else { // If we're decelerating // If we're already at or below the target velocity if (startVelocity <= targetVelocity) { // We won't decelerate, so just use current velocity return startVelocity * duration; } // Otherwise we need to figure out how much decelerating will be done double maxChange = accel * duration; double requestedChange = targetVelocity - startVelocity; // If our requested change is at least (as negative as) our maxChange, // we'll be decelerating the whole time if (requestedChange <= maxChange) { return (startVelocity + (maxChange / 2.0)) * duration; } else { // Otherwise, we will decelerate for part of it double decelDuration = (targetVelocity - startVelocity) / accel; // Find our average velocity during this time double avgDecelVelocity = (targetVelocity + startVelocity) / 2; // The distance is how far we go during deceleration plus how far // we go after that return avgDecelVelocity * decelDuration + targetVelocity * (duration - decelDuration); } } } }