2D Vector Math Plugin

The frigame.vec2.js file provides commonly used 2D vector math functions.

New in version 2.3.0.

User API

friGame.Vec2.randomUnit(out)
friGame.Vec2.random(out, scale)
friGame.Vec2.clone(out, a)
friGame.Vec2.magnitude(a)
friGame.Vec2.squaredMagnitude(a)
friGame.Vec2.azimuth(a)
friGame.Vec2.fromMagAngle(out, mag, angle)
friGame.Vec2.fromValues(out, x, y)
friGame.Vec2.scale(out, value)
friGame.Vec2.invert(out)
friGame.Vec2.normalize(out)
friGame.Vec2.add(out, a)
friGame.Vec2.subtract(out, a)
friGame.Vec2.rotate(out, angle)
friGame.Vec2.rotateAroundPoint(out, a, axisPoint, angle)
friGame.Vec2.equals(a, b)
friGame.Vec2.distance(a, b)
friGame.Vec2.squaredDistance(a, b)
friGame.Vec2.sum(out, a, b)
friGame.Vec2.difference(out, a, b)
friGame.Vec2.dot(a, b)
friGame.Vec2.cross(a, b)
friGame.Vec2.lerp(out, a, b, t)

Instead of having a separate vector class, this plugin treats as a vector any object that has a numeric value for the x and y members.

Arguments
  • out – The vector object to change

  • a – The first vector

  • b – The second vector

Returns

If the function has an out parameter, the out object is returned. If the function does not have an out parameter, the computed numeric value is returned.

Example:

var
    // first_vec is a vector with a random angle, and a random magnitude, up to 10
    first_vec = friGame.Vec2.random({}, 10),
    // second_vec is a vector with x = 10 and y = 20
    second_vec = friGame.Vec2.fromValues({}, 10, 20)
;

// Now both first_vec and second_vec have a numeric x and y value
if (first_vec.x < 3) {
}

// This is something like
// second_vec += first_vec
friGame.Vec2.add(second_vec, first_vec);

// Functions that don't have an out parameter, return a numeric value
if (friGame.Vec2.azimuth(second_vec) < Math.PI) {
}