Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Problem with vector math

  • Problem with vector math

    Posted by Jorge Torrens on September 1, 2007 at 10:27 am

    HI, I’m having a lot of trouble with a 3d trigonometry problem. It’s not in after effects, but in a different software, Nuke, but as there are a few math wizards in here I thought I would give it a try in the Cow.
    So if anybody could have a look and try to spot where my mistake is, I would really appreciate it.
    Actually I know how to do it in AE so I would try to translate my problem to AE terms.

    The problem is, given 3 points in space a b c orient the resulting plane

    what I do is:

    1. get the vector normal to the plane with the following method


    u = b – a
    v = c – a

    where u and v are two vectors that are on the plane


    N = cross (u,v)

    where N is the normal vector to the plane obtained from the result of the cross product of the two vectors u and v
    This step would be the same in after effects.

    2. calculate the 3 angles using atan2


    angle_x = atan2(N_z,N_x)
    angle_y = atan2(N_y,N_x)
    angle_z = atan2(N_y,N_z)

    where N_x, N_y, N_z are the x y and z components of N

    In after effecs I would just do lookAt(a, N) and be finished

    3. convert the angles from radians to degrees.

    4. In the application, in my case Nuke, but would be the same in AE, create a 3D solid with position values in the average point of a b c and with angle_x angle_y and angle_z as the orientation values.

    So, In short would be how to orient a plane in 3d space without using the lookat function.

    I’ve been trying to solve this for a week now and I can’t find where the problem is.

    Thanks in advance

    Jorge

    Dan Ebberts replied 11 years, 9 months ago 4 Members · 9 Replies
  • 9 Replies
  • Dan Ebberts

    September 1, 2007 at 2:28 pm

    I think the problem is that you can’t calculate the angles independently and then just plug them in. If you watch what happens when you orient a plane by hand you’ll see that the angles interact. Unfortunately, I think you’re going to have to use 3D rotation transforms (matrix math). It’s challenging the first time you do it, but rewarding. 🙂

    Dan

  • Jorge Torrens

    September 1, 2007 at 7:00 pm

    Thanks a lot Dan, I think I understand what you say about calculating the angles independently. Would you mind to give me some input on how to deal with the problem?
    I’m really quite lost and any help would be very much appreciated.

    Best regards

    Jorge

  • Dan Ebberts

    September 1, 2007 at 8:12 pm

    Here’s an example I dug up from my archives that might help. It works in AE’s 3D world space (+y is down, +z is away from front view) and calculates the world space orientation of “Layer 1”. The rotation matrix has been reduced down to a couple of sin & cos operations:

    L = thisComp.layer(“Layer 1”);
    u = L.toWorldVec([1,0,0]);
    v = L.toWorldVec([0,1,0]);
    w = L.toWorldVec([0,0,1]);

    sinb = clamp(w[0],-1,1);
    b = Math.asin(sinb);
    cosb = Math.cos(b);
    if (Math.abs(cosb) > .0005){
    c = -Math.atan2(v[0],u[0]);
    a = -Math.atan2(w[1],w[2]);
    }else{
    a = Math.atan2(u[1],v[1]);
    c = 0;
    }
    [radiansToDegrees(a),radiansToDegrees(b),radiansToDegrees(c)]

    Dan

  • Luis Martinez

    August 5, 2014 at 11:04 pm

    Hello, Dan and everyone.

    I’m trying to delve into rotations, orientations and matrices within AE and I’m quite interested in the fact you mentioned about it’s complicated but rewarding subject. Could you bring us a little more light on the hows and whys? How did you approach the problem so you ended up with such piece of code? What I’m really looking for is to cope with rotations with confidence so I can build more sofisticated rigs.

    Thank you!

  • Dan Ebberts

    August 5, 2014 at 11:45 pm

    Well, the complicated part is getting the chops to work with and understand rotation matrix math. There are a lot of resources out there, but it does take some time to figure out. I have the derivation of this code plotted out in a notebook and the main reference is sourced to David Eberly. It looks very much like this one:

    https://www.geometrictools.com/Documentation/EulerAngles.pdf

    but it’s a little different and I’m not sure exactly where it came from.

    The rewarding part is knowing that you figured it out, it makes sense, and that you could probably do it again if you had to.

    Dan

  • Quang Nguyen

    August 22, 2014 at 10:42 am

    Hello Dan,

    If I don’t have a layer to input but a 3D vector, how would I put it into this code?

    Best regards,
    Quang.

  • Dan Ebberts

    August 22, 2014 at 4:41 pm

    If you only have one vector, I guess if you normalized it, it would correspond to the w vector in the expression. You’d still need something for u and v though, so you’d probably be better off using something like this:

    v = [100,100,100]; // your vector
    p = toWorld(anchorPoint);
    lookAt(p+v,p)

    Dan

  • Quang Nguyen

    August 22, 2014 at 7:55 pm

    Thanks Dan,

    I got it like this:
    u = [-1, 0, 0];
    v = [0, -1, 0];
    w = [0, 0, -1];

    And it worked for my case. I wonder if you could help me simplify it more to speed up a little bit?

    You saved me most times Dan!

  • Dan Ebberts

    August 22, 2014 at 10:17 pm

    I’m not sure how you would simplify it unless you can make certain assumptions about the vector.

    Dan

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