开发者

maths: motion depending on time

i have a mathematical problem:

i have a function where the only parameter is the current time. the return should be a position which is used to place an object on a certain place.

int position(int time)
{
    int x = 0; //TODO implement x depending on time
    return x;
}

so basically the function is called every frame to put the object in motion.

the motion should look like this (this is the actual question):

  1. a linear motion for time A, the object moves at constant speed
  2. no motion 开发者_开发百科for time B, the object is stopped
  3. repeat at 1.

thanks!

edit: ok in other words: imagine a car that drives at constant speed for A minutes and then stops for B minutes. then drives again for A minutes and stops again for B minutes.

where is the car at time X?


OK, if I understand correctly:

int position(int time)
{
    int count_of_AB_cycles = time / (A + B);
    int time_in_current_cycle = time % (A + B);
    int time_moved_this_cycle = (time_in_current_cycle < A) ? time_in_current_cycle : A;
    int total_time_moving = (count_of_AB_cycles * A) + time_moved_this_cycle;
    return total_time_moving * speed;
}

assuming integer division, etc - you'll need floor()s if A and B are non-integers etc.


With the limited information you give, nothing more specific than

return x * THE_SPEED

can be suggested.

The second condition might be intended as a maximum on the return value, but it's hard to tell without context.

The third specification baffles me.


Something like

int position(int t)
{
  static int pt = -1;
  static int px = 0;
  static int state = 1; // running...
  static int lastt = 0;
  if (pt < 0) { pt = t; lastt = t; }
  if ( state == 1 ) {
     if ( (t-pt) > A ) {
        state = 0;
        pt = t;
     }
  } else {
     if ( (t-pt) > B ) {
        state = 1;
        pt = t;
     }
  }
  px += state ? SPEED*(t-lastt) : 0; // advance
  lastt = t;
  return px;
}

EDIT Comment of the usage of the prev code

The code is meant to be used "run time": it gives no the result provided the time t once and for all. It is programmed to "move" virtually the car by one step each time the function is called, according to how much time is passed from previous calling. Suitable for a "game" where the func is called every "tick" or so, and the position of the car must be updated as tick increases, so that it could be e.g. drawn on the screen, tick by tick, in the current position.

If the OP Question was about knowing where the car is at time t, "mathematically", other solution are the good one (also, read my second comment to the question)


For the first two time intervals you can do this:

time = min(time, TIME_INTERVAL_A);
return time * CONST_SPEED;

This will cause your object to travel at a constant speed over time interval A and then stop.


I'm not sure if I understood your problem correctly, but do you want something like this?

(assuming C)

int position(int time)
{
    static int x = 0;
    x += time * SPEED;
    return x;
}


If I understand correctly, you have an object which moves at a constant speed for a while, then stops, then starts again (at the same speed ?), then stops, then starts, and so forth ? It moves along a 1D path so distance from origin is the only output you are interested in ?

I suggest that you define an auxiliary function called speedAt(time T) which might look something like:

if 0 < T <= 25 then 5;
if 25 < T <= 32 then 0;
if 32 < T <= 47 then 3;
if 47 < T <= 49 then 0;
if 49 < T <= 125 then 1;
if 125 < T then 0.

Now what you need to do is integrate this function to derive the distance travelled at time T which is represented by the area between the graph of T and the horizontal axis (in an x-y plot).

Since this is not a differentiable or continuous function you won't have to do any difficult maths, you can integrate numerically very easily. A bit like this:

distanceTravelled = 0
for t = 1 to T
   distanceTravelled = distanceTravelled + speedAt(t)
end

This probably looks a bit messy to you. A more sophisticated approach would be to derive the value of speedAt from a data structure, but I hope you get the picture.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜