1
\$\begingroup\$

I have an object with vec2 position, velocity, and destination with 2 variables maxspeed and acceleration. In each step, its position is calculated as follows:

vec2 desiredVel = destination - position;

var dist = desiredVel.length;
if (2 * dist * acceleration < currentSpeed*currentSpeed)
    desiredVel.zero();

vec2 steering = desiredVel - velocity;
steering.setLength(acceleration);

velocity += steering * dt;
velocity.truncate(maxspeed);
currentSpeed = velocity.length();

position += velocity * dt;

Basically the object will accelerate to its maxspeed towards destination, then maintains speed until the a certain distance to destination. At that distance, the object will decelerate at the -max acceleration rate so that it stops approximately to the destination point.

My question is that if I have an object with an initial velocity and I set a destination point, how can I estimate the time it will take to reach the destination point? Assuming its maxspeed and max acceleration doesn't change and no other force/acceleration applies to the object in the path.

\$\endgroup\$
4
\$\begingroup\$

Distance d is the integral of velocity v (calculus). Velocity v is the integral of acceleration a. If you start at velocity s, and you travel for time t, then distance will be d = s * t + 1/2 * a * t^2.

You will have two cases.

  1. If the object does not reach maxspeed, then you'll have one part where you're accelerating and one part when you're decelerating. You'll have to solve the equations for the velocity you reach, and then from velocity you can tell the first part takes time (velocity-initialspeed)/acceleration and the second part times velocity/acceleration. I believe it ends up being velocity = sqrt((totaldistance * acceleration + initialspeed^2) / 2) but I haven't double checked this.
  2. If the object does reach maxspeed, then you'll have three parts. For the third part, from maxspeed to 0, the time it takes will be maxspeed/acceleration, and from the time you can calculate the distance traveled. For the first part, from initialspeed to maxspeed, the time it takes will be (maxspeed-initialspeed)/acceleration, and from the time you can calculate the distance traveled. The second part will have to travel the remaining distance, so the time it takes will be (totaldistance-firstdistance-thirddistance)/maxspeed.

I've not tested this - sorry. :-(

\$\endgroup\$
  • \$\begingroup\$ I am having problem visualizing how to calculate the time for each segment (v0 -> maxspeed, constant speed, speed -> 0) because the path the object takes can be a curve/ not straight. How do I calculate the start and end of each segment (both in position and time)? Or does it not matter? \$\endgroup\$ – user62462 Mar 7 '15 at 21:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.