Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Forums Adobe After Effects Expressions sin^-1 available in expressions?

  • sin^-1 available in expressions?

  • Mark Walczak

    June 4, 2009 at 4:23 pm

    Hi everyone,

    I’m doing a personal exercise in expression writing. I am creating a triangle in AE and have been able to write expressions that are able to find the length of each side, given the (x,y) coordinates of each vertex of the triangle. I am now looking to further complicate things by being able to find the angle of each vertex. Given this, I am wondering if there is a sin^-1, cos^-1, tan^-1 etc. function within AE javascript. Basically, I am trying to use the Law of Cosines to find each angle, but cannot find an inverted cosine function in order to find the angle, given the length of each side.

    Make sense? I hope so, ’cause I’m pretty sure I can’t repeat it 😛

    Any help would be greatly appreciated!!!

    Thanks in advance,

    Mark

  • Mark Walczak

    June 4, 2009 at 5:03 pm

    Thanks everyone!

    Dan, that site looks amazing.

    I’m still, however having some issues getting it all to work. Perhaps I’ll post the scene if I can.

    Thanks again!

    What makes you explode?
    http://www.explosivegraffix.com

  • Dan Ebberts

    June 4, 2009 at 5:03 pm

    Math.asin(), Math.acos(), math.atan(), and math.atan2() are what you’re looking for. This is a good reference:

    https://www.w3schools.com/jsref/jsref_obj_math.asp

    Dan

  • Koby Goldberg

    June 4, 2009 at 5:07 pm

    Yes, they exist. Use:

    Math.asin(x)
    Math.acos(x)
    Math.atan(x)

    Koby.

  • Koby Goldberg

    June 4, 2009 at 8:40 pm

    Mark,
    Note that asin, acos,… return the angle in Radians!
    If you want to convert it to Degrees, you should multiply the result by 180 and divide by pi (Math.PI).
    However, if you are using sin(x) or cos(x), x should be in Radians.

    Koby.

  • Mark Walczak

    June 5, 2009 at 2:51 am

    Thanks for the tip! Fortunately, I was able to find the radians-to-degrees command, which seemed to do the trick. Thanks for all of your help!

    What makes you explode?
    http://www.explosivegraffix.com

  • jim lefevre

    March 31, 2021 at 8:14 am

    Mark (and everyone),

    THANK YOU! I’m doing exactly the same thing and have been hitting my head against a wall for a day or so.

    You guys are all so great.

    Jim

  • jim lefevre

    March 31, 2021 at 8:47 am

    Ah… spoke too soon!

    As Mark was doing, I’m trying to figure out the angle of a right angled triangle from the lengths of the sides using inverse sin.

    I’ve followed all of the replies here and am getting somewhere all up until I try and call the inverse sin using …

    Math.asin().

    …where the expression gives me an error. Here is the code…

    //half the base length
    m = thisComp.layer("DRIVER_all_sides").effect("length - X")("Slider")/2;
    //pythagorus to find the length of the hypotenuse
    a = thisComp.layer("DRIVER_all_sides").effect("length - X")("Slider")/2;
    b = thisComp.layer("DRIVER_all_sides").effect("HEIGHT_TRIANGLE")("Slider");
    x = Math.sqrt(((a*a) + (b*b)));
    //angle via inverse sin
    q = (x/m);
    p = Math.asin(q);
    p

    The error is the “Error: couldn’t turn the result into a numeric value” one

    I’ve yet to get it to the stage of turning it the Radians into degrees as it’s falling over as soon as Math.asin() is in there.

    Does anyone have any suggestions? (and I’ve had a trawl through that wonderful Math resource you posted Dan)

  • jim lefevre

    March 31, 2021 at 12:33 pm

    HA!!

    Done it I think.

    For some reason my equation to get the angle via inverse sin had the values dividing the wrong way – I was dividing the base length by the hypotenuse or the other way round – which meant that I was coming out with a value greater than 1 which, it appears, an inverse sine can’t do (I have no idea how this works btw).

    I swapped them round and came to a result which worked and then had to take that value away from 90 degrees so that it was rotating in the direction I needed.

    Works perfectly now…

    //I have two slide values that control the Base length and the height of the triangle (the altitude)
    //half the base length to get me a value that creates a right angle triangle essentially
    m = thisComp.layer("DRIVER_all_sides").effect("length - X")("Slider")/2;


    //pythagorus to find the length of the hypotenuse
    a = thisComp.layer("DRIVER_all_sides").effect("length - X")("Slider")/2;
    b = thisComp.layer("DRIVER_all_sides").effect("HEIGHT_TRIANGLE")("Slider");
    x = Math.sqrt(((a*a) + (b*b)));



    //get the angle via inverse sin

    q = Math.asin(m/x);


    //convert this to degrees

    p = (radiansToDegrees(q));



    //final result needs offsetting so it rotates the correct way when I change the base triangle value

    90-p

  • Koby Goldberg

    March 31, 2021 at 5:06 pm

    Hi Jim,

    You should be able to do the calculation slightly simpler by using Math.atan2 instead of Math.asin, and thus you will not need the calculation of x (you won’t need the sqrt calculation). Simpler expressions could make your render faster, because expressions are being calculated every frame.

    Try this, I think it should give you the same result as above:

    a = thisComp.layer(“DRIVER_all_sides”).effect(“length – X”)(“Slider”)/2;
    b = thisComp.layer(“DRIVER_all_sides”).effect(“HEIGHT_TRIANGLE”)(“Slider”);

    Math.atan2(b,a)

    If you still need to do the (90-p) conversion, then just swap a and b in the Math.atan2, i.e.: Math.atan2(a,b), but I think atan2(b,a) should give you the final angle you wanted.

    Cheers,

    Koby

Viewing 1 - 10 of 11 posts

Log in to reply.

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