vec2 is 2D math vector class. It supports many operations such as dot and cross product, rotation, tests for proximity, find orthogonal vectors, clamping, interpolation and more. The API calls can be chained which allows the creation of complex operation with a single line of code.

Besides the usual vector operations the class also has some useful ones for game development e.g. perpendicular rotation to the left/right. The class is a slightly reworked version of the original work made by playchilla.


var v1:Vec2 = new Vec2;

// add sub mul div
v1.addSelf(new Vec2(1, 2)).subSelf(new Vec2(3, 4)).mulSelf(new Vec2(2, -3)).divSelf(new Vec2(2, 3));
v1 = new Vec2().addXY(1, 2).subXY(3, 4).mulXY(2, -3).divXY(2, 3);

// length and angle setters
v1.setXY(1, 1);
v1.length = 4;
v1.angle = Math.PI / 2;

// scale
v1.setXY(1, 2).scaleSelf(3);
v1.setXY(1, 2).scale(3)

// normalize
v1.setXY(10, 0).normalizeSelf();
v1.setXY(0, 10).normalize(5);
Math. URL.