Jump to content
  • Advertisement
Sign in to follow this  

Solving three equations

This topic is 2782 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello all,

I've been working on a piece of code that is designed to move an object smoothly over a given distance. My intent is to have the object move in three segments: A) acceleration B) max speed C) decceleration. The motion should be smooth ( no jumps in speed ) and object should always end up with a speed of zero. I am using a function to compute distance over time for each segment with a fixed acceleration/deccelearation and a top speed, so in general my problem is just finding the appropriate time values.

My current solution works by always starting the object with a speed of zero, so it's fairly easy to determine the time values because the acceleration time is always the same as the deccelearation time ( and so is the distance ). Either I have time to hit my top speed ( in which case all values are known except segment B ), or I only accellerate up to half the distance, but here I can simply plug in the values and solve for time using the quadratic equation.

However, a problem occurs when the object needs to re-compute a movement while already moving: its speed will "jerk" back to 0 as it begins the next set of segments (which looks bad visually). What I would like to do is to use the current object speed for segment A), and find the three time values that will accelerate to a max, hold, and deccelerate. However, I cannot seem to substitute my eqations down into anything with a single variable that could actually be solved. Below is what I am working with; as a note, S is speed, A is acceleration, T is time, and D is distance. Subscripts a,b,c refer to the Segment, i refers to the initial speed, and m refers to the max speed that could be reached.

Currently, I have 2 equations for speed:

(Segment A) : Sm = Si + A(Ta)
(Segment C) : 0 = Sm - A(Tc)

And I have 4 equations for distance:

(Segment A) : Da = Si(Ta) + 1/2(A)(Ta^2)
(Segment B) : Db = Sm(Tb)
(Segment C) : Dc = Sm(Tc) - 1/2(A)(Tc^2)

(Total ) : D = Da + Db + Dc

If I plug in the three segment distance equations to the total, I end up with four unknown variables: Sm, Da, Db, and Dc. I could then substitute relative to Sm for Ta and Tc using the speed equations, but I unfortunatley don't have anything to substitute for Db. So at best I end up with 2 unknowns, and no equations that could further eliminate one of them. Does anybody know of a way that I might be able to solve this problem? Or, is there a better way of approaching this kind of "controlled speed curve"? Thanks much for any advice!

Share this post

Link to post
Share on other sites
Do you really have to calculate the velocity beforehand as a function of time?

In the context of a simulation/game I usually do this by calculating for every time step whether to accelerate or break.

if(distance <= 0.5*v*v/a_brake)
v = max(0, v - a_brake*dt);
v = min(v_max, v+a*dt);

This will automatically hit the brakes at the latest possible point or accelerate to v_max otherwise.

Edit: whoops, had the min and max functions the wrong way around

Share this post

Link to post
Share on other sites
If I understood the problem correctly, then:

The acceleration phase would ideally last until the current speed

v( t ) := a * ( t - t[sub]0[/sub] ) for t >= t[sub]0[/sub], a > 0 const, v( t[sub]0[/sub] ) = 0

has reached the (desired) maximum at a time t[sub]1[/sub]

v( t[sub]1[/sub] ) = a * ( t[sub]1[/sub] - t[sub]0[/sub] ) == v[sub]max[/sub]

so that the durartion of acceleration will be

T[sub]1[/sub] := t[sub]1[/sub] - t[sub]0[/sub] = v[sub]max[/sub] / a

and the distance driven in this time will be

D[sub]1[/sub] := 0.5 * a * T[sub]1[/sub][sup]2[/sup]

Because later the deceleration will be -a, the deceleration phase will last as long as the acceleration phase and the distance will be the same:

D[sub]3[/sub] := D[sub]1[/sub]
T[sub]3[/sub] := T[sub]1[/sub]

Obviously, D[sub]1[/sub]+D[sub]3[/sub] must be less or equal to D to let the object reach v[sub]max[/sub] at all. Then the route with constant speed will be

D[sub]2[/sub] = D - D[sub]1[/sub] - D[sub]3[/sub]

for which a time of

T[sub]2[/sub] := D[sub]2[/sub] / v[sub]max[/sub]

will be needed. Hence the sequence of time moments will be

{ start: t[sub]0[/sub]; acc_to_const: t[sub]1[/sub] = t[sub]0[/sub] + T[sub]1[/sub]; const_to_dec: t[sub]2[/sub] = t[sub]1[/sub] + T[sub]2[/sub]; stop: t[sub]3[/sub] = t[sub]2[/sub] + T[sub]3[/sub] }

All necessary values are IMHO given.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!