UNPKG

minecraft.js

Version:

Minecraft data serialization/deserialization and networking

60 lines (51 loc) 1.16 kB
/** @constructor */ var Vector2 = module.exports = function(x, z) { this.x = x; this.z = z; }; Vector2.prototype.toString = function() { return "(" + this.x + ", " + this.z + ")"; }; Vector2.prototype.distanceTo = function(v2) { return Math.sqrt(Math.pow(v2.x, 2) + Math.pow(v2.z, 2)); }; Vector2.prototype.angleTo = function(v2) { var diff = v2.subtract(this); return Math.atan2(diff.x, diff.z) * (180 / Math.PI); }; Vector2.prototype.add = function(v2) { if(typeof v2 == 'number') { v2 = new Vector2(v2, v2); } return new Vector2( this.x + v2.x, this.z + v2.z ); }; Vector2.prototype.subtract = function(v2) { if(typeof v2 == 'number') { v2 = new Vector2(v2, v2); } return new Vector2( this.x - v2.x, this.z - v2.z ); }; Vector2.prototype.multiply = function(v2) { if(typeof v2 == 'number') { v2 = new Vector2(v2, v2); } return new Vector2( this.x * v2.x, this.z * v2.z ); }; Vector2.prototype.divide = function(v2) { if(typeof v2 == 'number') { v2 = new Vector2(v2, v2); } return new Vector2( this.x / v2.x, this.z / v2.z ); };