Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Detecting an object’s direction change

  • Detecting an object’s direction change

    Posted by Paulo Jan on October 19, 2007 at 3:10 pm

    Hi all:

    I’m trying to write an expression to detect whether an object in motion has changed its direction or not. The way I’m trying to do it is:

    1) Read the object’s X/Y position 0.5 seconds and 0.2 seconds before the current time, as well as its current position.
    2) Calculate the difference between the object’s X position 0.5 sec. ago and 0.2 sec. ago (let’s call it Mov1X), and the difference between its X position 0.2 sec. ago and now (let’s call it Mov2X).
    3) If Mov1X and Mov2X have different signs, then the object has changed directions. I.e.: the object was moving in the X-positive direction in the 0.5 sec. ago – 0.2 sec. ago interval, but in the X-negative direction in the 0.2 sec. ago – present interval. In that case, Mov1X would be positive, while Mov2X would be negative.
    4) Do the same for the Y position, obviously.

    Now, my problem is that I can’t think of a way to compare the signs of two numbers. I don’t need their absolute values or anything, just to know whether one is positive and the other negative or viceversa. I’m sure that this is very simple, but I just can’t think of a way to do it. Anyone can help? (Alternatively, is there a more elegant way to detect direction changes?)

    Paulo.

    Dan Ebberts
    replied 17 years, 3 months ago
    2 Members · 3 Replies
  • 3 Replies
  • Dan Ebberts

    October 19, 2007 at 4:51 pm

    I think I’d do something like this – this opacity expression sets the opacity to 50% when the layer has changed direction by more than the angle tolerance setting over the period defined by delta (half a frame in this case):

    delta = thisComp.frameDuration/2;
    tolerance = .05;
    v1 = transform.position.velocity;
    angle1 = Math.atan2(v1[1],v1[0]);
    v2 = transform.position.velocityAtTime(time-delta);
    angle2 = Math.atan2(v2[1],v2[0]);

    if (Math.abs(angle1 – angle2) > tolerance) 50 else 100;

    Hopefully that helps.

    Dan

  • Paulo Jan

    October 19, 2007 at 8:34 pm

    Wow, thanks. Now, if only I could understand what you’ve done in your code… 🙂 I’ve been reading it and browsing the references, and I have two doubts in particular:

    1) The “tolerance” variable has the angle… in radians. Am I right?

    2) I’ve read the AE Expressions Reference several times (both today and in the past, when I had to solve similar problems), but I’ve never been able to understand exactly what the “velocity” attribute is, much less when it talks about it being “the tangent vector value” of the position (I’ve half-forgotten my high-school trigonometry). Without getting into a remedial math class, am I wrong if I suppose that “velocity” is a sort of vector that points in the direction of the object’s movement?

    Thanks in advance.

    Paulo.

  • Dan Ebberts

    October 19, 2007 at 9:54 pm

    1) Not exactly. The tolerence variable is the amount (in radians) that the angle has to change from the previous half frame to be considered as having changed direction.

    2) Velocity is a vector that contains the speed and direction of movement. It’s stored as an x component and a y component. Using Math.atan2(), you can use those components to calculate the angle (relative to the world coordinate system) of movement at that moment.

    Dan

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