Vector reference
Creating a vector
Vec(3, 5);
// Result: Vec(3, 5)
You don’t need to write new.
Accessing the x and y properties of a vector
Vec(3, 5).x;
// Result: 3
Vec(3, 5).y;
// Result: 5
Vector arithmetic
Cuttle allows you to do vector arithmetic with the standard arithmetic operators.
Vec(1, 2) + Vec(4, 1);
// Result: Vec(5, 3)
Vec(1, 2) - Vec(4, 1);
// Result: Vec(-3, 1)
Vec(1, 2) * 3;
// Result: Vec(3, 6)
Vec(1, 2) / 2;
// Result: Vec(0.5, 1)
This would not be possible in normal javascript. We do some fancy things with the Cuttle editor to make this work.
Length and distance
Vec(1, 1).length();
// Result: 1.4142…
Vec(1, 3).distance(Vec(1, 5));
// Result: 2
Angles
Getting the angle of a vector:
Vec(1, 1).angle();
// Result: 45
Making a vector from an angle:
Vec(1, 0).rotate(45);
// Result: Vec(0.7071…, 0.7071…)
Additional functions
vec.normalize() will mutate the vector to be of unit length
Vec(2, 1).normalize();
// Result: Vec(0.8944…, 0.4472…)
Vec(2, 1).normalize().length();
// Result: 1
Vec.dot(a, b) will return the dot product of a and b.
Vec.dot(Vec(1, 2), Vec(3, 4));
// Result: 11
Component-wise math functions
Some common math functions can take vectors as arguments. When a vector is passed to one of these functions, the result will be a vector.
min(Vec(1, 4), Vec(2, 3));
// Result: Vec(1, 3)
// Equivalent to: Vec(min(1, 2), min(4, 3))
min(Vec(1, 4), 3);
// Result: Vec(1, 3)
// Equivalent to: Vec(min(1, 3), min(4, 3))
Math functions that support component-wise calls
abs sign
floor ceil round trunc
max min clamp saturate
mix smoothstep modulo moduloDistance