Travel time math problem

tin

Joined
Jan 20, 2025
Messages
1
Reaction score
0
Hi =) this is my first post here so i might be doing something wrong!(if so just tell me)
I am working on a game in unity C#. but i felt this question might fit better here since it's not related to unity in any way!
My problem: I want to know the time it takes to travel and stop at a distance/position assuming my initial position to be 0. What I know is: initial velocity (v0) , acceleration(a), distance (d) and max velocity(v_max). To complicate it even more I want VelocityDirection and separate times for the following stages: (stage1/accel time, stage2/cruice time, stage3/decel time) but combined they should form total time.
I have managed to get it somewhat working but im constantly running into scenarios where it won't really work. I'd think this problem is already solved except for my stage complication. But I could probably make that work in another way if someone could help me calculate the TotalTime. important to note that d and v0 can both be positive and negative the values i have been using lately are: v0 = +-34, a=25, v_max = 50, d = 45.

I couldn't post images so here is a link to my image illustration of 7 different scenarios and the desired movement:


Here is my flawed code for reference.
//////////////////////////////////////////////////////////////
private struct ManoverStages
{
public float stage1time;
public float stage2time;
public float stage3time;
public float stage1dir;
public float stage3dir;
public ManoverStages(float stage1, float stage2, float stage3, float dir1, float dir3)
{
this .stage1time = stage1;
this .stage2time = stage2;
this .stage3time = stage3;
this .stage1dir = dir1;
this .stage3dir = dir3;
}
}

private ManoverStages TimeNeadedForManover(float v0, float a, float v_lim, float d)
{
//INITIAL VELOCCITY IS ON COURCE
bool onCource = v0 * d > 0;
bool willOverShoot;
//NORMALIZE DISTANCE
d = math.abs(d);
v0 = math.abs(v0);
//DIRECTION
float dir1 = 1;
float dir3 = -1;
//TIME
float t1;
float t2;
float t3;
//DISTANCE
float d1;
float d2;
float d3;
//REACHABLE LIMIT
float v_reach;
float t_lim;
float d_lim;
//DELTA INITIAL
float Dt0 = (0 - v0) / a;
float Dd0 = ((0 + v0) / 2) * Dt0;
//CHECK OVER SHOOT
willOverShoot = d < Dd0;

//UPDATE DISTAANCE
if(!onCource)
{
d += Dd0;
}
else if(Dd0 > d)
{
t3 = Dt0;
d3 = Dd0;
d -= Dd0;
}
//VELLOCITY ACHEVABLE ON HALF THE DISTANCE
float v_half = math.sqrt(2 * a * d / 2);
//SET REACH
v_reach = v_lim > v_half ? v_half : v_lim;
t_lim = (v_reach - 0) / a;
d_lim = (0 + v_reach) / 2 * t_lim;
//ACCELERATING STAGE
t1 = t_lim;
d1 = d_lim;
if (onCource)
{
t1 -= Dt0;
d1 -= Dd0;
//t1 = (v0 - v_reach) / a;
//d1 = (v_reach + v0) / 2 * t1;
}
//DECELERATING STAGE
t3 = t_lim;
d3 = d_lim;
//CRUICE STAGE
d2 = d - d1 - d3;
t2 = d2 / v_lim;

return new ManoverStages((onCource ? t1 : t1 + Dt0), t2,t3,dir1,dir3);
}
 
Joined
Sep 21, 2022
Messages
218
Reaction score
33
I think there are two ways to do something like this.

(1) Figure out all the different cases, then work out the math for each one. This means the human is doing all the work, very error prone, and the program is just acting like a spreadsheet.

(2) Simulate a pilot and a ship, with very small time intervals.

The pilot is just a function that given position, velocity, and target position, returns 3 possible states: accelerate, decelerate, or cruise.

The ship routine just updates the position based on what the pilot decides, records how much time is used on each state, and decides when to stop.

The pilot logic would look something like:
Code:
P: ship position
V: ship velocity
D: target position

compute minimum stop distance
(the MSD math is not too complicated)
IF (going in wrong direction) OR (too fast) THEN
  RETURN "D"
ELSEIF ABS(D-P)<=MSD THEN
  RETURN "D"
ELSEIF ABS(V)<MAX THEN
  RETURN "A"
ELSE
  RETURN "C"
ENDIF
If it were me, I would code (2) first, because the logic is simple and the math only requires basic algebra.

If that didn't give me the right answers, I'd go with method (1).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,490
Latest member
Finplus

Latest Threads

Top