decorative banner

Vector math methods


    These are global methods that perform operations on arrays, treating them as mathematical vectors. Unlike built-in JavaScript methods, such as Math.sin(), you do not use the Math prefix with these methods. Unless otherwise specified, vector math methods are lenient about dimensions and return a value that is the dimension of the largest array, filling in missing elements with zeros. For example, the expression "add([10, 20], [1, 2, 3])" returns [11, 22, 3].

    Array add(v1, v2) {v1 and v2 are arrays} Adds two vectors, componentwise.

    Array sub(v1, v2) {v1 and v2 are arrays} Subtracts two vectors, componentwise.

    Array mul(v1, amount) {v1 is an array, amount is a number} Multiplies every element of the vector by the amount.

    Array div(v1, amount) {v1 is an array, amount is a number} Divides every element of the vector by the amount.

    Number length(v) {v is an array} Returns the length of vector v.

    Number length(p1, p2) {p1 and p2 are arrays} Returns the distance between two points. P2 is optional. For example, "length(p1, p2)" is the same as "length(sub(p1, p2))".

    Array normalize(v) {v is an array} Normalizes the vector so that its length is 1.0. This is a short way of writing "div(v, length(v))".

    Array [2 or 3] cross(v1, v2) {v1 and v2 are arrays [2 or 3]} Returns a vector cross-product. Refer to a math reference guide or JavaScript guide for more information.

    Number dot(v1, v2) {v1 and v2 are arrays} Returns a dot product, which is the result of multiplying the two vectors together componentwise.

    Array [3] look_at(from_pt, at_pt) {from_pt and at_pt are arrays [3]} From_pt is the location in world space of the layer you want to orient. At_pt is the point in world space you want to point the layer at. The return value can be used as an expression for the Orientation property, making the layer's z-axis point at at_pt. It is especially useful for cameras and lights. For example, "look_at(position, this_comp.layer(1).position)". If you use this on a camera, turn off auto-orientation.