Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity › Forums › Adobe After Effects Expressions › Distance Travelled Expression

  • Distance Travelled Expression

    Posted by Christopher Johnstone on February 1, 2013 at 10:08 am

    I am trying to generate an accumulation for distance travelled.

    Knowing that Time x Speed = Distance

    The code attached is successful in accumulating my speed but I now need to add a valueAtTime multiplier to get my distance.

    Can anyone help shine a light?

    Many thanks in advance – Chris

    // time x speed = distance //

    spd = comp("- CNTL - MPH -").layer("- MPH -").effect("MPH")("Slider");

    accum = 0;
    for (f = timeToFrames(inPoint); f <= timeToFrames(); f++){
    accum += spd.valueAtTime(framesToTime(f));
    }
    s = value + accum;

    [s]

    Christopher Johnstone

    Christopher Johnstone replied 13 years, 7 months ago 2 Members · 6 Replies
  • 6 Replies
  • Dan Ebberts

    February 2, 2013 at 4:26 am

    Try multiplying your accum variable by (time-inPoint). That should give you the distance traveled in the units of your speed control (pixels?)

  • Christopher Johnstone

    February 5, 2013 at 3:14 pm

    Hi Dan,

    Im stumped. Would you mind breaking that down for me.

    Many thanks – Chris

    Christopher Johnstone

  • Dan Ebberts

    February 5, 2013 at 3:46 pm

    It’s hard to get very specific without seeing how you have things set up, but I think that your accumulator should already be proportional to the distance traveled. You might need to multiply by some factor (not elapsed time as I originally suggested). So something like this (assuming it’s a position expression) would give you movement in the x direction:


    spd = comp("- CNTL - MPH -").layer("- MPH -").effect("MPH")("Slider");
    mult = .1;
    accum = 0;
    for (f = timeToFrames(inPoint); f <= timeToFrames(time); f++){
    accum += spd.valueAtTime(framesToTime(f));
    }

    value + [accum*mult,0]

    Dan

  • Christopher Johnstone

    February 5, 2013 at 4:32 pm

    Well it is all pretty basic my end.
    I am using a slider with some key frames to drive a number for MPH.
    This MPH is represented by a text number is in a digital dash readout.

    That is what this bit is:
    comp(“- CNTL – MPH -“).layer(“- MPH -“).effect(“MPH”)(“Slider”);

    I am trying to generate an accurate distance travelled number within my driving simulation given that at frame 0 my speed and distance is zero.

    I am unsure if I have to use the multiplier 0.1 in your example.

    Hope this makes a little more sense?
    Let me know what you think.

    Cheers – Chris

    Christopher Johnstone

  • Dan Ebberts

    February 5, 2013 at 5:51 pm

    If your speed control is miles per hour and you want your text layer to display miles traveled, I think this is what you want:


    spd = comp("- CNTL - MPH -").layer("- MPH -").effect("MPH")("Slider");
    accum = 0;
    for (f = timeToFrames(inPoint); f <= timeToFrames(time); f++){
    accum += spd.valueAtTime(framesToTime(f));
    }
    accum*thisComp.frameDuration/3600

    Dan

  • Christopher Johnstone

    February 6, 2013 at 10:23 am

    Lovely Dan,

    just what the doctor ordered – this works perfectly.

    Could you explain a little how the accum function works?
    I have tried to explain it to a friend a little and found myself a little unsure.

    Many thanks – Chris

    Christopher Johnstone

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy