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);
}
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);
}